Skip to content

How to add percent sign to a label? #803

Open

Description

@jokaorgua

Hello. I'm trying to add a text to the plot with the following code

fonts := font.NewCache(liberation.Collection())
plot.DefaultTextHandler = text.Latex{
	Fonts: fonts,
}

labels, err := plotter.NewLabels(plotter.XYLabels{
	XYs: []plotter.XY{
		{X: 0, Y: 0},
	},
	Labels: []string{
		"Money % spent today: 10",
	},
})
if err != nil {
	log.Fatalf("could not create labels: %+v", err)
}

labels.TextStyle[0].Font.Size = 24
labels.TextStyle[0].Color = color.RGBA{B: 255, A: 255}
labels.TextStyle[0].XAlign = draw.XCenter
labels.TextStyle[0].YAlign = draw.YCenter

p.Add(labels)

code was taken from the example https://github.com/gonum/plot/blob/master/text/latex_example_test.go

But I get nothing on the plot except Money. Like % sign cuts everything else.

Could anyone give a hint on how to add a % sign to a string?

Activity

  1. kortschak commented on Aug 16, 2025

    @kortschak
    Member

    This is a LaTeX issue. % is the LaTeX comment mark. Try \\%.

  2. jokaorgua commented on Aug 16, 2025

    @jokaorgua
    Author

    thanks for the quick response. tried \%, %%, \u0025 - does not work.

    maybe there is another way to add a multi-line text box to a plot?

  3. kortschak commented on Aug 16, 2025

    @kortschak
    Member

    Did you try \\%?

  4. jokaorgua commented on Aug 16, 2025

    @jokaorgua
    Author

    yes, github escaped :) tried \\% %% \u0025

  5. kortschak commented on Aug 16, 2025

    @kortschak
    Member

    I just had a look and it seems the length of the text is the issue. @sbinet will likely have a better understanding of what is going on here.

  6. kortschak commented on Aug 16, 2025

    @kortschak
    Member

    With the following change, the plot renders no text. One less, and text is present.

    diff --git a/text/latex_example_test.go b/text/latex_example_test.go
    index 198086e..b88b4cb 100644
    --- a/text/latex_example_test.go
    +++ b/text/latex_example_test.go
    @@ -47,7 +47,7 @@ func ExampleLatex() {
                    Labels: []string{
                            `$\frac{\sqrt{x}}{2\pi\Gamma\gamma}$`,
                            `$LaTeX$`,
    -                       "plain",
    +                       "plaintext, no",
                            `$\frac{\sqrt{x}}{2\beta}$`,
                    },
            })
    Image
  7. sbinet commented on Aug 16, 2025

    @sbinet
    Member
  8. sbinet commented on Sep 2, 2025

    @sbinet
    Member

    apologies for the belated answer.

    with the following:

    diff --git a/text/latex_example_test.go b/text/latex_example_test.go
    index 198086e..b0a02f0 100644
    --- a/text/latex_example_test.go
    +++ b/text/latex_example_test.go
    @@ -46,8 +46,8 @@ func ExampleLatex() {
                    },
                    Labels: []string{
                            `$\frac{\sqrt{x}}{2\pi\Gamma\gamma}$`,
    -                       `$LaTeX$`,
    -                       "plain",
    +                       `$LaTeX \%$`,
    +                       `plain %`,
                            `$\frac{\sqrt{x}}{2\beta}$`,
                    },
            })

    (ie: use backticks and \% in math-mode.)

    I got:

    Image
  9. kortschak commented on Sep 2, 2025

    @kortschak
    Member

    That explains the percent sign, but the longer text still causes all the text to be lost.

  10. sbinet commented on Sep 2, 2025

    @sbinet
    Member

    this is probably unrelated to any LaTeX issue:

    diff --git a/text/latex_example_test.go b/text/latex_example_test.go
    index 198086e..156fc4c 100644
    --- a/text/latex_example_test.go
    +++ b/text/latex_example_test.go
    @@ -23,14 +23,20 @@ import (
     
     func ExampleLatex() {
            fonts := font.NewCache(liberation.Collection())
    -       plot.DefaultTextHandler = text.Latex{
    +       plot.DefaultTextHandler = text.Plain{
                    Fonts: fonts,
            }
     
            p := plot.New()
    -       p.Title.Text = `$f(x) = \sqrt{\alpha x \Gamma}$`
    -       p.X.Label.Text = `$\frac{\sqrt{x}}{2\pi\Gamma\gamma}$`
    -       p.Y.Label.Text = `$\beta$`
    +       p.Title.Text = `title`
    +       p.X.Label.Text = `x`
    +       p.Y.Label.Text = `y`
     
            p.X.Min = -1
            p.X.Max = +1
    @@ -45,10 +51,14 @@ func ExampleLatex() {
                            {X: +0.5, Y: -0.9},
                    },
                    Labels: []string{
    -                       `$\frac{\sqrt{x}}{2\pi\Gamma\gamma}$`,
    -                       `$LaTeX$`,
    -                       "plain",
    -                       `$\frac{\sqrt{x}}{2\beta}$`,
    +                       "1",
    +                       "2",
    +                       "3333333333",
    +                       "4",
                    },
            })
            if err != nil {
    Image

    one more 3 and one gets:

    Image

    I'd say this comes from a too small Y-axis ?
    adding a "5" at {X: +0.5, Y: +100} (as the last label), one gets:

    Image

    if one adds it as the first label, one gets:

    Image

    to sum up, I suspect there are possibly 2 bugs:

    • one in plotter.NewLabels (b/c of the order of the XYLabels matters)
    • one around plot.Axis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions