diff --git a/examples/userdemo/pgf_fonts.py b/examples/userdemo/pgf_fonts.py index 112c249752c2..b81234a104b9 100644 --- a/examples/userdemo/pgf_fonts.py +++ b/examples/userdemo/pgf_fonts.py @@ -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. diff --git a/lib/matplotlib/backends/backend_pgf.py b/lib/matplotlib/backends/backend_pgf.py index 5c4ffe9adb84..255714a7ea03 100644 --- a/lib/matplotlib/backends/backend_pgf.py +++ b/lib/matplotlib/backends/backend_pgf.py @@ -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)) + 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)