10000 Make generated pgf code more robust against later changes of tex engine. · matplotlib/matplotlib@7ebce14 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7ebce14

Browse files
committed
Make generated pgf code more robust against later changes of tex engine.
By moving the conditionals to tex itself, the generated tex code becomes compilable with engines different from the one originally configured; this is similar to the `\ifdefined\pdfpagewidth` test in PdfPages.savefig.
1 parent 469b96b commit 7ebce14

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

lib/matplotlib/backends/backend_pgf.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,29 @@
3434

3535
def _get_preamble():
3636
"""Prepare a LaTeX preamble based on the rcParams configuration."""
37-
preamble = [
37+
return "\n".join([
3838
# Remove Matplotlib's custom command \mathdefault. (Not using
3939
# \mathnormal instead since this looks odd with Computer Modern.)
4040
r"\def\mathdefault#1{#1}",
4141
# Use displaystyle for all math.
4242
r"\everymath=\expandafter{\the\everymath\displaystyle}",
4343
# Allow pgf.preamble to override the above definitions.
4444
mpl.rcParams["pgf.preamble"],
45-
]
46-
if mpl.rcParams["pgf.texsystem"] != "pdflatex":
47-
preamble.append("\\usepackage{fontspec}")
48-
if mpl.rcParams["pgf.rcfonts"]:
49-
families = ["serif", "sans\\-serif", "monospace"]
50-
commands = ["setmainfont", "setsansfont", "setmonofont"]
51-
for family, command in zip(families, commands):
52-
# 1) Forward slashes also work on Windows, so don't mess with
53-
# backslashes. 2) The dirname needs to include a separator.
54-
path = pathlib.Path(fm.findfont(family))
55-
preamble.append(r"\%s{%s}[Path=\detokenize{%s/}]" % (
56-
command, path.name, path.parent.as_posix()))
57-
preamble.append(mpl.texmanager._usepackage_if_not_loaded(
58-
"underscore", option="strings")) # Documented as "must come last".
59-
return "\n".join(preamble)
45+
r"\ifdefined\pdftexversion\else % non-pdftex case.",
46+
r" \usepackage{fontspec}",
47+
*([
48+
r" \%s{%s}[Path=\detokenize{%s/}]"
49+
% (command, path.name, path.parent.as_posix())
50+
for command, path in zip(
51+
["setmainfont", "setsansfont", "setmonofont"],
52+
[pathlib.Path(fm.findfont(family))
53+
for family in ["serif", "sans\\-serif", "monospace"]]
54+
)
55+
] if mpl.rcParams["pgf.rcfonts"] else []),
56+
r"\fi",
57+
# Documented as "must come last".
58+
mpl.texmanager._usepackage_if_not_loaded("underscore", option="strings"),
59+
])
6060

6161

6262
# It's better to use only one unit for all coordinates, since the
@@ -94,9 +94,9 @@ def _escape_and_apply_props(s, prop):
9494
family = prop.get_family()[0]
9595
if family in families:
9696
commands.append(families[family])
97-
elif (any(font.name == family for font in fm.fontManager.ttflist)
98-
and mpl.rcParams["pgf.texsystem"] != "pdflatex"):
99-
commands.append(r"\setmainfont{%s}\rmfamily" % family)
97+
elif any(font.name == family for font in fm.fontManager.ttflist):
98+
commands.append(
99+
r"\ifdefined\pdftexversion\else\setmainfont{%s}\rmfamily\fi" % family)
100100
else:
101101
_log.warning("Ignoring unknown font: %s", family)
102102

0 commit comments

Comments
 (0)
0