8000 Reuse TexManager implementation in convert_psfrags. · matplotlib/matplotlib@eb8dac9 · GitHub
[go: up one dir, main page]

Skip to content

Commit eb8dac9

Browse files
committed
Reuse TexManager implementation in convert_psfrags.
This allows convert_psfrags to also benefit from improvements to texmanager, such as the recent addition of support for unicode minus.
1 parent 9e7a235 commit eb8dac9

File tree

1 file changed

+24
-54
lines changed

1 file changed

+24
-54
lines changed

lib/matplotlib/backends/backend_ps.py

Lines changed: 24 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from matplotlib.mathtext import MathTextParser
3232
from matplotlib._m 10000 athtext_data import uni2type1
3333
from matplotlib.path import Path
34+
from matplotlib.texmanager import TexManager
3435
from matplotlib.transforms import Affine2D
3536
from matplotlib.backends.backend_mixed import MixedModeRenderer
3637
from . import _backend_pdf_ps
@@ -1263,65 +1264,37 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
12631264
file that includes the actual text.
12641265
"""
12651266
tmpdir = os.path.split(tmpfile)[0]
1266-
epsfile = tmpfile+'.eps'
1267-
shutil.move(tmpfile, epsfile)
1268-
latexfile = tmpfile+'.tex'
1269-
dvifile = tmpfile+'.dvi'
1270-
psfile = tmpfile+'.ps'
1267+
psfile = tmpfile + '.ps'
12711268

12721269
if orientation == 'landscape':
12731270
angle = 90
12741271
else:
12751272
angle = 0
12761273

1277-
if rcParams['text.latex.unicode']:
1278-
unicode_preamble = """\\usepackage{ucs}
1279-
\\usepackage[utf8x]{inputenc}"""
1280-
else:
1281-
unicode_preamble = ''
1282-
1283-
s = r"""\documentclass{article}
1284-
%s
1285-
%s
1286-
%s
1287-
\usepackage[
1288-
dvips, papersize={%sin,%sin}, body={%sin,%sin}, margin={0in,0in}]{geometry}
1289-
\usepackage{psfrag}
1290-
\usepackage[dvips]{graphicx}
1291-
\usepackage{color}
1292-
\pagestyle{empty}
1293-
\begin{document}
1294-
\begin{figure}
1295-
\centering
1296-
\leavevmode
1297-
%s
1298-
\includegraphics*[angle=%s]{%s}
1299-
\end{figure}
1300-
\end{document}
1301-
""" % (font_preamble, unicode_preamble, custom_preamble,
1302-
paper_width, paper_height, paper_width, paper_height,
1303-
'\n'.join(psfrags), angle, os.path.split(epsfile)[-1])
1304-
1305-
try:
1306-
pathlib.Path(latexfile).write_text(
1307-
s, encoding='utf-8' if rcParams['text.latex.unicode'] else 'ascii')
1308-
except UnicodeEncodeError:
1309-
_log.info("You are using unicode and latex, but have not enabled the "
1310-
"Matplotlib 'text.latex.unicode' rcParam.")
1311-
raise
1312-
1313-
# Replace \\ for / so latex does not think there is a function call
1314-
latexfile = latexfile.replace("\\", "/")
1315-
# Replace ~ so Latex does not think it is line break
1316-
latexfile = latexfile.replace("~", "\\string~")
1274+
with mpl.rc_context({
1275+
"text.latex.preamble":
1276+
rcParams["text.latex.preamble"] +
1277+
r"\usepackage{psfrag,color}"
1278+
r"\usepackage[dvips]{graphicx}"
1279+
r"\PassOptionsToPackage{dvips}{geometry}"}):
1280+
dvifile = TexManager().make_dvi(
1281+
r"\newgeometry{papersize={%(width)sin,%(height)sin},"
1282+
r"body={%(width)sin,%(height)sin}, margin={0in,0in}}""\n"
1283+
r"\begin{figure}"
1284+
r"\centering\leavevmode%(psfrags)s"
1285+
r"\includegraphics*[angle=%(angle)s]{%(epsfile)s}"
1286+
r"\end{figure}"
1287+
% {
1288+
"width": paper_width, "height": paper_height,
1289+
"psfrags": "\n".join(psfrags),
1290+
"angle": angle,
1291+
"epsfile": pathlib.Path(tmpfile).resolve().as_posix(),
1292+
},
1293+
fontsize=10) # tex's default fontsize.
13171294

13181295
cbook._check_and_log_subprocess(
1319-
["latex", "-interaction=nonstopmode", '"%s"' % latexfile],
1296+
['dvips', '-q', '-R0', '-o', os.path.basename(psfile), dvifile],
13201297
_log, cwd=tmpdir)
1321-
cbook._check_and_log_subprocess(
1322 9B5B -
['dvips', '-q', '-R0', '-o', os.path.basename(psfile),
1323-
os.path.basename(dvifile)], _log, cwd=tmpdir)
1324-
os.remove(epsfile)
13251298
shutil.move(psfile, tmpfile)
13261299

13271300
# check if the dvips created a ps in landscape paper. Somehow,
@@ -1332,10 +1305,7 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
13321305
# information. The return value is used in pstoeps step to recover
13331306
# the correct bounding box. 2010-06-05 JJL
13341307
with open(tmpfile) as fh:
1335-
if "Landscape" in fh.read(1000):
1336-
psfrag_rotated = True
1337-
else:
1338-
psfrag_rotated = False
1308+
psfrag_rotated = "Landscape" in fh.read(1000):
13391309

13401310
if not debugPS:
13411311
for fname in glob.glob(tmpfile+'.*'):

0 commit comments

Comments
 (0)
0