diff --git a/lib/matplotlib/mpl-data/matplotlibrc b/lib/matplotlib/mpl-data/matplotlibrc index 283636e26be0..f220cbeba686 100644 --- a/lib/matplotlib/mpl-data/matplotlibrc +++ b/lib/matplotlib/mpl-data/matplotlibrc @@ -301,6 +301,8 @@ # Values other than 0 or 6 have no defined meaning. #text.antialiased: True # If True (default), the text will be antialiased. # This only affects raster outputs. +#text.parse_math: True # Use mathtext if there is an even number of unescaped + # dollar signs. ## *************************************************************************** diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index 37015bc0d76a..78141c5445be 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -932,6 +932,7 @@ def _convert_validator_spec(key, conv): "text.hinting_factor": validate_int, "text.kerning_factor": validate_int, "text.antialiased": validate_bool, + "text.parse_math": validate_bool, "mathtext.cal": validate_font_properties, "mathtext.rm": validate_font_properties, diff --git a/lib/matplotlib/tests/test_text.py b/lib/matplotlib/tests/test_text.py index 0b703adadc25..6e70901ba9a4 100644 --- a/lib/matplotlib/tests/test_text.py +++ b/lib/matplotlib/tests/test_text.py @@ -755,6 +755,20 @@ def test_parse_math(): fig.canvas.draw() +def test_parse_math_rcparams(): + # Default is True + fig, ax = plt.subplots() + ax.text(0, 0, r"$ \wrong{math} $") + with pytest.raises(ValueError, match='Unknown symbol'): + fig.canvas.draw() + + # Setting rcParams to False + with mpl.rc_context({'text.parse_math': False}): + fig, ax = plt.subplots() + ax.text(0, 0, r"$ \wrong{math} $") + fig.canvas.draw() + + @image_comparison(['text_pdf_font42_kerning.pdf'], style='mpl20') def test_pdf_font42_kerning(): plt.rcParams['pdf.fonttype'] = 42 diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index a98b534d2ef6..1e675290574c 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -142,7 +142,7 @@ def __init__(self, wrap=False, transform_rotates_text=False, *, - parse_math=True, + parse_math=None, # defaults to rcParams['text.parse_math'] **kwargs ): """ @@ -163,7 +163,8 @@ def __init__(self, color if color is not None else mpl.rcParams["text.color"]) self.set_fontproperties(fontproperties) self.set_usetex(usetex) - self.set_parse_math(parse_math) + self.set_parse_math(parse_math if parse_math is not None else + mpl.rcParams['text.parse_math']) self.set_wrap(wrap) self.set_verticalalignment(verticalalignment) self.set_horizontalalignment(horizontalalignment)