Unexpected spacing in LaTeX plot label

I’d like to label a line on a plot using LaTeX to write a norm, e.g.

using Plots
using LaTeXStrings
plot([1,2,3], label=L"||x-y||^2")

I see
Bildschirmfoto 2021-06-29 um 11.37.30 Kopie

but the text inside the norm appears shifted to the right and the spacing between the double bars is greater than expected. I was hoping the label text would look more like
x-y^2

Is there a way to get my desired appearance?

(Julia official release 1.6.1 (2021-04-23) on OS X)

I think this is a bug in GR’s fonts rendering

Yeah, there’s a couple of those. It also messes up \mathrm{x}^2.

GR doesn’t actually run LaTeX, but has its own built-in approximation. Doesn’t always work.

Workaround: use unicode:

plot([3,2,1], label="‖x-y‖²")
2 Likes

It works fine with Plots.jl’s pgfplotsx() backend:

using LaTeXStrings, Plots; pgfplotsx()

plot([1,2,3], label=L"||x-y||^2")

2 Likes

That does look nicer - thanks! At the end of the day this will allow me to generate my plots, but it’s a bit inconvenient because my other plots unfortunately don’t work with pgfplotsx() (and they’re slow, so I want to use GR for its promised speed). Simply changing the backend for some of the plots doesn’t work in my code as written, because of this issue where you can’t change plot backends within a function. In any case, I’ll mark this as the solution since the original problem is a known issue with the GR backend.

1 Like

You should be able to hack it with \, or \;

2 Likes