Typing special characters in Plot legends and titles

I still didn’t find a practical way to type special characters in to a plot legend. In particular, I am trying to type an Angstrom (Å) character in an axis label.

The situation is the following:

In the REPL I can use the unicode substitution and type \AA or \Angstrom, which is replaced by the symbol, and appears correctly in the GR plot, and I can save the figure nicely to PNG or PDF.

This solution appeared to be fine, except that I cannot find a way to do the same thing in a script. The code:

using Plots
plot(xlabel="\\AA")
savefig("test.png")

returns \AA: undefined symbol

If that option worked I think it would be the optimal solution.

I read some threads and it was suggested to use LaTeXStrings. However, I cannot get an acceptable result with it either, and this might be because of the poor documentation of the package, or perhaps I am missing something obvious. In particular, I try:

plot(xlabel=L"\AA")

but that gives me the Angstrom sign in LaTeX equation mode (italics), which is not what I need. I tried

plot(xlabel=L"\textrm{\AA}")

this works, but the font of the Angstrom sign is different from the fonts of the other text of the plot. I could not find a solution to obtain the same fonts when using the latex strings package.

In other words, I still cannot find a way to produce a publication quality plot with Plots with special characters. The use of the LaTeX code seems to be the most general solution, but in this case a practical way to adjust the fonts of the plot to the fonts is needed. As a simple alternative workaround for simple characters, it would be nice if the unicode mapping worked with scripts as it works in the REPL.

I appreciate any help.

1 Like

I normally do one of two approaches

  1. Use pyplot as backend and the call matplotlib2tikz
  2. Use pgfplots as backend and save directly as tex

I prefer approach 1 whenever it works as it produces far better and readable tikz code.

Here’s the code I normally use to save a tikz figure using matplotlib2tikz

using PyCall
PyCall.@pyimport matplotlib2tikz
"""
`savetikz(path; fig = PyPlot.gcf(), extra::Vector{String})`
"""
function savetikz(path; fig = PyPlot.gcf(), extra=[])
    if extra == []
        matplotlib2tikz.save(path,fig, figureheight = "\\figureheight", figurewidth = "\\figurewidth")
    else
        matplotlib2tikz.save(path,fig, figureheight = "\\figureheight", figurewidth = "\\figurewidth", extra_tikzpicture_parameters = PyCall.pybuiltin("set")(extra))
    end
end

I am still in search for a good solution for this problem. Here goes a more detailed description:

I am trying to use LaTeXStrings to add special characters to the label of a plot. The following results can be obtained:

histogram!(xlabel=L"Test \alpha")

Result: image

All text is interpreted as an equation, which is not satisfactory.

histogram!(xlabel=L"\textrm{Test }\alpha")

Result: image

Possibly the best alternative, but the fonts are different from the fonts of the rest of the plot.

histogram!(xlabel="Test $(L"\alpha")")

Result:
image

This one looks very promising, because the special character (alpha) is rendered, and the fonts are not changed. However, the dollar signs appear. I could not get rid of them.

I am not sure if this an issue of the package or if there is a solution. I was able to find the following workaround for some characeters:

string="$(L"Test \alpha")"
histogram!(xlabel=string[2:length(string)-1])

Result:
image

(unfortunately this option does not work to produce the Angstrom character (\AA))

Any thoughts? I could not find help anywhere else.

I can’t test it right now, but maybe sprintf the Angstrom symbol as you did in Latex mode and then use the resulting string as the label?

You want histogram!(xlabel=L"Test $\alpha$")

Can anyone shed light on what the root problem is with just displaying Unicode in strings? Do backends not support Unicode or is Plots dropping the ball? Or what?

Thanks, but, at least here, it returns 64275213-7b3a5d80-cf1b-11e9-990f-4b21ef8ffacd

What plots backend are you using?

I suppose it is “GR” (I didn’t set anything manually)

It should work with PyPlot. I don’t think GR really supports LaTeX.

1 Like

Plots is Latin1 by default with the GR backend at least, due in part to limitations of the backend:

Even if a backend supports Unicode strings, its text rendering may not be capable of displaying many characters (due to limited font coverage, lack of support for combining characters, lack of support for fallback fonts for missing glyphs…). See here for a summary:

The solution is for backends to switch to a modern full-featured text renderer. As explained in the issue above, HarfBuzz seems to be a promising cross-platform option.

1 Like

histogram!(xlabel=L"C$\alpha$ distance to surface / $\mathrm{\AA}$")

With pyplot() it worked, I add the need to use \mathrm to obtain an angstrom sign not in italics.

The plot are not as pretty as with GR, but this is a solution.