@@ -187,6 +187,7 @@ def __init__(self, message, latex_output=""):
187
187
self .latex_output = latex_output
188
188
189
189
190
+ @cbook .deprecated ("3.1" )
190
191
class LatexManagerFactory :
191
192
previous_instance = None
192
193
@@ -223,14 +224,31 @@ def _build_latex_header():
223
224
# Create LaTeX header with some content, else LaTeX will load some math
224
225
# fonts later when we don't expect the additional output on stdout.
225
226
# TODO: is this sufficient?
226
- latex_header = [r"\documentclass{minimal}" ,
227
- latex_preamble ,
228
- latex_fontspec ,
229
- r"\begin{document}" ,
230
- r"text $math \mu$" , # force latex to load fonts now
231
- r"\typeout{pgf_backend_query_start}" ]
227
+ latex_header = [
228
+ # Include TeX program name as a comment for cache invalidation.
229
+ r"% !TeX program = {}" .format (rcParams ["pgf.texsystem" ]),
230
+ r"\documentclass{minimal}" ,
231
+ latex_preamble ,
232
+ latex_fontspec ,
233
+ r"\begin{document}" ,
234
+ r"text $math \mu$" , # force latex to load fonts now
235
+ r"\typeout{pgf_backend_query_start}" ,
236
+ ]
232
237
return "\n " .join (latex_header )
233
238
239
+ @classmethod
240
+ def _get_cached_or_new (cls ):
241
+ """
242
+ Return the previous LatexManager if the header and tex system did not
243
+ change, or a new instance otherwise.
244
+ """
245
+ return cls ._get_cached_or_new_impl (cls ._build_latex_header ())
246
+
247
+ @functools .lru_cache (1 )
248
+ @classmethod
249
+ def _get_cached_or_new_impl (cls , header ): # Helper for _get_cached_or_new.
250
+ return cls ()
251
+
234
252
@staticmethod
235
253
def _cleanup_remaining_instances ():
236
254
unclean_instances = list (LatexManager ._unclean_instances )
@@ -388,7 +406,7 @@ def __init__(self, figure, fh, dummy=False):
388
406
self .image_counter = 0
389
407
390
408
# get LatexManager instance
391
- self .latexManager = LatexManagerFactory . get_latex_manager ()
409
+ self .latexManager = LatexManager . _get_cached_or_new ()
392
410
393
411
if dummy :
394
412
# dummy==True deactivate all methods
0 commit comments