Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix issue with PGF backend not falling back to default LaTeX fonts. #22111

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions examples/userdemo/pgf_fonts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
=========
"""

import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.use("pgf")
plt.rcParams.update({
"font.family": "serif",
# Use LaTeX default serif font.
Expand Down
17 changes: 12 additions & 5 deletions lib/matplotlib/backends/backend_pgf.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,18 @@ def _get_preamble():
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()))
try:
# 1) Forward slashes also work on Windows, so do not mess
# with backslashes.
# 2) The dirname needs to include a separator.
path = pathlib.Path(fm.findfont(family,
fallback_to_default=False))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor nit: only the findfont call should be wrapped in the try... except; this makes it clearer that you're catching failures of findfont and not e.g. findfont returning an invalid value for Path(). (Plus this avoids the awkward linebreak.)

except ValueError:
# Use default LaTeX font instead
pass
else:
preamble.append(r"\%s{%s}[Path=\detokenize{%s}]" % (
command, path.name, path.parent.as_posix() + "/"))
return "\n".join(preamble)


Expand Down