32
32
from matplotlib .mathtext import MathTextParser
33
33
from matplotlib ._mathtext_data import uni2type1
34
34
from matplotlib .path import Path
35
+ from matplotlib .texmanager import TexManager
35
36
from matplotlib .transforms import Affine2D
36
37
from matplotlib .backends .backend_mixed import MixedModeRenderer
37
38
from . import _backend_pdf_ps
@@ -1238,67 +1239,32 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
1238
1239
commands to convert those tags to text. LaTeX/dvips produces the postscript
1239
1240
file that includes the actual text.
1240
1241
"""
1241
- tmpdir = os .path .split (tmpfile )[0 ]
1242
- epsfile = tmpfile + '.eps'
1243
- shutil .move (tmpfile , epsfile )
1244
- latexfile = tmpfile + '.tex'
1245
- dvifile = tmpfile + '.dvi'
1246
- psfile = tmpfile + '.ps'
1247
-
1248
- if orientation == 'landscape' :
1249
- angle = 90
1250
- else :
1251
- angle = 0
1252
-
1253
- if rcParams ['text.latex.unicode' ]:
1254
- unicode_preamble = """\\ usepackage{ucs}
1255
- \\ usepackage[utf8x]{inputenc}"""
1256
- else :
1257
- unicode_preamble = ''
1258
-
1259
- s = r"""\documentclass{article}
1260
- %s
1261
- %s
1262
- %s
1263
- \usepackage[
1264
- dvips, papersize={%sin,%sin}, body={%sin,%sin}, margin={0in,0in}]{geometry}
1265
- \usepackage{psfrag}
1266
- \usepackage[dvips]{graphicx}
1267
- \usepackage{color}
1268
- \pagestyle{empty}
1269
- \begin{document}
1270
- \begin{figure}
1271
- \centering
1272
- \leavevmode
1273
- %s
1274
- \includegraphics*[angle=%s]{%s}
1275
- \end{figure}
1276
- \end{document}
1277
- """ % (font_preamble , unicode_preamble , custom_preamble ,
1278
- paper_width , paper_height , paper_width , paper_height ,
1279
- '\n ' .join (psfrags ), angle , os .path .split (epsfile )[- 1 ])
1280
-
1281
- try :
1282
- pathlib .Path (latexfile ).write_text (
1283
- s , encoding = 'utf-8' if rcParams ['text.latex.unicode' ] else 'ascii' )
1284
- except UnicodeEncodeError :
1285
- _log .info ("You are using unicode and latex, but have not enabled the "
1286
- "Matplotlib 'text.latex.unicode' rcParam." )
1287
- raise
1288
-
1289
- # Replace \\ for / so latex does not think there is a function call
1290
- latexfile = latexfile .replace ("\\ " , "/" )
1291
- # Replace ~ so Latex does not think it is line break
1292
- latexfile = latexfile .replace ("~" , "\\ string~" )
1293
-
1294
- cbook ._check_and_log_subprocess (
1295
- ["latex" , "-interaction=nonstopmode" , '"%s"' % latexfile ],
1296
- _log , cwd = tmpdir )
1297
- cbook ._check_and_log_subprocess (
1298
- ['dvips' , '-q' , '-R0' , '-o' , os .path .basename (psfile ),
1299
- os .path .basename (dvifile )], _log , cwd = tmpdir )
1300
- os .remove (epsfile )
1301
- shutil .move (psfile , tmpfile )
1242
+ with mpl .rc_context ({
1243
+ "text.latex.preamble" :
1244
+ rcParams ["text.latex.preamble" ] +
1245
+ r"\usepackage{psfrag,color}"
1246
+ r"\usepackage[dvips]{graphicx}"
1247
+ r"\PassOptionsToPackage{dvips}{geometry}" }):
1248
+ dvifile = TexManager ().make_dvi (
1249
+ r"\newgeometry{papersize={%(width)sin,%(height)sin},"
1250
+ r"body={%(width)sin,%(height)sin}, margin={0in,0in}}" "\n "
1251
+ r"\begin{figure}"
1252
+ r"\centering\leavevmode%(psfrags)s"
1253
+ r"\includegraphics*[angle=%(angle)s]{%(epsfile)s}"
1254
+ r"\end{figure}"
1255
+ % {
1256
+ "width" : paper_width , "height" : paper_height ,
1257
+ "psfrags" : "\n " .join (psfrags ),
1258
+ "angle" : 90 if orientation == 'landscape' else 0 ,
1259
+ "epsfile" : pathlib .Path (tmpfile ).resolve ().as_posix (),
1260
+ },
1261
+ fontsize = 10 ) # tex's default fontsize.
1262
+
1263
+ with TemporaryDirectory () as tmpdir :
1264
+ psfile = os .path .join (tmpdir , "tmp.ps" )
1265
+ cbook ._check_and_log_subprocess (
1266
+ ['dvips' , '-q' , '-R0' , '-o' , psfile , dvifile ], _log )
1267
+ shutil .move (psfile , tmpfile )
1302
1268
1303
1269
# check if the dvips created a ps in landscape paper. Somehow,
1304
1270
# above latex+dvips results in a ps file in a landscape mode for a
@@ -1308,15 +1274,7 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
1308
1274
# information. The return value is used in pstoeps step to recover
1309
1275
# the correct bounding box. 2010-06-05 JJL
1310
1276
with open (tmpfile ) as fh :
1311
- if "Landscape" in fh .read (1000 ):
1312
- psfrag_rotated = True
1313
- else :
1314
- psfrag_rotated = False
1315
-
1316
- if not debugPS :
1317
- for fname in glob .glob (tmpfile + '.*' ):
1318
- os .remove (fname )
1319
-
1277
+ psfrag_rotated = "Landscape" in fh .read (1000 )
1320
1278
return psfrag_rotated
1321
1279
1322
1280
0 commit comments