package vgtex
import "gonum.org/v1/plot/vg/vgtex"
Package vgtex provides a vg.Canvas implementation for LaTeX, targeted at the TikZ/PGF LaTeX package: https://sourceforge.net/projects/pgf
vgtex generates PGF instructions that will be interpreted and rendered by LaTeX.
vgtex allows to put any valid LaTeX notation inside plot's strings.
An example of making a LaTeX plot.
Code:play
Example¶
package main
import (
"image/color"
"log"
"os"
"runtime"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/vg"
"gonum.org/v1/plot/vg/draw"
"gonum.org/v1/plot/vg/vgtex"
)
func main() {
p := plot.New()
// p.HideAxes()
p.Title.Text = `A scatter plot: $\sqrt{\frac{e^{3i\pi}}{2\cos 3\pi}}$`
p.Title.TextStyle.Font.Size = 16
p.X.Label.Text = `$x = \eta$`
p.Y.Label.Text = `$y$ is some $\Phi$`
scatter1, err := plotter.NewScatter(plotter.XYs{{X: 1, Y: 1}, {X: 0, Y: 1}, {X: 0, Y: 0}})
if err != nil {
log.Fatal(err)
}
scatter1.Color = color.RGBA{R: 255, A: 200}
scatter2, err := plotter.NewScatter(plotter.XYs{{X: 1, Y: 0}, {X: 1, Y: 0.5}})
if err != nil {
log.Fatal(err)
}
scatter2.GlyphStyle.Shape = draw.PyramidGlyph{}
scatter2.GlyphStyle.Radius = 2
scatter2.Color = color.RGBA{B: 255, A: 200}
p.Add(scatter1, scatter2)
txtFont := p.TextHandler.Cache().Lookup(
p.X.Label.TextStyle.Font,
p.X.Label.TextStyle.Font.Size,
)
c := vgtex.NewDocument(5*vg.Centimeter, 5*vg.Centimeter)
p.Draw(draw.New(c))
c.SetColor(color.Black)
c.FillString(txtFont, vg.Point{X: 2.5 * vg.Centimeter, Y: 2.5 * vg.Centimeter}, "x")
f, err := os.Create("testdata/scatter_" + runtime.GOARCH + ".tex")
if err != nil {
log.Fatal(err)
}
defer f.Close()
if _, err = c.WriteTo(f); err != nil {
log.Fatal(err)
}
err = f.Close()
if err != nil {
log.Fatal(err)
}
}
Index ¶
- type Canvas
- func New(w, h vg.Length) *Canvas
- func NewDocument(w, h vg.Length) *Canvas
- func (c *Canvas) DrawImage(rect vg.Rectangle, img image.Image)
- func (c *Canvas) Fill(p vg.Path)
- func (c *Canvas) FillString(f font.Face, pt vg.Point, text string)
- func (c *Canvas) Pop()
- func (c *Canvas) Push()
- func (c *Canvas) Rotate(rad float64)
- func (c *Canvas) Scale(x, y float64)
- func (c *Canvas) SetColor(clr color.Color)
- func (c *Canvas) SetLineDash(pattern []vg.Length, offset vg.Length)
- func (c *Canvas) SetLineWidth(w vg.Length)
- func (c *Canvas) Size() (w, h vg.Length)
- func (c *Canvas) Stroke(p vg.Path)
- func (c *Canvas) Translate(pt vg.Point)
- func (c *Canvas) WriteTo(w io.Writer) (int64, error)
Examples ¶
Types ¶
type Canvas ¶
type Canvas struct {
// contains filtered or unexported fields
}
Canvas implements the vg.Canvas interface, translating drawing primitives from gonum/plot to PGF.
func New ¶
New returns a new LaTeX canvas.
func NewDocument ¶
NewDocument returns a new LaTeX canvas that can be readily compiled into a standalone document.
func (*Canvas) DrawImage ¶
DrawImage implements the vg.Canvas.DrawImage method. DrawImage will first save the image inside a PNG file and have the generated LaTeX reference that file. The file name will be "gonum-pgf-image-<canvas-id>-<time.Now()>.png
func (*Canvas) Fill ¶
Fill implements the vg.Canvas.Fill method.
func (*Canvas) FillString ¶
FillString implements the vg.Canvas.FillString method.
func (*Canvas) Pop ¶
func (c *Canvas) Pop()
Pop implements the vg.Canvas.Pop method.
func (*Canvas) Push ¶
func (c *Canvas) Push()
Push implements the vg.Canvas.Push method.
func (*Canvas) Rotate ¶
Rotate implements the vg.Canvas.Rotate method.
func (*Canvas) Scale ¶
Scale implements the vg.Canvas.Scale method.
func (*Canvas) SetColor ¶
SetColor implements the vg.Canvas.SetColor method.
func (*Canvas) SetLineDash ¶
SetLineDash implements the vg.Canvas.SetLineDash method.
func (*Canvas) SetLineWidth ¶
SetLineWidth implements the vg.Canvas.SetLineWidth method.
func (*Canvas) Size ¶
Size returns the width and height of the canvas.
func (*Canvas) Stroke ¶
Stroke implements the vg.Canvas.Stroke method.
func (*Canvas) Translate ¶
Translate implements the vg.Canvas.Translate method.
func (*Canvas) WriteTo ¶
WriteTo implements the io.WriterTo interface, writing a LaTeX/pgf plot.
Source Files ¶
canvas.go
- Version
- v0.15.0 (latest)
- Published
- Oct 22, 2024
- Platform
- linux/amd64
- Imports
- 14 packages
- Last checked
- 2 weeks ago –
Tools for package owners.