Skip to content

Commit ecc437b

Browse files
committed
Improve pgf special escapes.
- In common_texification.py, when splitting math and non-math segments, match *two* dollar signs surrounding a math segment rather than single dollar signs; this lets us correctly handle e.g. `$2` which we explicitly support in other backends as being plain text, not unbalanced math (the previous implementation would handle that as math). - Instead of adding `\displaystyle` in front of each and every math block, use the TeX-provided `\everymath` mechanism to just register that once (this basically auto-prepends `\displaystyle` before every math block). - Define `\mathdefault` as a noop (which is consistent with what texmanager does) instead of stripping it out, which would allow e.g. for the possibility that someone redefines `\mathdefault` in whatever way they want.
1 parent 5ba3911 commit ecc437b

File tree

3 files changed

+17
-25
lines changed

3 files changed

+17
-25
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
backend_pgf deprecations
2+
~~~~~~~~~~~~~~~~~~~~~~~~
3+
``backend_pgf.re_mathsep`` is deprecated.

lib/matplotlib/backends/backend_pgf.py

+11-24
Original file line numberDiff line numberDiff line change
@@ -75,42 +75,25 @@ def get_preamble():
7575
# helper functions
7676

7777
NO_ESCAPE = r"(?<!\\)(?:\\\\)*"
78-
re_mathsep = re.compile(NO_ESCAPE + r"\$")
79-
80-
78+
re_mathsep = re.compile(NO_ESCAPE + r"\$") # Deprecated in 3.4.
79+
_split_math = re.compile(rf"({NO_ESCAPE}\$.*?{NO_ESCAPE}\$)").split
8180
_replace_escapetext = functools.partial(
8281
# When the next character is _, ^, $, or % (not preceded by an escape),
8382
# insert a backslash.
8483
re.compile(NO_ESCAPE + "(?=[_^$%])").sub, "\\\\")
85-
_replace_mathdefault = functools.partial(
86-
# Replace \mathdefault (when not preceded by an escape) by empty string.
87-
re.compile(NO_ESCAPE + r"(\\mathdefault)").sub, "")
8884

8985

9086
def common_texification(text):
9187
r"""
9288
Do some necessary and/or useful substitutions for texts to be included in
9389
LaTeX documents.
9490
95-
This distinguishes text-mode and math-mode by replacing the math separator
96-
``$`` with ``\(\displaystyle %s\)``. Escaped math separators (``\$``)
97-
are ignored.
98-
99-
The following characters are escaped in text segments: ``_^$%``
91+
The characters ``_^$%`` are escaped in text mode (i.e., outside of math
92+
blocks).
10093
"""
101-
# Sometimes, matplotlib adds the unknown command \mathdefault.
102-
# Not using \mathnormal instead since this looks odd for the latex cm font.
103-
text = _replace_mathdefault(text)
104-
# split text into normaltext and inline math parts
105-
parts = re_mathsep.split(text)
106-
for i, s in enumerate(parts):
107-
if not i % 2:
108-
# textmode replacements
109-
s = _replace_escapetext(s)
110-
else:
111-
# mathmode replacements
112-
s = r"\(\displaystyle %s\)" % s
113-
parts[i] = s
94+
parts = _split_math(text) # split into normal text and inline math parts
95+
for i in range(0, len(parts), 2):
96+
parts[i] = _replace_escapetext(parts[i]) # textmode replacements
11497
return "".join(parts)
11598

11699

@@ -216,6 +199,8 @@ def _build_latex_header():
216199
r"\usepackage{graphicx}",
217200
latex_preamble,
218201
latex_fontspec,
202+
r"\def\mathdefault#1{#1}",
203+
r"\everymath=\expandafter{\the\everymath\displaystyle}",
219204
r"\begin{document}",
220205
r"text $math \mu$", # force latex to load fonts now
221206
r"\typeout{pgf_backend_query_start}",
@@ -827,6 +812,8 @@ def _print_pgf_to_fh(self, fh, *, bbox_inches_restore=None):
827812
fh.write("\n")
828813
writeln(fh, r"\begingroup")
829814
writeln(fh, r"\makeatletter")
815+
writeln(fh, r"\def\mathdefault#1{#1}")
816+
writeln(fh, r"\everymath=\expandafter{\the\everymath\displaystyle}")
830817
writeln(fh, r"\begin{pgfpicture}")
831818
writeln(fh,
832819
r"\pgfpathrectangle{\pgfpointorigin}{\pgfqpoint{%fin}{%fin}}"

lib/matplotlib/tests/test_backend_pgf.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ def create_figure():
9090

9191

9292
@pytest.mark.parametrize('plain_text, escaped_text', [
93-
(r'quad_sum: $\sum x_i^2$', r'quad\_sum: \(\displaystyle \sum x_i^2\)'),
93+
(r'quad_sum: $\sum x_i^2$', r'quad\_sum: $\sum x_i^2$'),
9494
(r'no \$splits \$ here', r'no \$splits \$ here'),
95+
('2$', '2$'),
96+
('$2', '$2'),
9597
('with_underscores', r'with\_underscores'),
9698
('% not a comment', r'\% not a comment'),
9799
('^not', r'\^not'),

0 commit comments

Comments
 (0)