...

Source file src/gonum.org/v1/plot/text/latex_example_test.go

Documentation: gonum.org/v1/plot/text

     1  // Copyright ©2021 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package text_test
     6  
     7  import (
     8  	"image/color"
     9  	"log"
    10  	"math"
    11  	"testing"
    12  
    13  	"gonum.org/v1/plot"
    14  	"gonum.org/v1/plot/cmpimg"
    15  	"gonum.org/v1/plot/font"
    16  	"gonum.org/v1/plot/font/liberation"
    17  	"gonum.org/v1/plot/plotter"
    18  	"gonum.org/v1/plot/text"
    19  	"gonum.org/v1/plot/vg"
    20  	"gonum.org/v1/plot/vg/draw"
    21  )
    22  
    23  func ExampleLatex() {
    24  	fonts := font.NewCache(liberation.Collection())
    25  	plot.DefaultTextHandler = text.Latex{
    26  		Fonts: fonts,
    27  	}
    28  
    29  	p := plot.New()
    30  	p.Title.Text = `$f(x) = \sqrt{\alpha x \Gamma}$`
    31  	p.X.Label.Text = `$\frac{\sqrt{x}}{2\pi\Gamma\gamma}$`
    32  	p.Y.Label.Text = `$\beta$`
    33  
    34  	p.X.Min = -1
    35  	p.X.Max = +1
    36  	p.Y.Min = -1
    37  	p.Y.Max = +1
    38  
    39  	labels, err := plotter.NewLabels(plotter.XYLabels{
    40  		XYs: []plotter.XY{
    41  			{X: +0.0, Y: +0.0},
    42  			{X: -0.9, Y: -0.9},
    43  			{X: +0.6, Y: +0.0},
    44  			{X: +0.5, Y: -0.9},
    45  		},
    46  		Labels: []string{
    47  			`$\frac{\sqrt{x}}{2\pi\Gamma\gamma}$`,
    48  			`$LaTeX$`,
    49  			"plain",
    50  			`$\frac{\sqrt{x}}{2\beta}$`,
    51  		},
    52  	})
    53  	if err != nil {
    54  		log.Fatalf("could not create labels: %+v", err)
    55  	}
    56  	labels.TextStyle[0].Font.Size = 24
    57  	labels.TextStyle[0].Color = color.RGBA{B: 255, A: 255}
    58  	labels.TextStyle[0].XAlign = draw.XCenter
    59  	labels.TextStyle[0].YAlign = draw.YCenter
    60  
    61  	labels.TextStyle[1].Font.Size = 24
    62  	labels.TextStyle[1].Color = color.RGBA{R: 255, A: 255}
    63  	labels.TextStyle[1].Rotation = math.Pi / 4
    64  
    65  	labels.TextStyle[2].Font.Size = 24
    66  	labels.TextStyle[2].Rotation = math.Pi / 4
    67  	labels.TextStyle[2].YAlign = draw.YCenter
    68  	labels.TextStyle[2].Handler = &text.Plain{Fonts: fonts}
    69  
    70  	labels.TextStyle[3].Font.Size = 24
    71  	labels.TextStyle[3].Rotation = math.Pi / 2
    72  
    73  	p.Add(labels)
    74  	p.Add(plotter.NewGlyphBoxes())
    75  	p.Add(plotter.NewGrid())
    76  
    77  	err = p.Save(10*vg.Centimeter, 5*vg.Centimeter, "testdata/latex.png")
    78  	if err != nil {
    79  		log.Fatalf("could not save plot: %+v", err)
    80  	}
    81  }
    82  
    83  func TestLatex(t *testing.T) {
    84  	cmpimg.CheckPlot(ExampleLatex, t, "latex.png")
    85  }
    86  

View as plain text