Skip to content

Commit 044f3d0

Browse files
authored
Merge pull request matplotlib#20767 from anntzer/ipl
2 parents 471b914 + 979bd7d commit 044f3d0

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
@@ -1137,13 +1137,9 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
11371137
with mpl.rc_context({
11381138
"text.latex.preamble":
11391139
mpl.rcParams["text.latex.preamble"] +
1140-
# Only load these packages if they have not already been loaded, in
1141-
# order not to clash with custom packages.
1142-
r"\makeatletter"
1143-
r"\@ifpackageloaded{color}{}{\usepackage{color}}"
1144-
r"\@ifpackageloaded{graphicx}{}{\usepackage{graphicx}}"
1145-
r"\@ifpackageloaded{psfrag}{}{\usepackage{psfrag}}"
1146-
r"\makeatother"
1140+
mpl.texmanager._usepackage_if_not_loaded("color") +
1141+
mpl.texmanager._usepackage_if_not_loaded("graphicx") +
1142+
mpl.texmanager._usepackage_if_not_loaded("psfrag") +
11471143
r"\geometry{papersize={%(width)sin,%(height)sin},margin=0in}"
11481144
% {"width": paper_width, "height": paper_height}
11491145
}):

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)