1

I'm trying to get mathtext to render my figure axes labels correctly (ideally I don't want to use Latex itself to render the text). However, this small minimum working example produces the error shown below. It seems to me that it should work, is maybe a necessary package not installed?

I'm running matplotlib 2.2.2 and Python 3.5

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(0,0)
ax.set_ylabel(r'$\mathrm A$')

the error produced:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/mathtext.py", line 2516, in parse
    result = self._expression.parseString(s)
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1632, in parseString
    raise exc
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1622, in parseString
    loc, tokens = self._parse( instring, 0 )
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1529, in _parseCache
    value = self._parseNoCache(instring, loc, doActions, callPreParse)
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1379, in _parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 3717, in parseImpl
    return self.expr._parse( instring, loc, doActions, callPreParse=False )
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1529, in _parseCache
    value = self._parseNoCache(instring, loc, doActions, callPreParse)
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1379, in _parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 3395, in parseImpl
    loc, exprtokens = e._parse( instring, loc, doActions )
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1529, in _parseCache
    value = self._parseNoCache(instring, loc, doActions, callPreParse)
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1379, in _parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 3919, in parseImpl
    return super(ZeroOrMore, self).parseImpl(instring, loc, doActions)
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 3848, in parseImpl
    loc, tokens = self_expr_parse( instring, loc, doActions, callPreParse=False )
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1529, in _parseCache
    value = self._parseNoCache(instring, loc, doActions, callPreParse)
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1379, in _parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 3378, in parseImpl
    loc, resultlist = self.exprs[0]._parse( instring, loc, doActions, callPreParse=False )
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1529, in _parseCache
    value = self._parseNoCache(instring, loc, doActions, callPreParse)
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1405, in _parseNoCache
    tokens = fn( instring, tokensStart, retTokens )
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1049, in wrapper
    ret = func(*args[limit[0]:])
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/mathtext.py", line 2585, in math_string
    return self._math_expression.parseString(toks[0][1:-1])
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1632, in parseString
    raise exc
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1622, in parseString
    loc, tokens = self._parse( instring, 0 )
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1529, in _parseCache
    value = self._parseNoCache(instring, loc, doActions, callPreParse)
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1379, in _parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 3717, in parseImpl
    return self.expr._parse( instring, loc, doActions, callPreParse=False )
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1529, in _parseCache
    value = self._parseNoCache(instring, loc, doActions, callPreParse)
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1379, in _parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 3848, in parseImpl
    loc, tokens = self_expr_parse( instring, loc, doActions, callPreParse=False )
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1529, in _parseCache
    value = self._parseNoCache(instring, loc, doActions, callPreParse)
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1379, in _parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 3717, in parseImpl
    return self.expr._parse( instring, loc, doActions, callPreParse=False )
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1529, in _parseCache
    value = self._parseNoCache(instring, loc, doActions, callPreParse)
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1379, in _parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 3530, in parseImpl
    ret = e._parse( instring, loc, doActions )
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1529, in _parseCache
    value = self._parseNoCache(instring, loc, doActions, callPreParse)
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1405, in _parseNoCache
    tokens = fn( instring, tokensStart, retTokens )
  File "/usr/local/lib/python3.5/dist-packages/pyparsing.py", line 1049, in wrapper
    ret = func(*args[limit[0]:])
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/mathtext.py", line 2687, in unknown_symbol
    raise ParseFatalException(s, loc, "Unknown symbol: %s" % c)
pyparsing.ParseFatalException: Unknown symbol: \mathrm (at char 0), (line:1, col:1)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/backend_gtk3agg.py", line 39, in on_draw_event
    self._render_figure(w, h)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/backend_gtk3agg.py", line 30, in _render_figure
    backend_agg.FigureCanvasAgg.draw(self)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/backend_agg.py", line 433, in draw
    self.figure.draw(self.renderer)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/figure.py", line 1475, in draw
    renderer, self, artists, self.suppressComposite)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/image.py", line 141, in _draw_list_compositing_images
    a.draw(renderer)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/axes/_base.py", line 2607, in draw
    mimage._draw_list_compositing_images(renderer, self, artists)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/image.py", line 141, in _draw_list_compositing_images
    a.draw(renderer)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/axis.py", line 1204, in draw
    self.label.draw(renderer)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/text.py", line 706, in draw
    bbox, info, descent = textobj._get_layout(renderer)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/text.py", line 309, in _get_layout
    ismath=ismath)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/backend_agg.py", line 237, in get_text_width_height_descent
    self.mathtext_parser.parse(s, self.dpi, prop)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/mathtext.py", line 3294, in parse
    box = self._parser.parse(s, font_output, fontsize, dpi)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/mathtext.py", line 2522, in parse
    six.text_type(err)]))
ValueError: 
\mathrm A
^
Unknown symbol: \mathrm (at char 0), (line:1, col:1)

If you suspect this is an IPython bug, please report it at:
    https://github.com/ipython/ipython/issues
or send an email to the mailing list at [email protected]

You can print a more detailed traceback right now with "%tb", or use "%debug"
to interactively debug it.

Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
    %config Application.verbose_crash=True
4
  • Do you mean \mathrm{A}? Commented Apr 12, 2018 at 10:06
  • I can't believe that that was the error... I feel a bit stupid :) You should make that as an answer so that I can accept it Commented Apr 12, 2018 at 10:22
  • Nah, this is close to a typo and the question should be closed for that very same reason. Commented Apr 12, 2018 at 10:24
  • 1
    Yes and no. In Latex itself you don't necessarily need the curly brakets. It would work without them Commented Apr 12, 2018 at 10:33

1 Answer 1

1

You are correct that in LaTeX, \mathrm A is valid, but don't forget that the Matplotlib parser is not the same as LaTeX.

I tried this on Python 3.6.4 and Matplotlib 2.2.2, and the error no longer exists.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.