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

Skip to content

Commit 0bffe21

Browse files
committed
Pick TeX cache name based on entire TeX source.
This allows invalidating the cache when the source generation algorithm changes.
1 parent bb2924c commit 0bffe21

File tree

1 file changed

+20
-28
lines changed

1 file changed

+20
-28
lines changed

lib/matplotlib/texmanager.py

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,9 @@ def get_basefile(self, tex, fontsize, dpi=None):
161161
"""
162162
Return a filename based on a hash of the string, fontsize, and dpi.
163163
"""
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)
166165
return os.path.join(
167-
self.texcache, hashlib.md5(s.encode('utf-8')).hexdigest())
166+
self.texcache, hashlib.md5(src.encode('utf-8')).hexdigest())
168167

169168
def get_font_preamble(self):
170169
"""
@@ -176,7 +175,13 @@ def get_custom_preamble(self):
176175
"""Return a string containing user additions to the tex preamble."""
177176
return rcParams['text.latex.preamble']
178177

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')
180185
return "\n".join([
181186
r"\documentclass{article}",
182187
# Pass-through \mathdefault, which is used in non-usetex mode to
@@ -196,6 +201,15 @@ def _get_preamble(self):
196201
# Custom packages (e.g. newtxtext) may already have loaded textcomp
197202
# with different options.
198203
_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}",
199213
])
200214

201215
def make_tex(self, tex, fontsize):
@@ -204,30 +218,8 @@ def make_tex(self, tex, fontsize):
204218
205219
Return the file name.
206220
"""
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))
231223
return texfile
232224

233225
def _run_checked_subprocess(self, command, tex, *, cwd=None):

0 commit comments

Comments
 (0)