@@ -220,6 +220,8 @@ def _build_latex_header():
220
220
# Include TeX program name as a comment for cache invalidation.
221
221
# TeX does not allow this to be the first line.
222
222
r"% !TeX program = {}" .format (rcParams ["pgf.texsystem" ]),
223
+ # Test whether \includegraphics supports interpolate option.
224
+ r"\usepackage{graphicx}" ,
223
225
latex_preamble ,
224
226
latex_fontspec ,
225
227
r"\begin{document}" ,
@@ -376,6 +378,22 @@ def get_width_height_descent(self, text, prop):
376
378
return w , h + o , o
377
379
378
380
381
+ @functools .lru_cache (1 )
382
+ def _get_image_inclusion_command ():
383
+ man = LatexManager ._get_cached_or_new ()
384
+ man ._stdin_writeln (
385
+ r"\includegraphics[interpolate=true]{%s}"
386
+ # Don't mess with backslashes on Windows.
387
+ % cbook ._get_data_path ("images/matplotlib.png" ).as_posix ())
388
+ try :
389
+ prompt = man ._expect_prompt ()
390
+ return r"\includegraphics"
391
+ except LatexError :
392
+ # Discard the broken manager.
393
+ LatexManager ._get_cached_or_new_impl .cache_clear ()
394
+ return r"\pgfimage"
395
+
396
+
379
397
class RendererPgf (RendererBase ):
380
398
381
399
def __init__ (self , figure , fh , dummy = False ):
@@ -397,8 +415,7 @@ def __init__(self, figure, fh, dummy=False):
397
415
self .figure = figure
398
416
self .image_counter = 0
399
417
400
- # get LatexManager instance
401
- self .latexManager = LatexManager ._get_cached_or_new ()
418
+ self ._latexManager = LatexManager ._get_cached_or_new () # deprecated
402
419
403
420
if dummy :
404
421
# dummy==True deactivate all methods
@@ -413,6 +430,10 @@ def __init__(self, figure, fh, dummy=False):
413
430
"pgf-to-pdf option" , UserWarning )
414
431
self .__dict__ ["draw_image" ] = lambda * args , ** kwargs : None
415
432
433
+ @cbook .deprecated ("3.2" )
434
+ def latexManager (self ):
435
+ return self ._latexManager
436
+
416
437
def draw_markers (self , gc , marker_path , marker_trans , path , trans ,
417
438
rgbFace = None ):
418
439
# docstring inherited
@@ -660,8 +681,9 @@ def draw_image(self, gc, x, y, im, transform=None):
660
681
interp = str (transform is None ).lower () # interpolation in PDF reader
661
682
writeln (self .fh ,
662
683
r"\pgftext[left,bottom]"
663
- r"{\pgfimage[interpolate=%s,width=%fin,height=%fin]{%s}}" %
664
- (interp , w , h , fname_img ))
684
+ r"{%s[interpolate=%s,width=%fin,height=%fin]{%s}}" %
685
+ (_get_image_inclusion_command (),
686
+ interp , w , h , fname_img ))
665
687
writeln (self .fh , r"\end{pgfscope}" )
666
688
667
689
def draw_tex (self , gc , x , y , s , prop , angle , ismath = "TeX!" , mtext = None ):
@@ -724,7 +746,8 @@ def get_text_width_height_descent(self, s, prop, ismath):
724
746
s = common_texification (s )
725
747
726
748
# get text metrics in units of latex pt, convert to display units
727
- w , h , d = self .latexManager .get_width_height_descent (s , prop )
749
+ w , h , d = (LatexManager ._get_cached_or_new ()
750
+ .get_width_height_descent (s , prop ))
728
751
# TODO: this should be latex_pt_to_in instead of mpl_pt_to_in
729
752
# but having a little bit more space around the text looks better,
730
753
# plus the bounding box reported by LaTeX is VERY narrow
0 commit comments