|
34 | 34 |
|
35 | 35 | def _get_preamble():
|
36 | 36 | """Prepare a LaTeX preamble based on the rcParams configuration."""
|
37 |
| - preamble = [ |
| 37 | + return "\n".join([ |
38 | 38 | # Remove Matplotlib's custom command \mathdefault. (Not using
|
39 | 39 | # \mathnormal instead since this looks odd with Computer Modern.)
|
40 | 40 | r"\def\mathdefault#1{#1}",
|
41 | 41 | # Use displaystyle for all math.
|
42 | 42 | r"\everymath=\expandafter{\the\everymath\displaystyle}",
|
43 | 43 | # Allow pgf.preamble to override the above definitions.
|
44 | 44 | 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 | + ]) |
60 | 60 |
|
61 | 61 |
|
62 | 62 | # It's better to use only one unit for all coordinates, since the
|
@@ -94,9 +94,9 @@ def _escape_and_apply_props(s, prop):
|
94 | 94 | family = prop.get_family()[0]
|
95 | 95 | if family in families:
|
96 | 96 | 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) |
100 | 100 | else:
|
101 | 101 | _log.warning("Ignoring unknown font: %s", family)
|
102 | 102 |
|
|
0 commit comments