From b5c535b3cf08a15115637035a67a2a730856090b Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Thu, 14 Jul 2022 23:06:29 +0200 Subject: [PATCH 1/2] Tweak pgf escapes. - We don't need to escape underscores manually, but can rely on the underscore package like we already do for usetex. - We don't actually escape dollars (we parse them as math delimiters first). - Slightly tweak error message generation. - Move escaping tests before the big `create_figure` definition, which is used for further tests below. --- lib/matplotlib/backends/backend_pgf.py | 19 +++++++++------- lib/matplotlib/tests/test_backend_pgf.py | 28 ++++++++++++++---------- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/lib/matplotlib/backends/backend_pgf.py b/lib/matplotlib/backends/backend_pgf.py index 5c4ffe9adb84..349aec3891ef 100644 --- a/lib/matplotlib/backends/backend_pgf.py +++ b/lib/matplotlib/backends/backend_pgf.py @@ -70,6 +70,8 @@ def _get_preamble(): path = pathlib.Path(fm.findfont(family)) preamble.append(r"\%s{%s}[Path=\detokenize{%s/}]" % ( command, path.name, path.parent.as_posix())) + preamble.append(mpl.texmanager._usepackage_if_not_loaded( + "underscore", option="strings")) # Documented as "must come last". return "\n".join(preamble) @@ -84,9 +86,8 @@ def _get_preamble(): _NO_ESCAPE = r"(? Date: Fri, 15 Jul 2022 18:04:22 +0200 Subject: [PATCH 2/2] Help troubleshooting of latex failures. --- lib/matplotlib/backends/backend_pgf.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/backends/backend_pgf.py b/lib/matplotlib/backends/backend_pgf.py index 349aec3891ef..a5c93136829d 100644 --- a/lib/matplotlib/backends/backend_pgf.py +++ b/lib/matplotlib/backends/backend_pgf.py @@ -310,8 +310,10 @@ def __init__(self): test_input = self.latex_header + latex_end stdout, stderr = latex.communicate(test_input) if latex.returncode != 0: - raise LatexError("LaTeX returned an error, probably missing font " - "or error in preamble.", stdout) + raise LatexError( + f"LaTeX errored (probably missing font or error in preamble) " + f"while processing the following input:\n{test_input}", + stdout) self.latex = None # Will be set up on first use. # Per-instance cache.