Skip to content

[Bug]: Cryptic Mathtext ParseException on \text with double curly braces #32105

Description

@nj-vs-vh

Bug summary

Hi! I hit a cryptic error upon trying to render \text with double curly braces. This sometimes happens accidentally when I refactor code using f-strings (e.g., to substitute Python variable var into mathtext with subscripts I'd write f"x_\text{{sub}} = {var}"). However, when a string with such double curly braces is not an f-string ("\text{{example}}"), I get an error.

Code for reproduction

from matplotlib import pyplot as plt

fig, ax = plt.subplots()
ax.plot([1, 2], [1, 2], label="$\\text{{example}}$")
ax.legend()
fig.savefig("mre.png")

Actual outcome

The code above produces

ValueError: 
$\text{{example}}$
^
ParseException: Expected end of text, found '$'  (at char 0), (line:1, col:1)

If I replace \text with \mathrm, everything works, just like in LaTeX.

Expected outcome

In LaTeX \text{{example}} produces {example} in text font, i.e. the inner braces are taken as part of the text. The expected behaviour would be either to do this, or at least raise an informative error.

Additional information

No response

Operating system

OS/X

Matplotlib Version

3.10.9

Matplotlib Backend

No response

Python version

Python 3.14.5

Jupyter version

No response

Installation

pip

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