Skip to content

Add text.parse_math rcParams #22556

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 1 commit into from
Mar 29, 2022
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
2 changes: 2 additions & 0 deletions lib/matplotlib/mpl-data/matplotlibrc
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@
# Values other than 0 or 6 have no defined meaning.
#text.antialiased: True # If True (default), the text will be antialiased.
# This only affects raster outputs.
#text.parse_math: True # Use mathtext if there is an even number of unescaped
# dollar signs.


## ***************************************************************************
Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,7 @@ def _convert_validator_spec(key, conv):
"text.hinting_factor": validate_int,
"text.kerning_factor": validate_int,
"text.antialiased": validate_bool,
"text.parse_math": validate_bool,

"mathtext.cal": validate_font_properties,
"mathtext.rm": validate_font_properties,
Expand Down
14 changes: 14 additions & 0 deletions lib/matplotlib/tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,20 @@ def test_parse_math():
fig.canvas.draw()


def test_parse_math_rcparams():
# Default is True
fig, ax = plt.subplots()
ax.text(0, 0, r"$ \wrong{math} $")
with pytest.raises(ValueError, match='Unknown symbol'):
fig.canvas.draw()

# Setting rcParams to False
with mpl.rc_context({'text.parse_math': False}):
fig, ax = plt.subplots()
ax.text(0, 0, r"$ \wrong{math} $")
fig.canvas.draw()


@image_comparison(['text_pdf_font42_kerning.pdf'], style='mpl20')
def test_pdf_font42_kerning():
plt.rcParams['pdf.fonttype'] = 42
Expand Down
5 changes: 3 additions & 2 deletions lib/matplotlib/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def __init__(self,
wrap=False,
transform_rotates_text=False,
*,
parse_math=True,
parse_math=None, # defaults to rcParams['text.parse_math']
**kwargs
):
"""
Expand All @@ -163,7 +163,8 @@ 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_parse_math(parse_math if parse_math is not None else
mpl.rcParams['text.parse_math'])
self.set_wrap(wrap)
self.set_verticalalignment(verticalalignment)
self.set_horizontalalignment(horizontalalignment)
Expand Down