@@ -161,10 +161,9 @@ def get_basefile(self, tex, fontsize, dpi=None):
161
161
"""
162
162
Return a filename based on a hash of the string, fontsize, and dpi.
163
163
"""
164
- s = '' .join ([tex , self .get_font_config (), '%f' % fontsize ,
165
- self .get_custom_preamble (), str (dpi or '' )])
164
+ src = self ._get_tex_source (tex , fontsize ) + str (dpi )
166
165
return os .path .join (
167
- self .texcache , hashlib .md5 (s .encode ('utf-8' )).hexdigest ())
166
+ self .texcache , hashlib .md5 (src .encode ('utf-8' )).hexdigest ())
168
167
169
168
def get_font_preamble (self ):
170
169
"""
@@ -176,7 +175,13 @@ def get_custom_preamble(self):
176
175
"""Return a string containing user additions to the tex preamble."""
177
176
return rcParams ['text.latex.preamble' ]
178
177
179
- def _get_preamble (self ):
178
+ def _get_tex_source (self , tex , fontsize ):
179
+ """Return the complete TeX source for processing a TeX string."""
180
+ self .get_font_config () # Updates self._font_preamble.
181
+ baselineskip = 1.25 * fontsize
182
+ fontcmd = (r'\sffamily' if self ._font_family == 'sans-serif' else
183
+ r'\ttfamily' if self ._font_family == 'monospace' else
184
+ r'\rmfamily' )
180
185
return "\n " .join ([
181
186
r"\documentclass{article}" ,
182
187
# Pass-through \mathdefault, which is used in non-usetex mode to
@@ -196,6 +201,15 @@ def _get_preamble(self):
196
201
# Custom packages (e.g. newtxtext) may already have loaded textcomp
197
202
# with different options.
198
203
_usepackage_if_not_loaded ("textcomp" ),
204
+ r"\pagestyle{empty}" ,
205
+ r"\begin{document}" ,
206
+ r"% The empty hbox ensures that a page is printed even for empty " ,
207
+ r"% inputs, except when using psfrag which gets confused by it." ,
208
+ r"\fontsize{%f}{%f}%%" % (fontsize , baselineskip ),
209
+ r"\ifdefined\psfrag\else\hbox{}\fi%" ,
210
+ r"{\obeylines%s %s}\special{matplotlibbaselinemarker}"
211
+ % (fontcmd , tex ),
212
+ r"\end{document}" ,
199
213
])
200
214
201
215
def make_tex (self , tex , fontsize ):
@@ -204,30 +218,8 @@ def make_tex(self, tex, fontsize):
204
218
205
219
Return the file name.
206
220
"""
207
- basefile = self .get_basefile (tex , fontsize )
208
- texfile = '%s.tex' % basefile
209
- fontcmd = (r'\sffamily' if self ._font_family == 'sans-serif' else
210
- r'\ttfamily' if self ._font_family == 'monospace' else
211
- r'\rmfamily' )
212
- tex_template = r"""
213
- %(preamble)s
214
- \pagestyle{empty}
215
- \begin{document}
216
- %% The empty hbox ensures that a page is printed even for empty inputs, except
217
- %% when using psfrag which gets confused by it.
218
- \fontsize{%(fontsize)f}{%(baselineskip)f}%%
219
- \ifdefined\psfrag\else\hbox{}\fi%%
220
- {\obeylines%(fontcmd)s %(tex)s}\special{matplotlibbaselinemarker}
221
- \end{document}
222
- """
223
- Path (texfile ).write_text (tex_template % {
224
- "preamble" : self ._get_preamble (),
225
- "fontsize" : fontsize ,
226
- "baselineskip" : fontsize * 1.25 ,
227
- "fontcmd" : fontcmd ,
228
- "tex" : tex ,
229
- }, encoding = "utf-8" )
230
-
221
+ texfile = self .get_basefile (tex , fontsize ) + ".tex"
222
+ Path (texfile ).write_text (self ._get_tex_source (tex , fontsize ))
231
223
return texfile
232
224
233
225
def _run_checked_subprocess (self , command , tex , * , cwd = None ):
0 commit comments