-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Feature request: add option to disable mathtext parsing #4938
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I am amendable to this idea. It probably won't get worked on for a couple On Wed, Sep 23, 2015 at 5:09 AM, Sybren A. Stüvel notifications@github.com
|
I deleted my post (now quoted above), as it turned out it didn't work as easily as I expected. I got the raw "$\LaTeX$" in the output SVG, but it wasn't picked up by Inkscape to be removed from the SVG and transferred to a .pdf_tex file. Apparently, more has to be done to support my suggested workflow. |
@sybrenstuvel If you are solely targetting latex rendering, have a look at the pgf backend. |
Have you tried escaping the dollar signs? i.e. |
Yes. I would like to be able to disable it, too. It is particularly annoying if you use third party libraries for export, i.e. matplotlib2tikz. |
This would be a nice feature, currently I have to enable and disable all the escape symbols depending on what I use it for. Having a
The latter requires: plt.rcParams['svg.fonttype'] = 'none' and saving the images as an |
As noted above we have a pgf backend already, so what is the advantage of having an extra processing step? |
This link discusses how to import a drawing and or plot into your latex document in a way that:
Separating the text from the image can be done using Inkscape. This is, at least in my opinion better than the pgf backend as it ensures that the fontsize/type and other text properties in your image is exactly the same as in the rest of you document. Even if you rescale the image the font is rendered by latex in the same way as the rest of the document and does thus not scale with the image. The result is a more consistent document. See the following example: It is not only more consistent, it is also easier to set up than the pgf backend, as it does not require matplotlib to do anything more than exporting the text as a text in the .svg. The text is then handled by your normal latex editor and compiler (not matplotlib or python). This is especially nice if you want your code or images to be portable. To be clear this behaviour works perfect already with the current matplotlib module, you just need to ensure that matplotlib does not interpret the math you write, as mentioned above this can be achieved with escape symbols. Here an example of a string: >>> ax.set_ylabel(r"Local error $\sum_{d=1}^D \| u_d - \hat{u} \|^2$")
>>> ax.set_ylabel(r"Local error \$\\sum_{d=1}^D \\| u_d - \\hat{u} \\|^2\$") The only problem that I have at the moment is that I would like to switch between these two printing modes (math represented as math and math represented as latex commands). This makes it easier to work with, as I can look at the nicely rendered functions when developing code. Hence, I would propose a command that turns-off the interpretation of the strings such that all labels, annotations and others are rendered as plain text. I, and so I assume the original author, expected this to either exist already or for this to be an easy to implement. Again, the only thing that this setting should do is turn-off all string interpretation and just make it appear as plain text in the matplotlib window. Everything else will be handled by Inkscape and Latex. |
Thank you for the detailed explanation.
def _m(s): return s.replace("$", r"\$") if RAW_MATH else s and wrap all your strings in |
Just for clarification I made an example: import matplotlib.pyplot as plt
plt.rcParams['svg.fonttype'] = 'none'
plt.figure()
plt.xlabel(r"Just some text")
plt.ylabel(r"Local error \$\sum_{d=1}^D \| u_d - \hat{u} \|^2\$")
plt.savefig('plot.svg') When you open this in Inkscape (or Illustrator), will cause your font to be recognized in Inkscape as letters. \documentclass{article}
\usepackage{hyperref} % Import and set hyperrefs
\usepackage{geometry} % Tweakying page layout
\usepackage{graphicx} % Manage images and colors
% Reading and processing SVG files with correct fonts
% This can be replaced with using Inkscape and manually
% exporting the files as .pdf with text as latex. This
% automated process requires Inkscape to be in your
% global path, wich is a bit more difficult in windows.
\newcommand{\executeiffilenewer}[3]{%
\ifnum\pdfstrcmp{\pdffilemoddate{#1}}%
{\pdffilemoddate{#2}}>0%
{\immediate\write18{#3}}\fi%
}
\newcommand{\includesvg}[1]{%
\executeiffilenewer{#1.svg}{#1.pdf}%
{inkscape -z -D --file=#1.svg --export-pdf=#1.pdf --export-latex}%
\input{#1.pdf_tex}%
}
\begin{document}
\begin{figure}[t!]
\centering
\def\svgwidth{\textwidth}
\includesvg{plot}
\caption{An example plot with real latex rendering.}
\end{figure}
\end{document} This is the resulting folder including the python, latex and image files. |
Ahh, thanks for your swift response. I'll have a look at it tomorrow, as it is late already. Your proposal where I use a function to convert is actually perfect and this solves all my issues. I'll make an extra example tomorrow to show how this can be use to make a python script that can quickly switch between rendered and non-rendered maths in matplotlib. Afterwards I'll close this request. You are right, only the |
I still don't follow how that is preferable to \documentclass{article}
\usepackage{hyperref} % Import and set hyperrefs
\usepackage{geometry} % Tweakying page layout
\usepackage{graphicx} % Manage images and colors
\usepackage{tikz,pgf}
\begin{document}
\begin{figure}
\begin{center}
\input{plot.pgf}
\end{center}
\caption{An example plot with real latex rendering.}
\end{figure}
\end{document} which bypasses inkscape altogether: |
I must be honest, I thought that the If I manage to fix than the |
I've not yet managed to have the font type and size adapt to my latex run automatically with the import matplotlib.pyplot as plt
from tikzplotlib import save
plt.figure()
plt.xlabel(r"Just some text")
plt.ylabel(r"Local error $\sum_{d=1}^D \| u_d - \hat{u} \|^2$")
save("plot.tex",
axis_height=r"\axheight",
axis_width=r"\axwidth") \documentclass{article}
\usepackage{geometry}
\usepackage{pgfplots}
\pgfplotsset{compat = 1.5}
\newlength\axheight
\newlength\axwidth
\newcommand{\plot}[3]{%
\setlength\axheigaht{#1}%
\setlength\axwidth{#2}%
\input{#3}}
\begin{document}
\begin{figure}
\centering
\plot{5cm}{5cm}{plot.tex}
\caption{An example plot with real latex rendering.}
\end{figure}
\begin{figure}
\centering\footnotesize
\plot{5cm}{5cm}{plot.tex}
\caption{Image of the same size but with different text size.}
\end{figure}
\begin{figure}
\centering
\plot{6cm}{0.9\textwidth}{plot.tex}
\caption{The same image but with different dimensions, The text does not scale with the image.}
\end{figure}
\end{document} See the results here: test.zip. |
This is turning into a great tutorial on pgf and tikzplot etc! But to clarify my particular original problem, I was missing an easy way to display raw mathtext during the development of a plot without escaping everything or raising an error, and then later on switching to usetex with full latex support and perhaps custom \usepackages. For example, a label like:
|
Clearly I had a wrong idea regarding your motivation, sorry for that. I think that @anntzer in #4938 (comment) has the solution for you. Here is a minimal working example with your string: import matplotlib.pyplot as plt
RAW_MATH = True
def _m(s):
return s.replace("$", r"\$") if RAW_MATH else s
plt.figure()
plt.xlabel(_m("Just some text"))
plt.ylabel(_m("Local error $\sum_{d=1}^D \| u_d - \hat{u} \|^2$"))
plt.plot([1,2,3],label=_m('$x(\ce{CH4}) = \SI{1e16}{cm^{-3}}$'))
plt.legend()
plt.show() |
From what I understand, the question was answered and a workaround found. Can it be closed? On a separate note, would it be interesting to open another discussion about workflows for matplotlib and latex. Not with the intention to make changes in matplotlib, but with the following goals:
This could inform users and help them to improve their reports/publications. Where should such a discussion take place? |
https://discourse.matplotlib.org is a great place to discuss this. I think we will close this, because if I understand correctly, toggling this will lead to bad outcomes with the size of labels being mis-interpreted. So enabling this as a feature will leave us open to a lot of complaints if folks don't understand the drawbacks. The workaround above for those who really need to toggle this seems OK to me. But of course anyone can request a re-open, or make a PR that implements this anyways |
The workaround is really inconvenient for plotting from third party libs like pandas. I read from databases into dataframes and the column names include liberal use of characters in database fields that get interpreted as mathtex (e.g. some of these column names include multiple $ signs) and I just want clean text output of those columns names in my chart legends. To do that on my side, I'm going to have to wrap all my DB API calls with string escaping. An rcParam would be far simpler. |
@spillz Please open a new issue. |
Could not find a way to do this through current documentation discussion.
Requested feature: Simple way (rcParam?) to disable mathtext processing and instead display raw string.
An alternative might be to print raw string on error rather than fail parsing and entire script.
Use case: Normally process figure strings with latex strings. When prototyping use mathtext for speed, but fails in complex but useful latex cases (e.g., \ce{..} mhchem package). Would be annoying to add extensive try/except code.
The text was updated successfully, but these errors were encountered: