Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Add parse_math in Text and default it False for TextBox #20367

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

Merged
merged 10 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions 12 doc/api/next_api_changes/behavior/20367-AG.rst
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 10 additions & 0 deletions 10 lib/matplotlib/tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
24 changes: 24 additions & 0 deletions 24 lib/matplotlib/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ def __init__(self,
usetex=None, # defaults to rcParams['text.usetex']
wrap=False,
transform_rotates_text=False,
*,
parse_math=True,
**kwargs
):
"""
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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`.
Expand Down
7 changes: 5 additions & 2 deletions 7 lib/matplotlib/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.