@@ -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,26 +175,44 @@ 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
- # Pass-through \mathdefault, which is used in non-usetex mode to
183
- # use the default text font but was historically suppressed in
184
- # usetex mode.
187
+ r"% Pass-through \mathdefault, which is used in non-usetex mode" ,
188
+ r"% to use the default text font but was historically suppressed" ,
189
+ r"% in usetex mode." ,
185
190
r"\newcommand{\mathdefault}[1]{#1}" ,
186
191
self ._font_preamble ,
187
192
r"\usepackage[utf8]{inputenc}" ,
188
193
r"\DeclareUnicodeCharacter{2212}{\ensuremath{-}}" ,
189
- # geometry is loaded before the custom preamble as convert_psfrags
190
- # relies on a custom preamble to change the geometry.
194
+ r"% geometry is loaded before the custom preamble as " ,
195
+ r"% convert_psfrags relies on a custom preamble to change the " ,
196
+ r"% geometry." ,
191
197
r"\usepackage[papersize=72in, margin=1in]{geometry}" ,
192
198
self .get_custom_preamble (),
193
- # Use `underscore` package to take care of underscores in text
194
- # The [strings] option allows to use underscores in file names
199
+ r"% Use `underscore` package to take care of underscores in text." ,
200
+ r"% The [strings] option allows to use underscores in file names." ,
195
201
_usepackage_if_not_loaded ("underscore" , option = "strings" ),
196
- # Custom packages (e.g. newtxtext) may already have loaded textcomp
197
- # with different options.
202
+ r"% Custom packages (e.g. newtxtext) may already have loaded " ,
203
+ r"% textcomp with different options." ,
198
204
_usepackage_if_not_loaded ("textcomp" ),
205
+ r"\pagestyle{empty}" ,
206
+ r"\begin{document}" ,
207
+ r"% The empty hbox ensures that a page is printed even for empty" ,
208
+ r"% inputs, except when using psfrag which gets confused by it." ,
209
+ r"% matplotlibbaselinemarker is used by dviread to detect the" ,
210
+ r"% last line's baseline." ,
211
+ rf"\fontsize{{{ fontsize } }}{{{ baselineskip } }}%" ,
212
+ r"\ifdefined\psfrag\else\hbox{}\fi%" ,
213
+ rf"{{\obeylines{ fontcmd } { tex } }}%" ,
214
+ r"\special{matplotlibbaselinemarker}%" ,
215
+ r"\end{document}" ,
199
216
])
200
217
201
218
def make_tex (self , tex , fontsize ):
@@ -204,30 +221,8 @@ def make_tex(self, tex, fontsize):
204
221
205
222
Return the file name.
206
223
"""
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
10545
- \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
- {%(fontcmd)s %(tex)s}
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
-
224
+ texfile = self .get_basefile (tex , fontsize ) + ".tex"
225
+ Path (texfile ).write_text (self ._get_tex_source (tex , fontsize ))
231
226
return texfile
232
227
233
228
def _run_checked_subprocess (self , command , tex , * , cwd = None ):
0 commit comments