diff --git a/doc/api/next_api_changes/behavior/20367-AG.rst b/doc/api/next_api_changes/behavior/20367-AG.rst new file mode 100644 index 000000000000..38bc9ac05ad7 --- /dev/null +++ b/doc/api/next_api_changes/behavior/20367-AG.rst @@ -0,0 +1,12 @@ +``Text`` allows a boolean *parse_math* +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +`.Text` objects now allow a *parse_math* keyword-only argument +which defaults to True. When it is passed as False, that text object +will consider the string as a literal and will not try to parse it +as a mathtext object. + +``TextBox`` defaults it to False +-------------------------------- +`.TextBox` now defaults its text object's *parse_math** argument +as False. diff --git a/lib/matplotlib/tests/test_text.py b/lib/matplotlib/tests/test_text.py index dccb74ba0038..abefe3c3ab04 100644 --- a/lib/matplotlib/tests/test_text.py +++ b/lib/matplotlib/tests/test_text.py @@ -731,3 +731,13 @@ def test_unsupported_script(recwarn): [warn.message.args for warn in recwarn] == [(r"Glyph 2534 (\N{BENGALI DIGIT ZERO}) missing from current font.",), (r"Matplotlib currently does not support Bengali natively.",)]) + + +def test_parse_math(): + fig, ax = plt.subplots() + ax.text(0, 0, r"$ \wrong{math} $", parse_math=False) + fig.canvas.draw() + + ax.text(0, 0, r"$ \wrong{math} $", parse_math=True) + with pytest.raises(ValueError, match='Unknown symbol'): + fig.canvas.draw() diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index 9673f6ee6fc2..3c0bc9965ed1 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -125,6 +125,8 @@ def __init__(self, usetex=None, # defaults to rcParams['text.usetex'] wrap=False, transform_rotates_text=False, + *, + parse_math=True, **kwargs ): """ @@ -142,6 +144,7 @@ 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_wrap(wrap) self.set_verticalalignment(verticalalignment) self.set_horizontalalignment(horizontalalignment) @@ -1237,6 +1240,8 @@ def _preprocess_math(self, s): if s == " ": s = r"\ " return s, "TeX" + elif not self.get_parse_math(): + return s, False elif cbook.is_math_text(s): return s, True else: @@ -1274,6 +1279,25 @@ def get_usetex(self): """Return whether this `Text` object uses TeX for rendering.""" return self._usetex + def set_parse_math(self, parse_math): + """ + Override switch to enable/disable any mathtext + parsing for the given `Text` object. + + Parameters + ---------- + parse_math : bool + Whether to consider mathtext parsing for the string + """ + self._parse_math = bool(parse_math) + + def get_parse_math(self): + """ + Return whether mathtext parsing is considered + for this `Text` object. + """ + return self._parse_math + def set_fontname(self, fontname): """ Alias for `set_family`. diff --git a/lib/matplotlib/widgets.py b/lib/matplotlib/widgets.py index 7d2243f6c553..58a88263a90e 100644 --- a/lib/matplotlib/widgets.py +++ b/lib/matplotlib/widgets.py @@ -1018,9 +1018,12 @@ def __init__(self, ax, label, initial='', self.label = ax.text( -label_pad, 0.5, label, transform=ax.transAxes, verticalalignment='center', horizontalalignment='right') + + # TextBox's text object should not parse mathtext at all. self.text_disp = self.ax.text( - self.DIST_FROM_LEFT, 0.5, initial, transform=self.ax.transAxes, - verticalalignment='center', horizontalalignment='left') + self.DIST_FROM_LEFT, 0.5, initial, + transform=self.ax.transAxes, verticalalignment='center', + horizontalalignment='left', parse_math=False) self._observers = cbook.CallbackRegistry()