Skip to content

Commit 1936c94

Browse files
committed
Also handle \displaystyle and \mathdefault at the TeX level.
Note that tex_escape is still being tested by test_minus_signs_with_tex, which explicitly checks support for unicode minus.
1 parent 972b815 commit 1936c94

File tree

2 files changed

+13
-33
lines changed

2 files changed

+13
-33
lines changed

lib/matplotlib/backends/backend_pgf.py

+12-25
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838
@_api.caching_module_getattr
3939
class __getattr__:
4040
NO_ESCAPE = _api.deprecated("3.6", obj_type="")(
41-
property(lambda self: _NO_ESCAPE))
41+
property(lambda self: r"(?<!\\)(?:\\\\)*"))
4242
re_mathsep = _api.deprecated("3.6", obj_type="")(
43-
property(lambda self: _split_math.__self__))
43+
property(lambda self: r"(?<!\\)(?:\\\\)*\$"))
4444

4545

4646
@_api.deprecated("3.6")
@@ -58,7 +58,15 @@ def get_preamble():
5858

5959
def _get_preamble():
6060
"""Prepare a LaTeX preamble based on the rcParams configuration."""
61-
preamble = [mpl.rcParams["pgf.preamble"]]
61+
preamble = [
62+
# Remove Matplotlib's custom command \mathdefault. (Not using
63+
# \mathnormal instead since this looks odd with Computer Modern.)
64+
r"\def\mathdefault#1{#1}",
65+
# Use displaystyle for all math.
66+
r"\everymath=\expandafter{\the\everymath\displaystyle}",
67+
# Allow pgf.preamble to override the above definitions.
68+
mpl.rcParams["pgf.preamble"],
69+
]
6270
if mpl.rcParams["pgf.texsystem"] != "pdflatex":
6371
preamble.append("\\usepackage{fontspec}")
6472
if mpl.rcParams["pgf.rcfonts"]:
@@ -83,13 +91,6 @@ def _get_preamble():
8391
mpl_in_to_pt = 1. / mpl_pt_to_in
8492

8593

86-
_NO_ESCAPE = r"(?<!\\)(?:\\\\)*"
87-
_split_math = re.compile(_NO_ESCAPE + r"\$").split
88-
_replace_mathdefault = functools.partial(
89-
# Replace \mathdefault (when not preceded by an escape) by empty string.
90-
re.compile(_NO_ESCAPE + r"(\\mathdefault)").sub, "")
91-
92-
9394
@_api.deprecated("3.6")
9495
def common_texification(text):
9596
return _tex_escape(text)
@@ -99,22 +100,8 @@ def _tex_escape(text):
99100
r"""
100101
Do some necessary and/or useful substitutions for texts to be included in
101102
LaTeX documents.
102-
103-
This distinguishes text-mode and math-mode by replacing the math separator
104-
``$`` with ``\(\displaystyle %s\)``. Escaped math separators (``\$``)
105-
are ignored.
106103
"""
107-
# Sometimes, matplotlib adds the unknown command \mathdefault.
108-
# Not using \mathnormal instead since this looks odd for the latex cm font.
109-
text = _replace_mathdefault(text)
110-
text = text.replace("\N{MINUS SIGN}", r"\ensuremath{-}")
111-
# split text into normaltext and inline math parts
112-
parts = _split_math(text)
113-
for i, s in enumerate(parts):
114-
if i % 2: # mathmode replacements
115-
s = r"\(\displaystyle %s\)" % s
116-
parts[i] = s
117-
return "".join(parts)
104+
return text.replace("\N{MINUS SIGN}", r"\ensuremath{-}")
118105

119106

120107
@_api.deprecated("3.6")

lib/matplotlib/tests/test_backend_pgf.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import matplotlib.pyplot as plt
1212
from matplotlib.testing import _has_tex_package, _check_for_pgf
1313
from matplotlib.testing.compare import compare_images, ImageComparisonFailure
14-
from matplotlib.backends.backend_pgf import PdfPages, _tex_escape
14+
from matplotlib.backends.backend_pgf import PdfPages
1515
from matplotlib.testing.decorators import (
1616
_image_directories, check_figures_equal, image_comparison)
1717
from matplotlib.testing._markers import (
@@ -33,13 +33,6 @@ def compare_figure(fname, savefig_kwargs={}, tol=0):
3333
raise ImageComparisonFailure(err)
3434

3535

36-
@pytest.mark.parametrize('plain_text, escaped_text', [
37-
(r'quad_sum: $\sum x_i^2$', r'quad_sum: \(\displaystyle \sum x_i^2\)'),
38-
])
39-
def test_tex_escape(plain_text, escaped_text):
40-
assert _tex_escape(plain_text) == escaped_text
41-
42-
4336
@needs_pgf_xelatex
4437
@needs_ghostscript
4538
@pytest.mark.backend('pgf')

0 commit comments

Comments
 (0)