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

Commit 833658b

Browse filesBrowse files
authored
Merge pull request #20367 from aitikgupta/math-override
Add parse_math in Text and default it False for TextBox
2 parents dab3974 + 9e9b8d5 commit 833658b
Copy full SHA for 833658b

File tree

Expand file treeCollapse file tree

4 files changed

+51
-2
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+51
-2
lines changed
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
``Text`` allows a boolean *parse_math*
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
`.Text` objects now allow a *parse_math* keyword-only argument
5+
which defaults to True. When it is passed as False, that text object
6+
will consider the string as a literal and will not try to parse it
7+
as a mathtext object.
8+
9+
``TextBox`` defaults it to False
10+
--------------------------------
11+
`.TextBox` now defaults its text object's *parse_math** argument
12+
as False.

‎lib/matplotlib/tests/test_text.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_text.py
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,3 +731,13 @@ def test_unsupported_script(recwarn):
731731
[warn.message.args for warn in recwarn] ==
732732
[(r"Glyph 2534 (\N{BENGALI DIGIT ZERO}) missing from current font.",),
733733
(r"Matplotlib currently does not support Bengali natively.",)])
734+
735+
736+
def test_parse_math():
737+
fig, ax = plt.subplots()
738+
ax.text(0, 0, r"$ \wrong{math} $", parse_math=False)
739+
fig.canvas.draw()
740+
741+
ax.text(0, 0, r"$ \wrong{math} $", parse_math=True)
742+
with pytest.raises(ValueError, match='Unknown symbol'):
743+
fig.canvas.draw()

‎lib/matplotlib/text.py

Copy file name to clipboardExpand all lines: lib/matplotlib/text.py
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ def __init__(self,
125125
usetex=None, # defaults to rcParams['text.usetex']
126126
wrap=False,
127127
transform_rotates_text=False,
128+
*,
129+
parse_math=True,
128130
**kwargs
129131
):
130132
"""
@@ -142,6 +144,7 @@ def __init__(self,
142144
color if color is not None else mpl.rcParams["text.color"])
143145
self.set_fontproperties(fontproperties)
144146
self.set_usetex(usetex)
147+
self.set_parse_math(parse_math)
145148
self.set_wrap(wrap)
146149
self.set_verticalalignment(verticalalignment)
147150
self.set_horizontalalignment(horizontalalignment)
@@ -1237,6 +1240,8 @@ def _preprocess_math(self, s):
12371240
if s == " ":
12381241
s = r"\ "
12391242
return s, "TeX"
1243+
elif not self.get_parse_math():
1244+
return s, False
12401245
elif cbook.is_math_text(s):
12411246
return s, True
12421247
else:
@@ -1274,6 +1279,25 @@ def get_usetex(self):
12741279
"""Return whether this `Text` object uses TeX for rendering."""
12751280
return self._usetex
12761281

1282+
def set_parse_math(self, parse_math):
1283+
"""
1284+
Override switch to enable/disable any mathtext
1285+
parsing for the given `Text` object.
1286+
1287+
Parameters
1288+
----------
1289+
parse_math : bool
1290+
Whether to consider mathtext parsing for the string
1291+
"""
1292+
self._parse_math = bool(parse_math)
1293+
1294+
def get_parse_math(self):
1295+
"""
1296+
Return whether mathtext parsing is considered
1297+
for this `Text` object.
1298+
"""
1299+
return self._parse_math
1300+
12771301
def set_fontname(self, fontname):
12781302
"""
12791303
Alias for `set_family`.

‎lib/matplotlib/widgets.py

Copy file name to clipboardExpand all lines: lib/matplotlib/widgets.py
+5-2Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,9 +1018,12 @@ def __init__(self, ax, label, initial='',
10181018
self.label = ax.text(
10191019
-label_pad, 0.5, label, transform=ax.transAxes,
10201020
verticalalignment='center', horizontalalignment='right')
1021+
1022+
# TextBox's text object should not parse mathtext at all.
10211023
self.text_disp = self.ax.text(
1022-
self.DIST_FROM_LEFT, 0.5, initial, transform=self.ax.transAxes,
1023-
verticalalignment='center', horizontalalignment='left')
1024+
self.DIST_FROM_LEFT, 0.5, initial,
1025+
transform=self.ax.transAxes, verticalalignment='center',
1026+
horizontalalignment='left', parse_math=False)
10241027

10251028
self._observers = cbook.CallbackRegistry()
10261029

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.