Skip to content

Commit 979bd7d

Browse files
committed
Factor out latex ifpackageloaded pattern.
... which avoids having to explain twice its point, as well.
1 parent 01fb7bb commit 979bd7d

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

lib/matplotlib/backends/backend_ps.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,13 +1150,9 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
11501150
with mpl.rc_context({
11511151
"text.latex.preamble":
11521152
mpl.rcParams["text.latex.preamble"] +
1153-
# Only load these packages if they have not already been loaded, in
1154-
# order not to clash with custom packages.
1155-
r"\makeatletter"
1156-
r"\@ifpackageloaded{color}{}{\usepackage{color}}"
1157-
r"\@ifpackageloaded{graphicx}{}{\usepackage{graphicx}}"
1158-
r"\@ifpackageloaded{psfrag}{}{\usepackage{psfrag}}"
1159-
r"\makeatother"
1153+
mpl.texmanager._usepackage_if_not_loaded("color") +
1154+
mpl.texmanager._usepackage_if_not_loaded("graphicx") +
1155+
mpl.texmanager._usepackage_if_not_loaded("psfrag") +
11601156
r"\geometry{papersize={%(width)sin,%(height)sin},margin=0in}"
11611157
% {"width": paper_width, "height": paper_height}
11621158
}):

lib/matplotlib/texmanager.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,23 @@
3737
_log = logging.getLogger(__name__)
3838

3939

40+
def _usepackage_if_not_loaded(package, *, option=None):
41+
"""
42+
Output LaTeX code that loads a package (possibly with an option) if it
43+
hasn't been loaded yet.
44+
45+
LaTeX cannot load twice a package with different options, so this helper
46+
can be used to protect against users loading arbitrary packages/options in
47+
their custom preamble.
48+
"""
49+
option = f"[{option}]" if option is not None else ""
50+
return (
51+
r"\makeatletter"
52+
r"\@ifpackageloaded{%(package)s}{}{\usepackage%(option)s{%(package)s}}"
53+
r"\makeatother"
54+
) % {"package": package, "option": option}
55+
56+
4057
class TexManager:
4158
"""
4259
Convert strings to dvi files using TeX, caching the results to a directory.
@@ -176,16 +193,10 @@ def _get_preamble(self):
176193
self.get_custom_preamble(),
177194
# Use `underscore` package to take care of underscores in text
178195
# The [strings] option allows to use underscores in file names
179-
r"\makeatletter"
180-
r"\@ifpackageloaded{underscore}{}"
181-
r"{\usepackage[strings]{underscore}}"
182-
r"\makeatother",
183-
# textcomp is loaded last (if not already loaded by the custom
184-
# preamble) in order not to clash with custom packages (e.g.
185-
# newtxtext) which load it with different options.
186-
r"\makeatletter"
187-
r"\@ifpackageloaded{textcomp}{}{\usepackage{textcomp}}"
188-
r"\makeatother",
196+
_usepackage_if_not_loaded("underscore", option="strings"),
197+
# Custom packages (e.g. newtxtext) may already have loaded textcomp
198+
# with different options.
199+
_usepackage_if_not_loaded("textcomp"),
189200
])
190201

191202
def make_tex(self, tex, fontsize):

0 commit comments

Comments
 (0)