Matplotlib with pgf backend, problem with underscore

  • Thread starter Thread starter fluidistic
  • Start date Start date
  • Tags Tags
    Matplotlib
Click For Summary
Using Matplotlib with the pgf backend can lead to errors when referencing LaTeX equations that contain underscores, as underscores are treated as special characters in LaTeX for subscripting. Users have attempted various methods to escape underscores, such as using backslashes or raw string notation, but these solutions have not resolved the issue. The only workaround that has successfully produced the .pgf file involves removing underscores from both the labels in Matplotlib and the corresponding references in LaTeX documents, which is not ideal. An alternative suggestion is to use figure captions to describe the expressions being plotted, allowing for reuse of figures without direct references to specific equations. Ultimately, the challenge remains in finding a way for Matplotlib to handle underscores in labels without compromising the integrity of the LaTeX references.
fluidistic
Gold Member
Messages
3,931
Reaction score
281
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.
 
Physics news on Phys.org
jedishrfu said:
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.
 
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.
 
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.
 
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.
 
  • Like
Likes fluidistic
pasmith said:
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?
 
fluidistic said:
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.
 
  • Like
Likes fluidistic

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 14 ·
Replies
14
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 21 ·
Replies
21
Views
5K
Replies
4
Views
4K
  • · Replies 15 ·
Replies
15
Views
2K