Skip to content

Commit 41207b2

Browse files
committed
Move preamble to tex configuration section
1 parent eeccfb1 commit 41207b2

6 files changed

Lines changed: 52 additions & 69 deletions

File tree

data/share/matplotlib-pgfutils/pgfutils.toml

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,39 @@ marginpar_sep = "11 points"
5151
num_columns = 1.5
5252
columnsep = "10 points"
5353

54+
# Custom preamble; by default, this is empty. This is useful if you're doing
55+
# something non-standard and you need to ensure when Matplotlib runs TeX (e.g.,
56+
# to measure string sizes) that it is configured in the same manner as your
57+
# final document. This can span over multiple lines. Put one or more spaces
58+
# before each line; these will be chopped off when the configuration is loaded.
59+
preamble = ""
60+
61+
# If enabled, then any instances of $basedir or ${basedir} in the preamble will
62+
# be replaced with the absolute path to the base or top-level directory, i.e.,
63+
# the directory the script is being run from (and that would contain
64+
# pgfutils.cfg).
65+
preamble_substitute = false
66+
67+
# One common use for the preamble is to load custom fonts. The path to fonts
68+
# should be absolute, not relative, as Matplotlib runs TeX in a different
69+
# directory when measuring string sizes. This can be easily achieved by using
70+
# preamble substitution:
71+
#
72+
# [tex]
73+
# preamble_substitute = true
74+
# preamble = '''
75+
# \usepackage{fontspec}
76+
# \setmainfont[
77+
# Path = ${basedir}/fonts/,
78+
# Extension = .ttf,
79+
# UprightFont = *-Regular,
80+
# BoldFont = *-Bold,
81+
# BoldItalicFont = *-BoldItalic
82+
# ]{MyCustomFontName}'''
83+
#
84+
# [pgfutils]
85+
# font_family = serif
86+
# font_name = MyCustomFontName
5487

5588

5689
# Settings for plotting.
@@ -82,37 +115,7 @@ legend_border_color = "0.8"
82115
legend_background = "white"
83116
legend_opacity = 0.8
84117
legend_font_size = 10.0
85-
86-
# Custom preamble; by default, this is empty. This is useful if you're doing
87-
# something non-standard and you need to ensure when Matplotlib runs TeX (e.g.,
88-
# to measure string sizes) that it is configured in the same manner as your
89-
# final document. This can span over multiple lines. Put one or more spaces
90-
# before each line; these will be chopped off when the configuration is loaded.
91-
preamble = ""
92-
93-
# If enabled, then any instances of $basedir or ${basedir} in the preamble will
94-
# be replaced with the absolute path to the base or top-level directory, i.e.,
95-
# the directory the script is being run from (and that would contain
96-
# pgfutils.cfg).
97-
preamble_substitute = false
98-
99-
# One common use for the preamble is to load custom fonts. The path to fonts
100-
# should be absolute, not relative, as Matplotlib runs TeX in a different
101-
# directory when measuring string sizes. This can be easily achieved by using
102-
# preamble substitution:
103-
#
104-
#preamble_substitute = true
105-
#preamble =
106-
# \usepackage{fontspec}
107-
# \setmainfont[
108-
# Path = ${basedir}/fonts/,
109-
# Extension = .ttf,
110-
# UprightFont = *-Regular,
111-
# BoldFont = *-Bold,
112-
# BoldItalicFont = *-BoldItalic
113-
# ]{MyCustomFontName}
114-
#font_family = serif
115-
#font_name = MyCustomFontName
118+
separate_legend = false
116119

117120
# Extra library-specific file trackers to install.
118121
extra_tracking = ""

pgfutils.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,6 @@ class PathConfig(TypedDict):
338338
class PGFUtilsConfig(TypedDict):
339339
"""Configuration of pgfutils behaviour."""
340340

341-
preamble: str
342-
preamble_substitute: bool
343341
font_family: Literal["serif", "sans-serif", "monospace", "cursive", "fantasy"]
344342
font_name: str
345343
font_size: float
@@ -374,6 +372,8 @@ class TexConfig(TypedDict):
374372
marginpar_sep: Dimension
375373
num_columns: int
376374
columnsep: Dimension
375+
preamble: str
376+
preamble_substitute: bool
377377

378378

379379
class Config:
@@ -398,8 +398,6 @@ def __init__(self, load_file: bool = True) -> None:
398398
data={Path.cwd().resolve()}, pythonpath=set(), extra_imports=set()
399399
)
400400
self.pgfutils = dict(
401-
preamble="",
402-
preamble_substitute=False,
403401
font_family="serif",
404402
font_name="",
405403
font_size=10.0,
@@ -426,6 +424,8 @@ def __init__(self, load_file: bool = True) -> None:
426424
marginpar_sep=parse_dimension("11 points"),
427425
num_columns=1,
428426
columnsep=parse_dimension("10 points"),
427+
preamble="",
428+
preamble_substitute=False,
429429
)
430430

431431
if load_file:
@@ -762,8 +762,8 @@ def setup_figure(
762762
matplotlib.rcParams["pgf.texsystem"] = config.tex["engine"]
763763

764764
# Custom TeX preamble.
765-
preamble = config.pgfutils["preamble"]
766-
if config.pgfutils["preamble_substitute"]:
765+
preamble = config.tex["preamble"]
766+
if config.tex["preamble_substitute"]:
767767
preamble = string.Template(preamble).substitute(basedir=str(Path.cwd()))
768768
matplotlib.rcParams["pgf.preamble"] = preamble
769769

tests/sources/fonts/noconfig/custom_font.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

tests/sources/fonts/pgfutils.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
# SPDX-FileCopyrightText: Blair Bonnett
22
# SPDX-License-Identifier: BSD-3-Clause
33

4-
[pgfutils]
5-
font_family="serif"
6-
font_name="CothamSans"
4+
[tex]
75
preamble_substitute=true
86
preamble='''
97
\usepackage{fontspec}
@@ -12,3 +10,7 @@ preamble='''
1210
Extension=.otf
1311
]
1412
'''
13+
14+
[pgfutils]
15+
font_family="serif"
16+
font_name="CothamSans"

tests/test_fonts.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,3 @@ def test_custom_font():
1212
"""Test figure using a custom font with fontspec in config file..."""
1313
with build_pypgf(srcdir, "custom_font.py") as res:
1414
assert res.returncode == 0, "Failed to build tests/sources/fonts/custom_font.py"
15-
16-
17-
def test_custom_font_kwargs():
18-
"""Test figure using a custom font specified through kwargs..."""
19-
with build_pypgf(srcdir / "noconfig", "custom_font.py") as res:
20-
assert res.returncode == 0, (
21-
"Failed to build tests/sources/fonts/no_config/custom_font.py"
22-
)

tests/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def build_pypgf(figure_dir, filename, environment=None):
6767
# Generate the sub-environment.
6868
env = dict(os.environ)
6969
env["PYTHONPATH"] = ":".join(paths)
70+
env["PYTHONWARNINGS"] = "always:unknown settings::pgfutils"
7071
env.update(environment)
7172

7273
# Run the script.
@@ -84,6 +85,12 @@ def build_pypgf(figure_dir, filename, environment=None):
8485
sys.stderr.write(res.stderr)
8586
sys.stderr.flush()
8687

88+
# Check if unknown settings were reported and convert to an exception.
89+
for line in res.stderr.splitlines():
90+
if "UserWarning: unknown settings" in line:
91+
_, _, msg = line.partition("UserWarning: ")
92+
raise RuntimeError(msg)
93+
8794
# Pass the result to the user, and clean up afterwards.
8895
try:
8996
yield res

0 commit comments

Comments
 (0)