LaTeX code for titles, labels,... with Plots.jl

@mkborregaard The correct capitalization of the package name is LaTeXStrings.

3 Likes

Indeed it is, thanks for the dilligence.

Note: you don’t need the LaTeXStrings package in order to render LaTeX in Plots.jl - it just makes doing so more convenient. You can use LaTeX directly in Plots.jl by manually escaping all the $s, \s, etc.

2 Likes

Hello, how would you use LaTeX in Plots without the LaTeXStrings package ? label="${\bar{fitted}} prices$" or label=L"{\bar{fitted}} prices" would not work…

EDIT: Sorry… got it: label="\${\\overline{fitted}} \\thinspace prices\$")

Sorry for the reviving this thread again but I’m struggling to get this right:

using Plots, LaTeXStrings
plot(rand(100))
title!("This is an alpha: α")                   # produces ? for alpha
title!("This is an alpha: \$\\alpha\$")         # produces $ α$
title!(L"This is an alpha: α")                  # produces nothing
title!(L"This is an alpha: \alpha")             # produces nothing
title!(L"This is an alpha: $\alpha$")           # produces $ α$

I can’t seem to find a combination that plots the Latex symbol without also plotting the dollar signs - any help appreciated.

4 Likes

In my (small) experience, it depends on the backend. If you’re using the default for Plots.jl (GR.jl), LaTeX doesn’t work very well. It only works if you’re using a string that is all LaTeX, e.g.:

title!("\$\\alpha\$")

Otherwise you should currently use a different backend. For me, PyPlot.jl usually does its job with LaTeX.

1 Like

…agree wrt. PyPlot, but on my computer (Win10), PyPlot doesn’t work in Juno – for some reason.

Hi Nils,

I found that for the title and gr() backend one has to use the latexstring() function directly instead of L"some latex code".

Below an example code

using Plots, LaTeXStrings

gr()

date = "07-08-2018"
titlestring = latexstring("\\mathrm{My\\, \\alpha\\, including\\, a\\, variable\\, date:\\,}", date)

plot(sin, (0:0.01:5*pi), dpi=300,
     title = titlestring,
     xlabel = L"\mathrm{My\, x-label\, (mJ/cm^{3})}",
     ylabel = L"\mathrm{\alpha (m/s)}")

Rob.

6 Likes

I’m having this issue too, and I’m getting extra dollar signs in all other backends : not sure if this is a juno issue, a backend issue or a plots issue…

I think the PyPlot backend is the only one that currently supports only part of the string being LaTeX, e.g. things like “foo \$\\infty\$”. In GR you get text with dollar signs.
As a workaround you can do e.g. “\$\\mathrm{foo}\,\\infty\$”.

2 Likes

Hi, I still have this problem. I just want to set my y label as “Unit is 10^3”, where the 10^3 should be latex symbol.

I’m using Juno with latest version of Julia, and none of above suggestions works.

Which backend are you using for Plots?
If you haven’t selected it then you’re using the default one (GR). In my experience, the solution given in the answer above your post should work, then… something like "\$\\mathrm{Unit is} 10^3\$"

Maybe you could try PGFPlots?

using Plots; pgfplots()
using LaTeXStrings

An example:
plot(..., ..., label=L"...", yaxis=:log, lw=1, color=1, line=:dot, xlabel=L"k", ylabel=L"\|r_k\|", legend=:best)

Hello Guys,

I am in Juno and under Linux. I could not get aplha on my PyPlot graphs, and the following worked for me after some trial runs, e.g.:

using Plots, LaTeXStrings
plot!(xdata, ydata, label = "\\alpha")

Hope this will help somebody…

2 Likes

Btw, i realized that if you want to combine greek and other characters with a text, it gets a bit trickier…
I could solve it through this hack (funny part, no using LaTeXStrings is needed), e.g.:

ylabel!("Permeability in z-direction"*"\\mu"*"m")

Works for titles, data and axes labels.

5 Likes

Can I use \frac? in a title?

using Plots
using LaTeXStrings

f(x)=1/sqrt(x+x^2)
plot(f,title=L "f(x) = \frac {1}{x+x^{2}}"

You can, but you need to make sure you construct the Latex string correctly - in the above you have a space between the L and the start of the string " which won’t work, you need L"f(x)..."

1 Like
using Plots
using LaTeXStrings
plot(0:2,0:2, xlab = L"x^2", xticks = ([0,1,2],[L"0", L"1", L"2"]))
'latex' is not recognized as an internal or external command,
operable program or batch file.
latex: failed to create a dvi file
'latex' is not recognized as an internal or external command,
operable program or batch file.
latex: failed to create a dvi file
'latex' is not recognized as an internal or external command,
operable program or batch file.
latex: failed to create a dvi file
'latex' is not recognized as an internal or external command,
operable program or batch file.
latex: failed to create a dvi file
'latex' is not recognized as an internal or external command,
operable program or batch file.
latex: failed to create a dvi file
'latex' is not recognized as an internal or external command,
operable program or batch file.
latex: failed to create a dvi file
'latex' is not recognized as an internal or external command,
operable program or batch file.
latex: failed to create a dvi file
'latex' is not recognized as an internal or external command,
operable program or batch file.
latex: failed to create a dvi file
'latex' is not recognized as an internal or external command,
operable program or batch file.
latex: failed to create a dvi file
'latex' is not recognized as an internal or external command,
operable program or batch file.
latex: failed to create a dvi file
'latex' is not recognized as an internal or external command,
operable program or batch file.
latex: failed to create a dvi file
'latex' is not recognized as an internal or external command,
operable program or batch file.
latex: failed to create a dvi file
'latex' is not recognized as an internal or external command,
operable program or batch file.
latex: failed to create a dvi file
'latex' is not recognized as an internal or external command,
operable program or batch file.
latex: failed to create a dvi file
'latex' is not recognized as an internal or external command,
operable program or batch file.
latex: failed to create a dvi file
'latex' is not recognized as an internal or external command,
operable program or batch file.
latex: failed to create a dvi file

Julia>versioninfo()
Julia Version 1.5.3
Commit 788b2c77c1 (2020-11-09 13:37 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i3-3240 CPU @ 3.40GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-9.0.1 (ORCJIT, ivybridge)

Just a quick note of what works for me:

plot(xlabel="axis label \$\\alpha^3_{AB}\$")

I’m using PyPlot in Jupyter Notebook on Windows, and Julia Version 1.4.1.
I’ve tried a lot of combinations of Latexstrings, double backslashes, dollar signs, etc., and only found this by accident. In the end, the syntax that seems to work is just putting one additional backslash in front of most latex commands (including the math mode dollar signs, but not subscripts and superscripts for example).

You are right, they change at times. I had to find the combination that worked for me, but months later then i run the same script - the results were different…
This is a bit disappointing indeed, i hope plotting packages will get stable and will produce consistent results in the nearest future.

1 Like