When we want to use the LaTeX backend with postscript, we write PSFrag tags to a temporary postscript file, each one marking a position for LaTeX to render some text. convert_psfrags generates a LaTeX document containing the commands to convert those tags to text. LaTeX/dvips produc
(tmppath, psfrags, paper_width, paper_height, orientation)
| 1234 | |
| 1235 | |
| 1236 | def _convert_psfrags(tmppath, psfrags, paper_width, paper_height, orientation): |
| 1237 | """ |
| 1238 | When we want to use the LaTeX backend with postscript, we write PSFrag tags |
| 1239 | to a temporary postscript file, each one marking a position for LaTeX to |
| 1240 | render some text. convert_psfrags generates a LaTeX document containing the |
| 1241 | commands to convert those tags to text. LaTeX/dvips produces the postscript |
| 1242 | file that includes the actual text. |
| 1243 | """ |
| 1244 | with mpl.rc_context({ |
| 1245 | "text.latex.preamble": |
| 1246 | mpl.rcParams["text.latex.preamble"] + |
| 1247 | mpl.texmanager._usepackage_if_not_loaded("color") + |
| 1248 | mpl.texmanager._usepackage_if_not_loaded("graphicx") + |
| 1249 | mpl.texmanager._usepackage_if_not_loaded("psfrag") + |
| 1250 | r"\geometry{papersize={%(width)sin,%(height)sin},margin=0in}" |
| 1251 | % {"width": paper_width, "height": paper_height} |
| 1252 | }): |
| 1253 | dvifile = TexManager().make_dvi( |
| 1254 | "\n" |
| 1255 | r"\begin{figure}""\n" |
| 1256 | r" \centering\leavevmode""\n" |
| 1257 | r" %(psfrags)s""\n" |
| 1258 | r" \includegraphics*[angle=%(angle)s]{%(epsfile)s}""\n" |
| 1259 | r"\end{figure}" |
| 1260 | % { |
| 1261 | "psfrags": "\n".join(psfrags), |
| 1262 | "angle": 90 if orientation == 'landscape' else 0, |
| 1263 | "epsfile": tmppath.resolve().as_posix(), |
| 1264 | }, |
| 1265 | fontsize=10) # tex's default fontsize. |
| 1266 | |
| 1267 | with TemporaryDirectory() as tmpdir: |
| 1268 | psfile = os.path.join(tmpdir, "tmp.ps") |
| 1269 | # -R1 is a security flag used to prevent shell command execution |
| 1270 | cbook._check_and_log_subprocess( |
| 1271 | ['dvips', '-q', '-R1', '-o', psfile, dvifile], _log) |
| 1272 | shutil.move(psfile, tmppath) |
| 1273 | |
| 1274 | # check if the dvips created a ps in landscape paper. Somehow, |
| 1275 | # above latex+dvips results in a ps file in a landscape mode for a |
| 1276 | # certain figure sizes (e.g., 8.3in, 5.8in which is a5). And the |
| 1277 | # bounding box of the final output got messed up. We check see if |
| 1278 | # the generated ps file is in landscape and return this |
| 1279 | # information. The return value is used in pstoeps step to recover |
| 1280 | # the correct bounding box. 2010-06-05 JJL |
| 1281 | with open(tmppath) as fh: |
| 1282 | psfrag_rotated = "Landscape" in fh.read(1000) |
| 1283 | return psfrag_rotated |
| 1284 | |
| 1285 | |
| 1286 | def _try_distill(func, tmppath, *args, **kwargs): |
no test coverage detected
searching dependent graphs…