8000 Make generated pgf code more robust against later changes of tex engine. by anntzer · Pull Request #26690 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Make generated pgf code more robust against later changes of tex engine. #26690

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

Merged
merged 1 commit into from
Sep 13, 2023
Merged
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
38 changes: 19 additions & 19 deletions lib/matplotlib/backends/backend_pgf.py
568D
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,29 @@

def _get_preamble():
"""Prepare a LaTeX preamble based on the rcParams configuration."""
preamble = [
return "\n".join([
# Remove Matplotlib's custom command \mathdefault. (Not using
# \mathnormal instead since this looks odd with Computer Modern.)
r"\def\mathdefault#1{#1}",
# Use displaystyle for all math.
r"\everymath=\expandafter{\the\everymath\displaystyle}",
# Allow pgf.preamble to override the above definitions.
mpl.rcParams["pgf.preamble"],
]
if mpl.rcParams["pgf.texsystem"] != "pdflatex":
preamble.append("\\usepackage{fontspec}")
if mpl.rcParams["pgf.rcfonts"]:
families = ["serif", "sans\\-serif", "monospace"]
commands = ["setmainfont", "setsansfont", "setmonofont"]
for family, command in zip(families, commands):
# 1) Forward slashes also work on Windows, so don't mess with
# backslashes. 2) The dirname needs to include a separator.
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)
r"\ifdefined\pdftexversion\else % non-pdftex case.",
r" \usepackage{fontspec}",
*([
r" \%s{%s}[Path=\detokenize{%s/}]"
% (command, path.name, path.parent.as_posix())
for command, path in zip(
["setmainfont", "setsansfont", "setmonofont"],
[pathlib.Path(fm.findfont(family))
for family in ["serif", "sans\\-serif", "monospace"]]
)
] if mpl.rcParams["pgf.rcfonts"] else []),
r"\fi",
# Documented as "must come last".
mpl.texmanager._usepackage_if_not_loaded("underscore", option="strings"),
])


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

Expand Down
0