From 6d83289cddd959e679c8e2d6180197aa8bf45f93 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Thu, 26 Mar 2020 12:25:47 +0100 Subject: [PATCH] Fix use of psfrags in ps backend + usetex. Looks like psfrags interacts badly with `\newgeometry`, so we need to use `\geometry` instead in the custom preamble to set the paper size. In turn this means that `\usepackage{geometry}` needs to move to before the custom preamble into `TexManager._get_preamble()`, and thus we can't pass the dvips option to it anymore, but fortunately dvips is documented as the default driver anyways (section 5.6 of `geometry` docs). Also, in order to keep everything under 79 characters wide, change `{72in,72in}` to just `72in` (which is synonymous per section 4.3.3 of `geometry` docs). Also insert some extra newlines in the generated tex file for ease of debugging. --- lib/matplotlib/backends/backend_ps.py | 20 +++++++++++--------- lib/matplotlib/texmanager.py | 5 +++-- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/matplotlib/backends/backend_ps.py b/lib/matplotlib/backends/backend_ps.py index 958ad6204210..a20abe56f621 100644 --- a/lib/matplotlib/backends/backend_ps.py +++ b/lib/matplotlib/backends/backend_ps.py @@ -1098,18 +1098,20 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble, with mpl.rc_context({ "text.latex.preamble": mpl.rcParams["text.latex.preamble"] + - r"\usepackage{psfrag,color}" - r"\usepackage[dvips]{graphicx}" - r"\PassOptionsToPackage{dvips}{geometry}"}): + r"\usepackage{psfrag,color}""\n" + r"\usepackage[dvips]{graphicx}""\n" + r"\geometry{papersize={%(width)sin,%(height)sin}," + r"body={%(width)sin,%(height)sin},margin=0in}" + % {"width": paper_width, "height": paper_height} + }): dvifile = TexManager().make_dvi( - r"\newgeometry{papersize={%(width)sin,%(height)sin}," - r"body={%(width)sin,%(height)sin}, margin={0in,0in}}""\n" - r"\begin{figure}" - r"\centering\leavevmode%(psfrags)s" - r"\includegraphics*[angle=%(angle)s]{%(epsfile)s}" + "\n" + r"\begin{figure}""\n" + r" \centering\leavevmode""\n" + r" %(psfrags)s""\n" + r" \includegraphics*[angle=%(angle)s]{%(epsfile)s}""\n" r"\end{figure}" % { - "width": paper_width, "height": paper_height, "psfrags": "\n".join(psfrags), "angle": 90 if orientation == 'landscape' else 0, "epsfile": pathlib.Path(tmpfile).resolve().as_posix(), diff --git a/lib/matplotlib/texmanager.py b/lib/matplotlib/texmanager.py index f954254adc9e..e274a7f7e662 100644 --- a/lib/matplotlib/texmanager.py +++ b/lib/matplotlib/texmanager.py @@ -186,6 +186,9 @@ def _get_preamble(self): self._font_preamble, r"\usepackage[utf8]{inputenc}", r"\DeclareUnicodeCharacter{2212}{\ensuremath{-}}", + # Needs to come early so that the custom preamble can change the + # geometry, e.g. in convert_psfrags. + r"\usepackage[papersize=72in,body=70in,margin=1in]{geometry}", self.get_custom_preamble(), ]) @@ -204,7 +207,6 @@ def make_tex(self, tex, fontsize): Path(texfile).write_text( r""" %s -\usepackage[papersize={72in,72in},body={70in,70in},margin={1in,1in}]{geometry} \pagestyle{empty} \begin{document} \fontsize{%f}{%f}%s @@ -239,7 +241,6 @@ def make_tex_preview(self, tex, fontsize): r""" %s \usepackage[active,showbox,tightpage]{preview} -\usepackage[papersize={72in,72in},body={70in,70in},margin={1in,1in}]{geometry} %% we override the default showbox as it is treated as an error and makes %% the exit status not zero