Skip to content

[Bug]: matplotlib 3.8: new mathtext feature "\text" fails. #26968

Closed

Description

@2sn

Bug summary

It seems for me the most welcomed \text command in mathtext does not seem to work (yet).
At least not out of the box.

My apologies if I am just missing something.

Code for reproduction

import matplotlib.pyplot as plt
plt.text(0.1, 0.3, r"$\text{dashes (-) are retained}$")

Actual outcome

Traceback (most recent call last):
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/backends/backend_gtk4.py", line 277, in idle_draw
    self.draw()
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/backends/backend_agg.py", line 388, in draw
    self.figure.draw(self.renderer)
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/artist.py", line 95, in draw_wrapper
    result = draw(artist, renderer, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/artist.py", line 72, in draw_wrapper
    return draw(artist, renderer)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/figure.py", line 3154, in draw
    mimage._draw_list_compositing_images(
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/image.py", line 132, in _draw_list_compositing_images
    a.draw(renderer)
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/artist.py", line 72, in draw_wrapper
    return draw(artist, renderer)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/axes/_base.py", line 3070, in draw
    mimage._draw_list_compositing_images(
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/image.py", line 132, in _draw_list_compositing_images
    a.draw(renderer)
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/artist.py", line 72, in draw_wrapper
    return draw(artist, renderer)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/text.py", line 748, in draw
    bbox, info, descent = self._get_layout(renderer)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/text.py", line 381, in _get_layout
    w, h, d = _get_text_metrics_with_cache(
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/text.py", line 69, in _get_text_metrics_with_cache
    return _get_text_metrics_with_cache_impl(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/text.py", line 77, in _get_text_metrics_with_cache_impl
    return renderer_ref().get_text_width_height_descent(text, fontprop, ismath)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/backends/backend_agg.py", line 213, in get_text_width_height_descent
    return super().get_text_width_height_descent(s, prop, ismath)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/backend_bases.py", line 652, in get_text_width_height_descent
    return self.get_texmanager().get_text_width_height_descent(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/texmanager.py", line 363, in get_text_width_height_descent
    dvifile = cls.make_dvi(tex, fontsize)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/texmanager.py", line 295, in make_dvi
    cls._run_checked_subprocess(
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/texmanager.py", line 258, in _run_checked_subprocess
    raise RuntimeError(
RuntimeError: latex was not able to process the following string:
b'$\\\\text{dashes (-) are retained}$'

Here is the full command invocation and its output:

latex -interaction=nonstopmode --halt-on-error --output-directory=tmpt1zptv_z 9e73bfe7c799e116dd6e80736e5eea76.tex

This is pdfTeX, Version 3.141592653-2.6-1.40.24 (TeX Live 2022/CVE-2023-32700 patched) (preloaded format=latex)
 restricted \write18 enabled.
entering extended mode
(./9e73bfe7c799e116dd6e80736e5eea76.tex
LaTeX2e <2022-06-01> patch level 5
L3 programming layer <2022-12-17>
(/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2021/10/04 v1.4n Standard LaTeX document class
(/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo))
(/usr/share/texlive/texmf-dist/tex/latex/type1cm/type1cm.sty)
(/usr/share/texlive/texmf-dist/tex/latex/cm-super/type1ec.sty
(/usr/share/texlive/texmf-dist/tex/latex/base/t1cmr.fd))
(/usr/share/texlive/texmf-dist/tex/latex/base/inputenc.sty)
(/usr/share/texlive/texmf-dist/tex/latex/geometry/geometry.sty
(/usr/share/texlive/texmf-dist/tex/latex/graphics/keyval.sty)
(/usr/share/texlive/texmf-dist/tex/generic/iftex/ifvtex.sty
(/usr/share/texlive/texmf-dist/tex/generic/iftex/iftex.sty)))
(/usr/share/texlive/texmf-dist/tex/latex/wasysym/wasysym.sty)
(/usr/share/texlive/texmf-dist/tex/latex/underscore/underscore.sty)
(/usr/share/texlive/texmf-dist/tex/latex/base/textcomp.sty)
(/usr/share/texlive/texmf-dist/tex/latex/l3backend/l3backend-dvips.def)
No file 9e73bfe7c799e116dd6e80736e5eea76.aux.
*geometry* driver: auto-detecting
*geometry* detected driver: dvips
(/usr/share/texlive/texmf-dist/tex/latex/wasysym/uwasy.fd)
! Undefined control sequence.
<recently read> \text 
                      
l.29 {\sffamily $\text
                      {dashes (-) are retained}$}%
No pages of output.
Transcript written on tmpt1zptv_z/9e73bfe7c799e116dd6e80736e5eea76.log.



Traceback (most recent call last):
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/backends/backend_gtk4.py", line 277, in idle_draw
    self.draw()
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/backends/backend_agg.py", line 388, in draw
    self.figure.draw(self.renderer)
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/artist.py", line 95, in draw_wrapper
    result = draw(artist, renderer, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/artist.py", line 72, in draw_wrapper
    return draw(artist, renderer)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/figure.py", line 3154, in draw
    mimage._draw_list_compositing_images(
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/image.py", line 132, in _draw_list_compositing_images
    a.draw(renderer)
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/artist.py", line 72, in draw_wrapper
    return draw(artist, renderer)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/axes/_base.py", line 3070, in draw
    mimage._draw_list_compositing_images(
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/image.py", line 132, in _draw_list_compositing_images
    a.draw(renderer)
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/artist.py", line 72, in draw_wrapper
    return draw(artist, renderer)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/text.py", line 748, in draw
    bbox, info, descent = self._get_layout(renderer)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/text.py", line 381, in _get_layout
    w, h, d = _get_text_metrics_with_cache(
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/text.py", line 69, in _get_text_metrics_with_cache
    return _get_text_metrics_with_cache_impl(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/text.py", line 77, in _get_text_metrics_with_cache_impl
    return renderer_ref().get_text_width_height_descent(text, fontprop, ismath)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/backends/backend_agg.py", line 213, in get_text_width_height_descent
    return super().get_text_width_height_descent(s, prop, ismath)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/backend_bases.py", line 652, in get_text_width_height_descent
    return self.get_texmanager().get_text_width_height_descent(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/texmanager.py", line 363, in get_text_width_height_descent
    dvifile = cls.make_dvi(tex, fontsize)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/texmanager.py", line 295, in make_dvi
    cls._run_checked_subprocess(
  File "/home/alex/Python/lib/python3.11/site-packages/matplotlib/texmanager.py", line 258, in _run_checked_subprocess
    raise RuntimeError(
RuntimeError: latex was not able to process the following string:
b'$\\\\text{dashes (-) are retained}$'

Here is the full command invocation and its output:

latex -interaction=nonstopmode --halt-on-error --output-directory=tmpx4zdudzo 9e73bfe7c799e116dd6e80736e5eea76.tex

This is pdfTeX, Version 3.141592653-2.6-1.40.24 (TeX Live 2022/CVE-2023-32700 patched) (preloaded format=latex)
 restricted \write18 enabled.
entering extended mode
(./9e73bfe7c799e116dd6e80736e5eea76.tex
LaTeX2e <2022-06-01> patch level 5
L3 programming layer <2022-12-17>
(/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2021/10/04 v1.4n Standard LaTeX document class
(/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo))
(/usr/share/texlive/texmf-dist/tex/latex/type1cm/type1cm.sty)
(/usr/share/texlive/texmf-dist/tex/latex/cm-super/type1ec.sty
(/usr/share/texlive/texmf-dist/tex/latex/base/t1cmr.fd))
(/usr/share/texlive/texmf-dist/tex/latex/base/inputenc.sty)
(/usr/share/texlive/texmf-dist/tex/latex/geometry/geometry.sty
(/usr/share/texlive/texmf-dist/tex/latex/graphics/keyval.sty)
(/usr/share/texlive/texmf-dist/tex/generic/iftex/ifvtex.sty
(/usr/share/texlive/texmf-dist/tex/generic/iftex/iftex.sty)))
(/usr/share/texlive/texmf-dist/tex/latex/wasysym/wasysym.sty)
(/usr/share/texlive/texmf-dist/tex/latex/underscore/underscore.sty)
(/usr/share/texlive/texmf-dist/tex/latex/base/textcomp.sty)
(/usr/share/texlive/texmf-dist/tex/latex/l3backend/l3backend-dvips.def)
No file 9e73bfe7c799e116dd6e80736e5eea76.aux.
*geometry* driver: auto-detecting
*geometry* detected driver: dvips
(/usr/share/texlive/texmf-dist/tex/latex/wasysym/uwasy.fd)
! Undefined control sequence.
<recently read> \text 
                      
l.29 {\sffamily $\text
                      {dashes (-) are retained}$}%
No pages of output.
Transcript written on tmpx4zdudzo/9e73bfe7c799e116dd6e80736e5eea76.log.

Expected outcome

the figure from
https://matplotlib.org/stable/users/prev_whats_new/whats_new_3.8.0.html#mathtext-now-supports-text

Additional information

fresh start, nothing else (all my matplotlib 3.7 code seems to work)

Operating system

Fedora 38

Matplotlib Version

3.8.0

Matplotlib Backend

GTK4Agg

Python version

3.11.4 (build from source)

Jupyter version

IPython 8.16.0

Installation

pip

Activity

  1. anntzer commented on Oct 1, 2023

    @anntzer
    Contributor

    You are running with usetex = True, in which case you need to add something like \usepackage{amsmath} to your rcParams["text.latex.preamble"] (and support for \text is then independent of matplotlib's version).
    Admittedly this could be made clearer both in the release notes and in the docs.

  2. 2sn commented on Oct 1, 2023

    @2sn
    Author

    @anntzer Yes, that fixes it. Thanks!

  3. melissawm commented on Oct 6, 2023

    @melissawm
    Member

    Is https://matplotlib.org/devdocs/users/explain/text/text_intro.html the best place to add this note, maybe in addition to the release notes? Or perhaps https://matplotlib.org/devdocs/users/explain/text/usetex.html?

  4. oscargus commented on Oct 6, 2023

    @oscargus
    Member

    I guess that there are two things here:

    1. Clarify the difference between LaTeX and MathText
    2. Clarify that many "common" LaTeX commands actually require packages to be loaded. Currently it says in the last document linked:

    rcParams["text.latex.preamble"] (default: '') is not officially supported. This option provides lots of flexibility, and lots of ways to cause problems. Please disable this option before reporting problems to the mailing list.

    Maybe this should be formulated differently (if we now want to officially support it).

    I am not really sure what @anntzer is referring to though. For the release note, the only thing I can see is to add the work "mathtext" to the text (and not just in the heading)?

  5. oscargus commented on Oct 6, 2023

    @oscargus
    Member

    There is also https://matplotlib.org/devdocs/users/explain/text/mathtext.html which is focusing on mathtext (although the title doesn't really say so), but at the same time most parts of it works as a LaTeX guide.

  6. jklymak commented on Oct 6, 2023

    @jklymak
    Member

    "Mathtext" is our jargon, so not really clear in the links above that it means rcParams['text.usetex']==False.

    I'm not sure if the jargon is defined anywhere explicitly. It should be. Probably https://matplotlib.org/devdocs/users/explain/text/mathtext.html is the right place.

  7. jklymak commented on Oct 6, 2023

    @jklymak
    Member

    @2sn I'm assuming you are actually have rcParams['text.usetex']=True.

  8. anntzer commented on Oct 6, 2023

    @anntzer
    Contributor

    I am not really sure what @anntzer is referring to though.

    We could have a list of tex commands we support in mathtext, and, for each of them, whether they need an additional usepackage to be used with usetex.

  9. jklymak commented on Oct 6, 2023

    @jklymak
    Member

    @anntzer, I think "mathtext" refers to our built-on math handling, which doesn't interact with LaTeX packages in any way?

  10. ksunden commented on Oct 6, 2023

    @ksunden
    Member

    It doesn't, but it does emulate certain features provided by e.g. amsmath, while most of it is "standard" latex, there are a few things that do require additional packages to be loaded if swapping to usetex=True. (Where you don't need to specify that for mathtext)

  11. jklymak commented on Oct 6, 2023

    @jklymak
    Member

    Oh I see - I mis-parsed his sentence.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions