Skip to content

Commit b7dbd28

Browse files
committed
Add text.parse_math rcParams
1 parent 45d26c5 commit b7dbd28

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

lib/matplotlib/mpl-data/matplotlibrc

+2
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,8 @@
296296
# Values other than 0 or 6 have no defined meaning.
297297
#text.antialiased: True # If True (default), the text will be antialiased.
298298
# This only affects raster outputs.
299+
#text.parse_math: True # Use mathtext if there is an even number of unescaped
300+
# dollar signs.
299301

300302

301303
## ***************************************************************************

lib/matplotlib/rcsetup.py

+1
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,7 @@ def _convert_validator_spec(key, conv):
918918
"text.hinting_factor": validate_int,
919919
"text.kerning_factor": validate_int,
920920
"text.antialiased": validate_bool,
921+
"text.parse_math": validate_bool,
921922

922923
"mathtext.cal": validate_font_properties,
923924
"mathtext.rm": validate_font_properties,

lib/matplotlib/tests/test_text.py

+14
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,20 @@ def test_parse_math():
755755
fig.canvas.draw()
756756

757757

758+
def test_parse_math_rcparams():
759+
# Default is True
760+
fig, ax = plt.subplots()
761+
ax.text(0, 0, r"$ \wrong{math} $")
762+
with pytest.raises(ValueError, match='Unknown symbol'):
763+
fig.canvas.draw()
764+
765+
# Setting rcParams to False
766+
plt.rcParams['text.parse_math'] = False
767+
fig, ax = plt.subplots()
768+
ax.text(0, 0, r"$ \wrong{math} $")
769+
fig.canvas.draw()
770+
771+
758772
@image_comparison(['text_pdf_font42_kerning.pdf'], style='mpl20')
759773
def test_pdf_font42_kerning():
760774
plt.rcParams['pdf.fonttype'] = 42

lib/matplotlib/text.py

+15-11
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,19 @@ def __repr__(self):
130130

131131
def __init__(self,
132132
x=0, y=0, text='',
133-
color=None, # defaults to rc params
133+
color=None,
134134
verticalalignment='baseline',
135135
horizontalalignment='left',
136136
multialignment=None,
137137
fontproperties=None, # defaults to FontProperties()
138138
rotation=None,
139139
linespacing=None,
140140
rotation_mode=None,
141-
usetex=None, # defaults to rcParams['text.usetex']
141+
usetex=None,
142142
wrap=False,
143143
transform_rotates_text=False,
144144
*,
145-
parse_math=True,
145+
parse_math=None,
146146
**kwargs
147147
):
148148
"""
@@ -156,8 +156,7 @@ def __init__(self,
156156
self._x, self._y = x, y
157157
self._text = ''
158158
self.set_text(text)
159-
self.set_color(
160-
color if color is not None else mpl.rcParams["text.color"])
159+
self.set_color(color)
161160
self.set_fontproperties(fontproperties)
162161
self.set_usetex(usetex)
163162
self.set_parse_math(parse_math)
@@ -934,8 +933,11 @@ def set_color(self, color):
934933
935934
Parameters
936935
----------
937-
color : color
936+
color : color, default: :rc:`text.color`
938937
"""
938+
if color is None:
939+
color = mpl.rcParams["text.color"]
940+
939941
# "auto" is only supported by axisartist, but we can just let it error
940942
# out at draw time for simplicity.
941943
if not cbook._str_equal(color, "auto"):
@@ -1269,9 +1271,8 @@ def set_usetex(self, usetex):
12691271
"""
12701272
Parameters
12711273
----------
1272-
usetex : bool or None
1273-
Whether to render using TeX, ``None`` means to use
1274-
:rc:`text.usetex`.
1274+
usetex : bool, default: :rc:`text.usetex`
1275+
Whether to render using TeX.
12751276
"""
12761277
if usetex is None:
12771278
self._usetex = mpl.rcParams['text.usetex']
@@ -1289,11 +1290,14 @@ def set_parse_math(self, parse_math):
12891290
12901291
Parameters
12911292
----------
1292-
parse_math : bool
1293+
parse_math : bool, default: :rc:`text.parse_math`
12931294
If False, this `Text` will never use mathtext. If True, mathtext
12941295
will be used if there is an even number of unescaped dollar signs.
12951296
"""
1296-
self._parse_math = bool(parse_math)
1297+
if parse_math is None:
1298+
self._parse_math = mpl.rcParams['text.parse_math']
1299+
else:
1300+
self._parse_math = bool(parse_math)
12971301

12981302
def get_parse_math(self):
12991303
"""Return whether mathtext parsing is considered for this `Text`."""

0 commit comments

Comments
 (0)