Skip to content

[Bug]: Unkown symbol in $..$ when text.usetex: False #22537

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

Closed
Nikongen opened this issue Feb 22, 2022 · 6 comments
Closed

[Bug]: Unkown symbol in $..$ when text.usetex: False #22537

Nikongen opened this issue Feb 22, 2022 · 6 comments

Comments

@Nikongen
Copy link

Bug summary

Placing non-standard tex commands like \unit{mm} from siunitx package on $...$ when used for labels (and probably other text elements) does not work when tex rendering by matplotlib is disabled.
Does it still check stuff in $ signs for valid commands?

Code for reproduction

import locale
import matplotlib
import matplotlib.pyplot as plt

matplotlib.use('TkAgg')
plt.style.use('matplotlib_style.mplstyle')  
# text.usetex: False 
# text.latex.preamble:  r'\usepackage{amsmath}\usepackage{siunitx}'
locale.setlocale(locale.LC_ALL, '')

fig, ax = plt.subplots()
ax.plot([0], [0], 'x')

ax.set_xlabel(r'$\Delta x$ \unit{mm}')  # <- does work
ax.set_xlabel(r'$\Delta x \unit{mm}$')  # <- does not work 
plt.savefig('fig.svg')  # plt.show() does not work either

Actual outcome

Traceback (most recent call last):
File "/venv/lib/python3.10/site-packages/matplotlib/_mathtext.py", line 2237, in parse
result = self._expression.parseString(s)
File "/venv/lib/python3.10/site-packages/pyparsing/core.py", line 1134, in parse_string
raise exc.with_traceback(None)
pyparsing.exceptions.ParseFatalException: Unknown symbol: \unit, found '' (at char 0), (line:1, col:1)

...

ValueError:
\unit{mm}
^
Unknown symbol: \unit, found '' (at char 0), (line:1, col:1)

Expected outcome

Label with plain text (not Tex rendered yet) text in .svg file
(rendering those later with latex svg package)

Additional information

Seems to happen when non standard commands are used in math mode (like $\unit{mm}$)
Both $\unit{mm}$ and \unit{mm} work with siunitx package and xetex

Operating system

PopOS 21.10

Matplotlib Version

3.5.1

Matplotlib Backend

TkAgg

Python version

3.10.0 | Make "In console: python --version or python3 --version" in bug report form

Jupyter version

No response

Installation

pip

@anntzer
Copy link
Contributor

anntzer commented Feb 22, 2022

matplotlib's own tex parser cannot support every tex package in existence and I think we should draw a fairly strict line to keep things manageable (e.g., not going beyond standard latex commands + amsmath). Certainly siunitx seems out of scope (even though it is a package I like very much).
(Perhaps we could consider some kind of \newcommand support though :p)

@Nikongen
Copy link
Author

Nikongen commented Feb 23, 2022

As I said I don’t want it to parse the tex commands. It’s supposed to handle the text in the label as plain/raw text.
I totally agree to your argument to support just basic commands.

What I wanted to point out is that it seems to parse/check the text when $ signs appear, although the rcParams disable tex parsing

Edit: accidentally clicked the wrong button 🤦🏽‍♂️

@anntzer
Copy link
Contributor

anntzer commented Feb 23, 2022

Ah, for that you can use https://matplotlib.org/stable/api/text_api.html#matplotlib.text.Text.set_parse_math.

@Nikongen
Copy link
Author

Nikongen commented Feb 23, 2022

Ah, for that you can use https://matplotlib.org/stable/api/text_api.html#matplotlib.text.Text.set_parse_math.

Thanks for that #:+1:
But is it possible to set this global with the matplotlibrc-file?
Can't find something similiar in https://matplotlib.org/stable/tutorials/introductory/customizing.html#the-matplotlibrc-file

@oscargus
Copy link
Member

It is currently not possible, but you can, which you may have realized, pass parse_math=False to the xlabel command. Which at least is a bit easier than calling set_parse_math on the created label.

I guess it can be possible to add it to the rc-file though.

@oscargus
Copy link
Member

I created a PR to support parse_math as an rcParams, #22556

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants