# Matplotlib with pgf backend, problem with underscore

Gold Member
I use matplotlib to create files.pgf that I can \include{} in my latex documents. All works nicely, except when I want to make a reference to some equation of my latex document. For example, I when plot several curves on a graph and label them as "Coming from expression ..." where ... is \ref{eq:some_equation}, then I get an error and no file.pgf is produced. If, however, the \label{} of the equation contains no underscore, then there is no problem, the file.pgf is created and can be correctly imported into the latex document.

How can I make matplotlib able to create the file.pgf without removing the underscores? Because then I also have to remove it in all my latex files referencing to that particular equation.

I already tried \_ instead of _, to no avail.
Here might be a MWE (untested):
Python:
import matplotlib as mpl
mpl.use('pgf')
import matplotlib.pyplot as plt
x, y = [1], [2]
plt.plot(x, y, label='coming from expr.\\ref{eq:heat_eq}')
plt.savefig('file.pgf')
plt.close()

This MWE should return an error, unless the underscore in heat_eq is removed. Ideally, I want to make matplotlib understand that it can deal with the underscore.

## Answers and Replies

Gold Member
From matplotlib:

https://matplotlib.org/tutorials/text/mathtext.html

The underscore is a special character in latex to indicate subscripts.

Some folks have suggested using \ to escape the underscore as in \_
As I wrote in my post, I already tried that suggestion. I also tried to put an r before the string containing this problematic part, to no avail.

Mentor
Did you do both? The r means the string is a raw unprocessed string so the \ will not be removed.

In C code, we would have to routinely use \\ because processing would remove one leaving \ so that's another thing you could try without the r.

Code:
Sometimes meta chars can be mega headaches. In one case, I had to
use \\\\ just to get one \ because of double processing of the string
ie \\\\ to \\ to \

(and for PF use [ code ] tags because \ was processed here too
without them.

Gold Member
Yeah, I tried every combinations possible:
_\
_

And the same thing with an r in front of the 'problematic_part'. Like that: label=r'coming from expr.\\ref{eq:heat_eq}' and label=r'coming from expr.\\ref{eq:heat\_eq}'

Nothing worked. What I eventually did was to remove the underscore and do the same in my whole .tex files (about 10 occurrences). That worked, of course, but this is not a good way to deal with the problem since it is bypassing the problem rather than tackling it.

Homework Helper
2022 Award
The alternative is to give your figure a caption which tells the reader what expression is being plotted (either instead of, or in addition to, including a legend in the figure which doesn't refer to expressions in the particular document - that way you can re-use the figure elsewhere.)

Code:
\begin{figure}
\begin{graphics}
\end{graphics}
\caprion{Plot of expression \ref{...} (solid lines) and ...}
\end{figure}

This is my usual practice, since I generally produce PNG output from matplotlib.

fluidistic
Gold Member
The alternative is to give your figure a caption which tells the reader what expression is being plotted (either instead of, or in addition to, including a legend in the figure which doesn't refer to expressions in the particular document - that way you can re-use the figure elsewhere.)

Code:
\begin{figure}
\begin{graphics}
\end{graphics}
\caprion{Plot of expression \ref{...} (solid lines) and ...}
\end{figure}

This is my usual practice, since I generally produce PNG output from matplotlib.
I see, yeah that could also be another solution to bypass the problem, although in that case the end result is not what I had in mind.

Interesting, but I do not really understand your last sentence. Why would it matter that you generally produce png output rather than say, pgf or pdf?

Homework Helper
2022 Award
Interesting, but I do not really understand your last sentence. Why would it matter that you generally produce png output rather than say, pgf or pdf?

LaTeX won't find a \ref command in a compressed list of "this pixel is this color" data.

fluidistic