Matplotlib with pgf backend, problem with underscore

  • Thread starter fluidistic
  • Start date
  • #1
fluidistic
Gold Member
3,678
112
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

  • #3
fluidistic
Gold Member
3,678
112
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.
 
  • #4
12,093
5,774
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 thats 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.
 
  • #5
fluidistic
Gold Member
3,678
112
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.
 
  • #6
pasmith
Homework Helper
1,857
513
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
  • #7
fluidistic
Gold Member
3,678
112
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?
 
  • #8
pasmith
Homework Helper
1,857
513
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

Related Threads on Matplotlib with pgf backend, problem with underscore

Replies
1
Views
7K
Replies
4
Views
7K
Top