From ce7f8a1b4503ba38b509465562121dd686a4d642 Mon Sep 17 00:00:00 2001 From: Aitik Gupta Date: Fri, 4 Jun 2021 23:15:35 +0530 Subject: [PATCH 01/10] Add override_math to Text --- lib/matplotlib/text.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index 9673f6ee6fc2..9a525ec36ad1 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -123,6 +123,7 @@ def __init__(self, linespacing=None, rotation_mode=None, usetex=None, # defaults to rcParams['text.usetex'] + override_math=False, wrap=False, transform_rotates_text=False, **kwargs @@ -142,6 +143,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_override_math(override_math) self.set_wrap(wrap) self.set_verticalalignment(verticalalignment) self.set_horizontalalignment(horizontalalignment) @@ -1237,7 +1239,7 @@ def _preprocess_math(self, s): if s == " ": s = r"\ " return s, "TeX" - elif cbook.is_math_text(s): + elif not self.get_override_math() and cbook.is_math_text(s): return s, True else: return s.replace(r"\$", "$"), False @@ -1274,6 +1276,22 @@ def get_usetex(self): """Return whether this `Text` object uses TeX for rendering.""" return self._usetex + def set_override_math(self, override_math): + """ + Parameters + ---------- + override_math : bool + Whether to override mathtext parsing with the normal parsing + """ + self._override_math = bool(override_math) + + def get_override_math(self): + """ + Return whether this `Text` object is supposed to + override mathtext parsing with normal parsing. + """ + return self._override_math + def set_fontname(self, fontname): """ Alias for `set_family`. From 7ac787a5be45454d5bbd4d284e2cf8a93fecb83b Mon Sep 17 00:00:00 2001 From: Aitik Gupta Date: Fri, 4 Jun 2021 23:31:44 +0530 Subject: [PATCH 02/10] Force TextBox's text to override math --- lib/matplotlib/widgets.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/widgets.py b/lib/matplotlib/widgets.py index 7d2243f6c553..2fbedb8a8dd1 100644 --- a/lib/matplotlib/widgets.py +++ b/lib/matplotlib/widgets.py @@ -1019,8 +1019,9 @@ def __init__(self, ax, label, initial='', -label_pad, 0.5, label, transform=ax.transAxes, verticalalignment='center', horizontalalignment='right') 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', override_math=True) self._observers = cbook.CallbackRegistry() From 367026f7d6d74b613c0802be4e053fbb576b4686 Mon Sep 17 00:00:00 2001 From: Aitik Gupta Date: Sat, 5 Jun 2021 09:10:22 +0530 Subject: [PATCH 03/10] Rename flag, docstrings fixed --- lib/matplotlib/text.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index 9a525ec36ad1..f38450106508 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -123,9 +123,9 @@ def __init__(self, linespacing=None, rotation_mode=None, usetex=None, # defaults to rcParams['text.usetex'] - override_math=False, wrap=False, transform_rotates_text=False, + parse_math=True, **kwargs ): """ @@ -143,7 +143,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_override_math(override_math) + self.set_parse_math(parse_math) self.set_wrap(wrap) self.set_verticalalignment(verticalalignment) self.set_horizontalalignment(horizontalalignment) @@ -1239,7 +1239,7 @@ def _preprocess_math(self, s): if s == " ": s = r"\ " return s, "TeX" - elif not self.get_override_math() and cbook.is_math_text(s): + elif self.get_parse_math() and cbook.is_math_text(s): return s, True else: return s.replace(r"\$", "$"), False @@ -1276,21 +1276,24 @@ def get_usetex(self): """Return whether this `Text` object uses TeX for rendering.""" return self._usetex - def set_override_math(self, override_math): + def set_parse_math(self, parse_math): """ + Override switch to enable/disable any mathtext + parsing for the given `Text` object. + Parameters ---------- - override_math : bool - Whether to override mathtext parsing with the normal parsing + parse_math : bool + Whether to consider mathtext parsing for the string """ - self._override_math = bool(override_math) + self._parse_math = bool(parse_math) - def get_override_math(self): + def get_parse_math(self): """ - Return whether this `Text` object is supposed to - override mathtext parsing with normal parsing. + Return whether mathtext parsing is considered + for this `Text` object. """ - return self._override_math + return self._parse_math def set_fontname(self, fontname): """ From c2ae3d63ae5308fb985eec3995176521006e8ee6 Mon Sep 17 00:00:00 2001 From: Aitik Gupta Date: Sat, 5 Jun 2021 09:19:20 +0530 Subject: [PATCH 04/10] Add test for parsing mathtext --- lib/matplotlib/tests/test_text.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/matplotlib/tests/test_text.py b/lib/matplotlib/tests/test_text.py index dccb74ba0038..71d465bef60f 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() + + with pytest.raises(ValueError, match='Unknown symbol'): + ax.text(0, 0, r"$ \wrong{math} $", parse_math=True) + fig.canvas.draw() From faabc0d8e92560fa9227937243698fea6a9a4da0 Mon Sep 17 00:00:00 2001 From: Aitik Gupta Date: Sat, 5 Jun 2021 09:57:44 +0530 Subject: [PATCH 05/10] update textbox param --- lib/matplotlib/widgets.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/widgets.py b/lib/matplotlib/widgets.py index 2fbedb8a8dd1..58a88263a90e 100644 --- a/lib/matplotlib/widgets.py +++ b/lib/matplotlib/widgets.py @@ -1018,10 +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', override_math=True) + horizontalalignment='left', parse_math=False) self._observers = cbook.CallbackRegistry() From dee7232bd69c185214ed61ab360022eea431f233 Mon Sep 17 00:00:00 2001 From: Aitik Gupta Date: Sat, 5 Jun 2021 18:41:32 +0530 Subject: [PATCH 06/10] Make parse_math kw-only --- lib/matplotlib/text.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index f38450106508..c8f84c831746 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -125,6 +125,7 @@ def __init__(self, usetex=None, # defaults to rcParams['text.usetex'] wrap=False, transform_rotates_text=False, + *, parse_math=True, **kwargs ): From 6a6a51e70f8c406c50bc5f3b1560e10a4a1fd9f1 Mon Sep 17 00:00:00 2001 From: Aitik Gupta Date: Sun, 20 Jun 2021 02:17:45 +0530 Subject: [PATCH 07/10] Add WhatsNew API entry for parse_math --- doc/api/next_api_changes/behavior/20367-AG.rst | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 doc/api/next_api_changes/behavior/20367-AG.rst 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..decf8b8374b7 --- /dev/null +++ b/doc/api/next_api_changes/behavior/20367-AG.rst @@ -0,0 +1,12 @@ +``Text`` +~~~~~~~~ + +``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`` +----------- +``TextBox`` now defaults its text object's **parse_math** argument +as False. From cff9073097e40c5aebfcf9e3b2c17acce3de55b9 Mon Sep 17 00:00:00 2001 From: Aitik Gupta Date: Thu, 24 Jun 2021 07:37:56 +0530 Subject: [PATCH 08/10] Reword API change doc --- doc/api/next_api_changes/behavior/20367-AG.rst | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/doc/api/next_api_changes/behavior/20367-AG.rst b/doc/api/next_api_changes/behavior/20367-AG.rst index decf8b8374b7..38bc9ac05ad7 100644 --- a/doc/api/next_api_changes/behavior/20367-AG.rst +++ b/doc/api/next_api_changes/behavior/20367-AG.rst @@ -1,12 +1,12 @@ -``Text`` -~~~~~~~~ +``Text`` allows a boolean *parse_math* +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -``Text`` objects now allow a **parse_math** keyword-only argument +`.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`` ------------ -``TextBox`` now defaults its text object's **parse_math** argument +``TextBox`` defaults it to False +-------------------------------- +`.TextBox` now defaults its text object's *parse_math** argument as False. From 61792ca3f426714debc74f2603d86f02b47547f4 Mon Sep 17 00:00:00 2001 From: Aitik Gupta Date: Thu, 24 Jun 2021 07:42:34 +0530 Subject: [PATCH 09/10] Raise check for correct line --- lib/matplotlib/tests/test_text.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_text.py b/lib/matplotlib/tests/test_text.py index 71d465bef60f..abefe3c3ab04 100644 --- a/lib/matplotlib/tests/test_text.py +++ b/lib/matplotlib/tests/test_text.py @@ -738,6 +738,6 @@ def test_parse_math(): 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'): - ax.text(0, 0, r"$ \wrong{math} $", parse_math=True) fig.canvas.draw() From 9e9b8d562034a1e6af9b1e858d7986fa3e46421a Mon Sep 17 00:00:00 2001 From: Aitik Gupta Date: Fri, 25 Jun 2021 16:55:09 +0530 Subject: [PATCH 10/10] Do not escape dollars if parse_math is True --- lib/matplotlib/text.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index c8f84c831746..3c0bc9965ed1 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -1240,7 +1240,9 @@ def _preprocess_math(self, s): if s == " ": s = r"\ " return s, "TeX" - elif self.get_parse_math() and cbook.is_math_text(s): + elif not self.get_parse_math(): + return s, False + elif cbook.is_math_text(s): return s, True else: return s.replace(r"\$", "$"), False