Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

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
2sn opened this issue Oct 1, 2023 · 11 comments · Fixed by #27017
Closed

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

2sn opened this issue Oct 1, 2023 · 11 comments · Fixed by #27017

Comments

@2sn
Copy link

2sn commented Oct 1, 2023

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

@anntzer
Copy link
Contributor

anntzer commented Oct 1, 2023

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.

@2sn
Copy link
Author

2sn commented Oct 1, 2023

@anntzer Yes, that fixes it. Thanks!

@melissawm
Copy link
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?

@oscargus
Copy link
Member

oscargus commented Oct 6, 2023

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)?

@oscargus
Copy link
Member

oscargus commented Oct 6, 2023

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.

@jklymak
Copy link
Member

jklymak commented Oct 6, 2023

"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.

@jklymak
Copy link
Member

jklymak commented Oct 6, 2023

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

@anntzer
Copy link
Contributor

anntzer commented Oct 6, 2023

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.

@jklymak
Copy link
Member

jklymak commented Oct 6, 2023

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

@ksunden
Copy link
Member

ksunden commented Oct 6, 2023

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)

@jklymak
Copy link
Member

jklymak commented Oct 6, 2023

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants