Repository navigation
Closed
Labels
Description
Saving vector graphic figures with certain characters in LaTeX will fail. Here I tried to use the \textmu character. Could it be related to #8068?
Code for reproduction
Here I'm saving to PostScript. It gives an error that I think is the most informative and points to some problems with ghostscript. Outputs for SVG and PDF below.
import matplotlib
matplotlib.rcParams['text.usetex'] = True
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_ylabel(r'\textmu')
plt.savefig('fig.ps')Output - PS
Traceback (most recent call last):
File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/backends/backend_ps.py", line 1520, in gs_distill
report = subprocess.check_output(command, stderr=subprocess.STDOUT)
File "/usr/lib/python3.6/subprocess.py", line 336, in check_output
**kwargs).stdout
File "/usr/lib/python3.6/subprocess.py", line 418, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['gs', '-dBATCH', '-dNOPAUSE', '-r6000', '-sDEVICE=ps2write', '-sPAPERSIZE=letter', '-sOutputFile=/tmp/tmpt8w23igm.ps', '/tmp/tmpt8w23igm']' returned non-zero exit status 1.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "Untitled.py", line 8, in <module>
plt.savefig('fig.ps')
File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/pyplot.py", line 701, in savefig
res = fig.savefig(*args, **kwargs)
File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/figure.py", line 1834, in savefig
self.canvas.print_figure(fname, **kwargs)
File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 2267, in print_figure
**kwargs)
File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/backends/backend_ps.py", line 910, in print_ps
return self._print_ps(outfile, 'ps', *args, **kwargs)
File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/backends/backend_ps.py", line 937, in _print_ps
**kwargs)
File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/backends/backend_ps.py", line 1359, in _print_figure_tex
rotated=psfrag_rotated)
File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/backends/backend_ps.py", line 1525, in gs_distill
'\n\n' % exc.output.decode("utf-8")))
RuntimeError: ghostscript was not able to process your image.
Here is the full report generated by ghostscript:
GPL Ghostscript 9.22 (2017-10-04)
Copyright (C) 2017 Artifex Software, Inc. All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Error: /undefined in --get--
Operand stack:
--nostringval-- --dict:9/18(ro)(L)-- 112 --dict:13/13(L)-- --dict:13/13(L)-- base
Execution stack:
%interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push 2015 1 3 %oparray_pop 2014 1 3 %oparray_pop 1998 1 3 %oparray_pop 1884 1 3 %oparray_pop --nostringval-- %errorexec_pop .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- %finish_stringwidth --nostringval-- --nostringval-- 14 8 0 --nostringval-- (pdf_text_enum_t) %op_show_continue --nostringval--
Dictionary stack:
--dict:986/1684(ro)(G)-- --dict:1/20(G)-- --dict:82/200(L)-- --dict:5/6(ro)(L)-- --dict:180/300(L)-- --dict:44/200(L)-- --dict:8/17(L)-- --dict:51/90(L)--
Current allocation mode is local
Last OS error: No such file or directory
Current file position is 58919
GPL Ghostscript 9.22: Unrecoverable error, exit code 1
Output - SVG, PDF
Traceback (most recent call last):
File "Untitled.py", line 8, in <module>
plt.savefig('fig.svg')
File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/pyplot.py", line 701, in savefig
res = fig.savefig(*args, **kwargs)
File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/figure.py", line 1834, in savefig
self.canvas.print_figure(fname, **kwargs)
File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 2267, in print_figure
**kwargs)
File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/backends/backend_svg.py", line 1193, in print_svg
return self._print_svg(filename, svgwriter, **kwargs)
File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/backends/backend_svg.py", line 1248, in _print_svg
self.figure.draw(renderer)
File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/figure.py", line 1299, in draw
renderer, self, artists, self.suppressComposite)
File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/image.py", line 138, in _draw_list_compositing_images
a.draw(renderer)
File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 2437, in draw
mimage._draw_list_compositing_images(renderer, self, artists)
File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/image.py", line 138, in _draw_list_compositing_images
a.draw(renderer)
File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/axis.py", line 1147, in draw
self.label.draw(renderer)
File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/text.py", line 762, in draw
mtext=mtext)
File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/backends/backend_svg.py", line 1150, in draw_tex
self._draw_text_as_path(gc, x, y, s, prop, angle, ismath="TeX")
File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/backends/backend_svg.py", line 950, in _draw_text_as_path
return_new_glyphs_only=True)
File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/textpath.py", line 335, in get_glyphs_tex
font_bunch = self.tex_font_map[dvifont.texname]
File "/home/mirek/.local/lib/python3.6/site-packages/matplotlib/dviread.py", line 850, in __getitem__
result = self._font[texname]
KeyError: b'tcss3583'
For PDF it's nearly identical; the last line reads:
KeyError: b'tcss1000'
In case of PS and SVG, no file is produced. In case of PDF, a corrupted file results. Saving to raster formats and inline previews in jupyter notebook work fine.
Matplotlib version
- Operating system: Debian 10
- Matplotlib version: 2.1.1
- Python version: 3.6.4
Matplotlib from pip. Both Matplotlib and LaTeX are installed in userspace
Reacted by Elan Ernest and Felipe S. S. Schneider
Activity
jklymak commentedon Jan 20, 2018
Oddly works fine for me on macosx both master and 2.1.1... @ImportanceOfBeingErnest did you duplicate this error?
ImportanceOfBeingErnest commentedon Jan 20, 2018
Yes I can replicate this using matplotlib 2.1 on python 2.7, windows 8.1. Using latex from miktex 2.9.
Saving to any vector format does not work, throwing the same error as reported in the post above. Saving to png (
plt.savefig('fig.png')) works as expected, also showing (plt.show()) works fine.This seems to be somehow related to the
\textmuor some special symbols. Other symbols, like e.g.'\rightarrow'work as expected.Edit: Here is a list of symbols I tried.
Works:
Does not work (fails with same error as above):
miromarszal commentedon Jan 20, 2018
Also the \textwon symbol mentioned in #8068 is causing the error, which fits to the many \text* symbols in your list.
ImportanceOfBeingErnest commentedon Jan 20, 2018
The issue seems related/the same, but the outcome for me is different, since also for svg those symbols cause an error, not simply a warning as reported in #8068.
miromarszal commentedon Jan 20, 2018
Is there a way to isolate the file that is being fed to ghostscript causing it to fail? I can see that normally it's immediately destroyed after the exception occurs.
jklymak commentedon Jan 20, 2018
Try adding logging to the end of the imports. I also got rid of the ticks so I only had three dvi files...
Your dvi files should be cached...
miromarszal commentedon Jan 20, 2018
Those DVI files do persist. One contains the symbol and the other says "lp", both very tiny.
I'm interested in this line of the error message:
The output file is created and it's corrupted, while the input file is not there, presumably being deleted right after the exception is caught. It would be nice to somehow grab it and see what bothers ghostscript.
anntzer commentedon Jan 20, 2018
Looks like you need to install the miktex/texlive "cm-super" package. Solves the issue for ps/pdf but not svg.
todo: 1) investigate whether this is really necessary, 2) fix the svg situation, 3) update the docs accordingly.
miromarszal commentedon Jan 21, 2018
I have just installed the package but the problem persists.
jklymak commentedon Jan 21, 2018
@tacaswell was saying something about gs on the gitter channel. Maybe related?
miromarszal commentedon Jan 21, 2018
I managed to capture the temporary PS file that crashes gs. You can find the file here: tmpfile_bad. I also did the same for a good input (no problematic characters): tmpfile_good.
What someone could now do is:
I have no idea about ghostscript so that's about all I could do.
EDIT: Here's the diff of the two. Apart from the obvious difference in positions there's something going on with fonts.
anntzer commentedon Mar 26, 2020
I think the pdf/ps part has been fixed by requiring cm-super, and the svg part is mostly fixed, though some issues remain but are tracked by #8068 / #14159. Closing, but feel free to reopen with new info. (Note also that the ps backend is currently independently broken per #16898.)
oorc06 commentedon Jun 19, 2021
Hello all,
I seem to run into a problem when I use the (latex) package
concmath, and try saving the resultant plot as a pdf (png and jpeg work fine). As suggested above, I've tried installing the "cm-super" package, but this does not solve the issue. I've also played around with the backends, but the problem still persists. A short code that generates this issue:with the following traceback:
Please let me know if there's a way to circumvent this issue. Thank you all in advance!
anntzer commentedon Jun 19, 2021
Can you clarify how you installed concmath? I can't even make it work on a plain latex source, e.g. running latex on
gives (with or without concmath-fonts installed as well)
oorc06 commentedon Jun 19, 2021
Hi @anntzer, as far as I’m aware, it came pre installed with TexShop. The font seems to work fine for me, on both plain latex and matplotlib on python. Only saving to a pdf seems problematic.
anntzer commentedon Jun 19, 2021
Please open a separate issue to track this, then.