Matplotlib with pgf backend, problem with underscore

  • Thread starter Thread starter fluidistic
  • Start date Start date
  • Tags Tags
    Matplotlib
Click For Summary

Discussion Overview

The discussion revolves around the challenges of using Matplotlib with the PGF backend in LaTeX documents, specifically regarding the handling of underscores in labels when referencing equations. Participants explore potential solutions and workarounds for this issue, which affects the generation of PGF files.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes an issue where using underscores in LaTeX references causes errors in PGF file generation with Matplotlib.
  • Another participant notes that underscores are special characters in LaTeX, typically used for subscripts, and suggests escaping them with a backslash.
  • A participant mentions that they have already attempted escaping underscores and using raw strings, but these methods did not resolve the issue.
  • Another reply suggests trying double backslashes to account for string processing in Python, indicating that escaping can be complex.
  • One participant shares their workaround of removing underscores from both the Matplotlib labels and the LaTeX files, expressing dissatisfaction with this solution.
  • An alternative approach is proposed, suggesting to use figure captions instead of legends for referencing expressions, which could allow for reusability of figures.
  • A later reply questions the relevance of producing PNG output versus PGF or PDF, noting that LaTeX cannot process references in pixel-based formats.

Areas of Agreement / Disagreement

Participants express various viewpoints on how to handle the underscore issue, with no consensus reached on a definitive solution. Some propose workarounds while others share their frustrations with the limitations of the current methods.

Contextual Notes

Participants mention limitations related to string processing in Python and the specific requirements of LaTeX for handling special characters, which may affect the proposed solutions.

fluidistic
Gold Member
Messages
3,934
Reaction score
286
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   Reactions: 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   Reactions: 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
6K
Replies
4
Views
5K
  • · Replies 15 ·
Replies
15
Views
2K