matplotlib / matplotlib/matplotlib

figure including align fails to output eps using savefig

Open
#11,233 12 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

backend: ps Difficulty: Hard keep status: confirmed bug topic: text/usetex
Dominant language
Python
Stars
23.2k
Forks
8.5k
Avg merge
1d 6h
Merged PRs (30d)
66

Description

### trouble with latex align and eps output

**png output works, eps breaks**

I have a figure that contains an align latex environment to stack two parameter definitions. Saving the figure as png is OK, while as eps gives a complicated Latex error.

```python
import matplotlib
import matplotlib.pyplot as plt

params= {'text.latex.preamble' : [r'\usepackage{amsmath}']}
plt.rcParams.update(params)

fig, ax = plt.subplots(figsize=(8, 6))
ax.text(0.5, 0.5, r'\begin{align*} H&: 2 \ \textrm{m} \\'\
+ r'\Lambda &: 40 \ \textrm{m} \end{align*}',
bbox=dict(boxstyle="square",
ec=(0.6, 0.6, 0.6),
fc=(1., 1., 1.),
),
fontsize=32)

plt.show(block=False)
# this works fine
#plt.savefig('test.png', format='png')
plt.savefig('test.eps', format='eps')
```

**Actual outcome**

```
---------------------------------------------------------------------------
CalledProcessError Traceback (most recent call last)
~/miniconda3/lib/python3.6/site-packages/matplotlib/backends/backend_ps.py in convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble, paperWidth, paperHeight, orientation)
1435 report = subprocess.check_output(command, cwd=tmpdir,
-> 1436 stderr=subprocess.STDOUT)
1437 except subprocess.CalledProcessError as exc:

~/miniconda3/lib/python3.6/subprocess.py in check_output(timeout, *popenargs, **kwargs)
335 return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
--> 336 **kwargs).stdout
337

~/miniconda3/lib/python3.6/subprocess.py in run(input, timeout, check, *popenargs, **kwargs)
417 raise CalledProcessError(retcode, process.args,
--> 418 output=stdout, stderr=stderr)
419 return CompletedProcess(process.args, retcode, stdout, stderr)

CalledProcessError: Command '['latex', '-interaction=nonstopmode', '"/tmp/tmpdnuve0l3.tex"']' returned non-zero exit status 1.

During handling of the above exception, another exception occurred:

RuntimeError Traceback (most recent call last)
~/python-workspace/icepyks/workspace/essay_plots/eps_bug.py in ()
21 plt.show(block=False)
22 #plt.savefig('test.png', format='png')
---> 23 plt.savefig('test.eps', format='eps')

~/miniconda3/lib/python3.6/site-packages/matplotlib/pyplot.py in savefig(*args, **kwargs)
708 def savefig(*args, **kwargs):
709 fig = gcf()
--> 710 res = fig.savefig(*args, **kwargs)
711 fig.canvas.draw_idle() # need this if 'transparent=True' to reset colors
712 return res

~/miniconda3/lib/python3.6/site-packages/matplotlib/figure.py in savefig(self, fname, **kwargs)
2033 self.set_frameon(frameon)
2034
-> 2035 self.canvas.print_figure(fname, **kwargs)
2036
2037 if frameon:

~/miniconda3/lib/python3.6/site-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
2259 orientation=orientation,
2260 bbox_inches_restore=_bbox_inches_restore,
-> 2261 **kwargs)
2262 finally:
2263 if bbox_inches and restore_bbox:

~/miniconda3/lib/python3.6/site-packages/matplotlib/backends/backend_ps.py in print_eps(self, outfile, *args, **kwargs)
929
930 def print_eps(self, outfile, *args, **kwargs):
--> 931 return self._print_ps(outfile, 'eps', *args, **kwargs)
932
933 def _print_ps(self, outfile, format, *args, **kwargs):

~/miniconda3/lib/python3.6/site-packages/matplotlib/backends/backend_ps.py in _print_ps(self, outfile, format, *args, **kwargs)
953 self._print_figure_tex(outfile, format, imagedpi, facecolor, edgecolor,
954 orientation, isLandscape, papertype,
--> 955 **kwargs)
956 else:
957 self._print_figure(outfile, format, imagedpi, facecolor, edgecolor,

~/miniconda3/lib/python3.6/site-packages/matplotlib/backends/backend_ps.py in _print_figure_tex(self, outfile, format, dpi, facecolor, edgecolor, orientation, isLandscape, papertype, metadata, **kwargs)
1349 custom_preamble, paperWidth,
1350 paperHeight,
-> 1351 orientation)
1352
1353 if (rcParams['ps.usedistiller'] == 'ghostscript'

~/miniconda3/lib/python3.6/site-packages/matplotlib/backends/backend_ps.py in convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble, paperWidth, paperHeight, orientation)
1441 'Here is the full report generated by LaTeX:\n%s '
1442 '\n\n' % (latexfile,
-> 1443 exc.output.decode("utf-8"))))
1444 _log.debug(report)
1445

RuntimeError: LaTeX was not able to process the following file:
/tmp/tmpdnuve0l3.tex

Here is the full report generated by LaTeX:
This is pdfTeX, Version 3.14159265-2.6-1.40.18 (TeX Live 2017) (preloaded format=latex)
restricted \write18 enabled.
entering extended mode
(/tmp/tmpdnuve0l3.tex
LaTeX2e <2017-04-15>
Babel <3.15> and hyphenation patterns for 84 language(s) loaded.
(/home/e2richards/texlive/2017/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/home/e2richards/texlive/2017/texmf-dist/tex/latex/base/size10.clo))
(/home/e2richards/texlive/2017/texmf-dist/tex/latex/type1cm/type1cm.sty)
(/home/e2richards/texlive/2017/texmf-dist/tex/latex/base/textcomp.sty
(/home/e2richards/texlive/2017/texmf-dist/tex/latex/base/ts1enc.def))
(/home/e2richards/texlive/2017/texmf-dist/tex/latex/amsmath/amsmath.sty
For additional information on amsmath, use the `?' option.
(/home/e2richards/texlive/2017/texmf-dist/tex/latex/amsmath/amstext.sty
(/home/e2richards/texlive/2017/texmf-dist/tex/latex/amsmath/amsgen.sty))
(/home/e2richards/texlive/2017/texmf-dist/tex/latex/amsmath/amsbsy.sty)
(/home/e2richards/texlive/2017/texmf-dist/tex/latex/amsmath/amsopn.sty))
(/home/e2richards/texlive/2017/texmf-dist/tex/latex/geometry/geometry.sty
(/home/e2richards/texlive/2017/texmf-dist/tex/latex/graphics/keyval.sty)
(/home/e2richards/texlive/2017/texmf-dist/tex/generic/oberdiek/ifpdf.sty)
(/home/e2richards/texlive/2017/texmf-dist/tex/generic/oberdiek/ifvtex.sty)
(/home/e2richards/texlive/2017/texmf-dist/tex/generic/ifxetex/ifxetex.sty)

Package geometry Warning: Over-specification in `h'-direction.
`width' (578.15999pt) is ignored.

Package geometry Warning: Over-specification in `v'-direction.
`height' (433.62pt) is ignored.

) (/home/e2richards/texlive/2017/texmf-dist/tex/latex/psfrag/psfrag.sty
(/home/e2richards/texlive/2017/texmf-dist/tex/latex/graphics/graphics.sty
(/home/e2richards/texlive/2017/texmf-dist/tex/latex/graphics/trig.sty)
(/home/e2richards/texlive/2017/texmf-dist/tex/latex/graphics-cfg/graphics.cfg)
(/home/e2richards/texlive/2017/texmf-dist/tex/latex/graphics-def/dvips.def)))
(/home/e2richards/texlive/2017/texmf-dist/tex/latex/graphics/graphicx.sty)
(/home/e2richards/texlive/2017/texmf-dist/tex/latex/graphics/color.sty
(/home/e2richards/texlive/2017/texmf-dist/tex/latex/graphics-cfg/color.cfg)
(/home/e2richards/texlive/2017/texmf-dist/tex/latex/graphics/dvipsnam.def))
No file tmpdnuve0l3.aux.
(/home/e2richards/texlive/2017/texmf-dist/tex/latex/base/ts1cmr.fd)
*geometry* detected driver: dvips

LaTeX Font Warning: Font shape `OMX/cmex/m/n' in size <26> not available
(Font) size <24.88> substituted on input line 28.

LaTeX Font Warning: Font shape `OMX/cmex/m/n' in size <18.19992> not available
(Font) size <17.28> substituted on input line 28.

LaTeX Font Warning: Font shape `OMX/cmex/m/n' in size <13> not available
(Font) size <12> substituted on input line 28.

LaTeX Font Warning: Font shape `OMX/cmex/m/n' in size <32> not available
(Font) size <24.88> substituted on input line 28.

LaTeX Font Warning: Font shape `OMX/cmex/m/n' in size <22.3999> not available
(Font) size <20.74> substituted on input line 28.

LaTeX Font Warning: Font shape `OMX/cmex/m/n' in size <16> not available
(Font) size <17.28> substituted on input line 28.

! Missing \endgroup inserted.

\endgroup
l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Missing } inserted.

}
l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Missing \endgroup inserted.

\endgroup
l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Missing } inserted.

}
l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! LaTeX Error: There's no line here to end.

See the LaTeX manual or LaTeX Companion for explanation.
Type H for immediate help.
...

l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Missing $ inserted.

$
l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Missing } inserted.

}
l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Missing } inserted.

}
l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Missing } inserted.

}
l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Missing \cr inserted.

\cr
l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Missing { inserted.

{
l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Missing { inserted.

{
l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Extra }, or forgotten $.
}

l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Missing $ inserted.

$
l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Missing } inserted.

}
l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Missing $ inserted.

$
l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Misplaced alignment tab character &.
H&: 2 \ \textrm {m} \\\Lambda &
: 40 \ \textrm {m}
l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Misplaced \cr.
\math@cr@@@ ->\cr

l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Misplaced \noalign.
\math@cr@@ ... \iffalse }\fi \math@cr@@@ \noalign
{\vskip #1\relax }
l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Missing $ inserted.

$
l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Missing } inserted.

}
l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Extra }, or forgotten \endgroup.
\math@cr@@ ...th@cr@@@ \noalign {\vskip #1\relax }

l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Misplaced \noalign.
\black@ #1->\noalign
{\ifdim #1>\displaywidth \dimen@ \prevdepth \nointerlin...
l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

Overfull \hbox (118.28842pt too wide) detected at line 28
[]
! Extra }, or forgotten \endgroup.
\endalign ->\math@cr \black@ \totwidth@ \egroup
\ifingather@ \restorealignst...
l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! LaTeX Error: \begin{figure} on input line 15 ended by \end{align*}.

See the LaTeX manual or LaTeX Companion for explanation.
Type H for immediate help.
...

l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Missing $ inserted.

$
l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Display math should end with $$.

\endgroup
l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Missing } inserted.

}
l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Missing } inserted.

}
l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Extra }, or forgotten \endgroup.
\Gin@erotate ...angle {#1}\setbox \z@ \hbox {{#2}}
\Grot@x \z@ \Grot@y \z@ \G...
l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Undefined control sequence.
\Grot@angle

l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Missing number, treated as zero.

)
l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Undefined control sequence.
\Grot@angle

l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Missing number, treated as zero.

)
l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Undefined control sequence.
\Grot@angle

l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Missing number, treated as zero.

)
l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Undefined control sequence.
\Grot@angle

l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Missing number, treated as zero.

)
l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Undefined control sequence.
\Grot@start ...currentpoint translate \Grot@angle
\GPT@space neg rotate neg ...
l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Extra }, or forgotten \endgroup.
\mbox #1->\leavevmode \hbox {#1}

l.28 \includegraphics*[angle=0]{tmpdnuve0l3.eps}

! Too many }'s.
\@endfloatbox ...pagefalse \outer@nobreak \egroup
\color@endbox
l.29 \end{figure}

! Extra \endgroup.
\color@endgroup ->\endgraf \endgroup

l.29 \end{figure}

! Too many }'s.
\color@endbox ->\color@endgroup \egroup

l.29 \end{figure}

! LaTeX Error: \begin{document} ended by \end{figure}.

See the LaTeX manual or LaTeX Companion for explanation.
Type H for immediate help.
...

l.29 \end{figure}

! Extra \endgroup.
\endgroup

l.29 \end{figure}

[1] (./tmpdnuve0l3.aux)

LaTeX Font Warning: Size substitutions with differences
(Font) up to 7.12pt have occurred.

)
(see the transcript file for additional information)
Output written on tmpdnuve0l3.dvi (1 page, 456 bytes).
Transcript written on tmpdnuve0l3.log.

```
**Matplotlib version**

* Operating system: RedHatEnterpriseServer 7.3
* Matplotlib version: '2.2.0'
* Matplotlib backend (`print(matplotlib.get_backend())`): TkAgg
* Python version:Python 3.6.4 |Anaconda, Inc.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the provided Python example with the EPS save path, then inspect matplotlib/backends/backend_ps.py, especially print_eps, _print_figure_tex, and convert_psfrags. Compare the LaTeX generated for the align text with the PNG path. Done means the example saves an EPS file without the reported LaTeX errors while preserving the displayed aligned text.

Written by the indexing model from the issue text.

Assessment

Tech stack
latex, python
Domain
backend, data-visualization
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.