Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7ccccc0

Browse files
committed
Dedupe texmanager preamble construction.
1 parent 76d5b43 commit 7ccccc0

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

lib/matplotlib/texmanager.py

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,22 @@ def get_custom_preamble(self):
192192
"""Return a string containing user additions to the tex preamble."""
193193
return rcParams['text.latex.preamble']
194194

195+
def _get_preamble(self):
196+
unicode_preamble = "\n".join([
197+
r"\usepackage[utf8]{inputenc}",
198+
r"\DeclareUnicodeCharacter{2212}{\ensuremath{-}}",
199+
]) if rcParams["text.latex.unicode"] else ""
200+
return "\n".join([
201+
r"\documentclass{article}",
202+
# Pass-through \mathdefault, which is used in non-usetex mode to
203+
# use the default text font but was historically suppressed in
204+
# usetex mode.
205+
r"\newcommand{\mathdefault}[1]{#1}",
206+
self._font_preamble,
207+
unicode_preamble,
208+
self.get_custom_preamble(),
209+
])
210+
195211
def make_tex(self, tex, fontsize):
196212
"""
197213
Generate a tex file to render the tex string at a specific font size.
@@ -200,30 +216,19 @@ def make_tex(self, tex, fontsize):
200216
"""
201217
basefile = self.get_basefile(tex, fontsize)
202218
texfile = '%s.tex' % basefile
203-
custom_preamble = self.get_custom_preamble()
204219
fontcmd = {'sans-serif': r'{\sffamily %s}',
205220
'monospace': r'{\ttfamily %s}'}.get(self.font_family,
206221
r'{\rmfamily %s}')
207222
tex = fontcmd % tex
208223

209-
unicode_preamble = "\n".join([
210-
r"\usepackage[utf8]{inputenc}",
211-
r"\DeclareUnicodeCharacter{2212}{\ensuremath{-}}",
212-
]) if rcParams["text.latex.unicode"] else ""
213-
214224
s = r"""
215-
\documentclass{article}
216-
\newcommand{\mathdefault}[1]{#1}
217-
%s
218-
%s
219225
%s
220226
\usepackage[papersize={72in,72in},body={70in,70in},margin={1in,1in}]{geometry}
221227
\pagestyle{empty}
222228
\begin{document}
223229
\fontsize{%f}{%f}%s
224230
\end{document}
225-
""" % (self._font_preamble, unicode_preamble, custom_preamble,
226-
fontsize, fontsize * 1.25, tex)
231+
""" % (self._get_preamble(), fontsize, fontsize * 1.25, tex)
227232
with open(texfile, 'wb') as fh:
228233
if rcParams['text.latex.unicode']:
229234
fh.write(s.encode('utf8'))
@@ -251,25 +256,15 @@ def make_tex_preview(self, tex, fontsize):
251256
"""
252257
basefile = self.get_basefile(tex, fontsize)
253258
texfile = '%s.tex' % basefile
254-
custom_preamble = self.get_custom_preamble()
255259
fontcmd = {'sans-serif': r'{\sffamily %s}',
256260
'monospace': r'{\ttfamily %s}'}.get(self.font_family,
257261
r'{\rmfamily %s}')
258262
tex = fontcmd % tex
259263

260-
unicode_preamble = "\n".join([
261-
r"\usepackage[utf8]{inputenc}",
262-
r"\DeclareUnicodeCharacter{2212}{\ensuremath{-}}",
263-
]) if rcParams["text.latex.unicode"] else ""
264-
265264
# newbox, setbox, immediate, etc. are used to find the box
266265
# extent of the rendered text.
267266

268267
s = r"""
269-
\documentclass{article}
270-
\newcommand{\mathdefault}[1]{#1}
271-
%s
272-
%s
273268
%s
274269
\usepackage[active,showbox,tightpage]{preview}
275270
\usepackage[papersize={72in,72in},body={70in,70in},margin={1in,1in}]{geometry}
@@ -284,8 +279,7 @@ def make_tex_preview(self, tex, fontsize):
284279
{\fontsize{%f}{%f}%s}
285280
\end{preview}
286281
\end{document}
287-
""" % (self._font_preamble, unicode_preamble, custom_preamble,
288-
fontsize, fontsize * 1.25, tex)
282+
""" % (self._get_preamble(), fontsize, fontsize * 1.25, tex)
289283
with open(texfile, 'wb') as fh:
290284
if rcParams['text.latex.unicode']:
291285
fh.write(s.encode('utf8'))

0 commit comments

Comments
 (0)