Skip to content

Improve pgf special escapes. #18856

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

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions doc/api/next_api_changes/deprecations/18856-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
backend_pgf deprecations
~~~~~~~~~~~~~~~~~~~~~~~~
``backend_pgf.re_mathsep`` is deprecated.
35 changes: 11 additions & 24 deletions lib/matplotlib/backends/backend_pgf.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,43 +75,26 @@ def get_preamble():
# helper functions

NO_ESCAPE = r"(?<!\\)(?:\\\\)*"
re_mathsep = re.compile(NO_ESCAPE + r"\$")


re_mathsep = re.compile(NO_ESCAPE + r"\$") # Deprecated in 3.4.
_split_math = re.compile(rf"({NO_ESCAPE}\$.*?{NO_ESCAPE}\$)").split
_replace_escapetext = functools.partial(
# When the next character is _, ^, $, or % (not preceded by an escape),
# insert a backslash.
re.compile(NO_ESCAPE + "(?=[_^$%])").sub, "\\\\")
_replace_mathdefault = functools.partial(
# Replace \mathdefault (when not preceded by an escape) by empty string.
re.compile(NO_ESCAPE + r"(\\mathdefault)").sub, "")


def common_texification(text):
r"""
Do some necessary and/or useful substitutions for texts to be included in
LaTeX documents.

This distinguishes text-mode and math-mode by replacing the math separator
``$`` with ``\(\displaystyle %s\)``. Escaped math separators (``\$``)
are ignored.

The following characters are escaped in text segments: ``_^$%``
The characters ``_^$%`` are escaped in text mode (i.e., outside of math
blocks).
"""
# Sometimes, matplotlib adds the unknown command \mathdefault.
# Not using \mathnormal instead since this looks odd for the latex cm font.
text = _replace_mathdefault(text)
text = text.replace("\N{MINUS SIGN}", r"\ensuremath{-}")
# split text into normaltext and inline math parts
parts = re_mathsep.split(text)
for i, s in enumerate(parts):
if not i % 2:
# textmode replacements
s = _replace_escapetext(s)
else:
# mathmode replacements
s = r"\(\displaystyle %s\)" % s
parts[i] = s
parts = _split_math(text) # split into normal text and inline math parts
for i in range(0, len(parts), 2):
parts[i] = _replace_escapetext(parts[i]) # textmode replacements
return "".join(parts)


Expand Down Expand Up @@ -228,6 +211,8 @@ def _build_latex_header():
r"\usepackage{graphicx}",
latex_preamble,
latex_fontspec,
r"\def\mathdefault#1{#1}",
r"\everymath=\expandafter{\the\everymath\displaystyle}",
r"\begin{document}",
r"text $math \mu$", # force latex to load fonts now
r"\typeout{pgf_backend_query_start}",
Expand Down Expand Up @@ -835,6 +820,8 @@ def _print_pgf_to_fh(self, fh, *, bbox_inches_restore=None):
fh.write("\n")
writeln(fh, r"\begingroup")
writeln(fh, r"\makeatletter")
writeln(fh, r"\def\mathdefault#1{#1}")
writeln(fh, r"\everymath=\expandafter{\the\everymath\displaystyle}")
writeln(fh, r"\begin{pgfpicture}")
writeln(fh,
r"\pgfpathrectangle{\pgfpointorigin}{\pgfqpoint{%fin}{%fin}}"
Expand Down
4 changes: 3 additions & 1 deletion lib/matplotlib/tests/test_backend_pgf.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ def create_figure():


@pytest.mark.parametrize('plain_text, escaped_text', [
(r'quad_sum: $\sum x_i^2$', r'quad\_sum: \(\displaystyle \sum x_i^2\)'),
(r'quad_sum: $\sum x_i^2$', r'quad\_sum: $\sum x_i^2$'),
(r'no \$splits \$ here', r'no \$splits \$ here'),
('2$', r'2\$'),
('$2', r'\$2'),
('with_underscores', r'with\_underscores'),
('% not a comment', r'\% not a comment'),
('^not', r'\^not'),
Expand Down