question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Latex error when using matplotlib with usetex=True

See original GitHub issue

Bug description

When using matplotlib with usetex=True, a Latex error is produced.

Actual behaviour

The example below produces

---------------------------------------------------------------------------
CalledProcessError                        Traceback (most recent call last)
/srv/conda/envs/notebook/lib/python3.7/site-packages/matplotlib/texmanager.py in _run_checked_subprocess(self, command, tex)
    304                                              cwd=self.texcache,
--> 305                                              stderr=subprocess.STDOUT)
    306         except FileNotFoundError as exc:

/srv/conda/envs/notebook/lib/python3.7/subprocess.py in check_output(timeout, *popenargs, **kwargs)
    410     return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
--> 411                **kwargs).stdout
    412 

/srv/conda/envs/notebook/lib/python3.7/subprocess.py in run(input, capture_output, timeout, check, *popenargs, **kwargs)
    511             raise CalledProcessError(retcode, process.args,
--> 512                                      output=stdout, stderr=stderr)
    513     return CompletedProcess(process.args, retcode, stdout, stderr)

CalledProcessError: Command '['latex', '-interaction=nonstopmode', '--halt-on-error', '/home/jovyan/.cache/matplotlib/tex.cache/1acea6f6c115d0ec7a634ed0529287b9.tex']' returned non-zero exit status 1.

The above exception was the direct cause of the following exception:

RuntimeError                              Traceback (most recent call last)
/srv/conda/envs/notebook/lib/python3.7/site-packages/IPython/core/formatters.py in __call__(self, obj)
    339                 pass
    340             else:
--> 341                 return printer(obj)
    342             # Finally look for special method names
    343             method = get_real_method(obj, self.print_method)

/srv/conda/envs/notebook/lib/python3.7/site-packages/IPython/core/pylabtools.py in <lambda>(fig)
    246 
    247     if 'png' in formats:
--> 248         png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
    249     if 'retina' in formats or 'png2x' in formats:
    250         png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))

/srv/conda/envs/notebook/lib/python3.7/site-packages/IPython/core/pylabtools.py in print_figure(fig, fmt, bbox_inches, **kwargs)
    130         FigureCanvasBase(fig)
    131 
--> 132     fig.canvas.print_figure(bytes_io, **kw)
    133     data = bytes_io.getvalue()
    134     if fmt == 'svg':

/srv/conda/envs/notebook/lib/python3.7/site-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, **kwargs)
   2077                             print_method, dpi=dpi, orientation=orientation),
   2078                         draw_disabled=True)
-> 2079                     self.figure.draw(renderer)
   2080                     bbox_artists = kwargs.pop("bbox_extra_artists", None)
   2081                     bbox_inches = self.figure.get_tightbbox(renderer,

/srv/conda/envs/notebook/lib/python3.7/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     36                 renderer.start_filter()
     37 
---> 38             return draw(artist, renderer, *args, **kwargs)
     39         finally:
     40             if artist.get_agg_filter() is not None:

/srv/conda/envs/notebook/lib/python3.7/site-packages/matplotlib/figure.py in draw(self, renderer)
   1734             self.patch.draw(renderer)
   1735             mimage._draw_list_compositing_images(
-> 1736                 renderer, self, artists, self.suppressComposite)
   1737 
   1738             renderer.close_group('figure')

/srv/conda/envs/notebook/lib/python3.7/site-packages/matplotlib/image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
    135     if not_composite or not has_images:
    136         for a in artists:
--> 137             a.draw(renderer)
    138     else:
    139         # Composite any adjacent images together

/srv/conda/envs/notebook/lib/python3.7/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     36                 renderer.start_filter()
     37 
---> 38             return draw(artist, renderer, *args, **kwargs)
     39         finally:
     40             if artist.get_agg_filter() is not None:

/srv/conda/envs/notebook/lib/python3.7/site-packages/matplotlib/axes/_base.py in draw(self, renderer, inframe)
   2628             renderer.stop_rasterizing()
   2629 
-> 2630         mimage._draw_list_compositing_images(renderer, self, artists)
   2631 
   2632         renderer.close_group('axes')

/srv/conda/envs/notebook/lib/python3.7/site-packages/matplotlib/image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
    135     if not_composite or not has_images:
    136         for a in artists:
--> 137             a.draw(renderer)
    138     else:
    139         # Composite any adjacent images together

/srv/conda/envs/notebook/lib/python3.7/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     36                 renderer.start_filter()
     37 
---> 38             return draw(artist, renderer, *args, **kwargs)
     39         finally:
     40             if artist.get_agg_filter() is not None:

/srv/conda/envs/notebook/lib/python3.7/site-packages/matplotlib/axis.py in draw(self, renderer, *args, **kwargs)
   1227         ticks_to_draw = self._update_ticks()
   1228         ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw,
-> 1229                                                                 renderer)
   1230 
   1231         for tick in ticks_to_draw:

/srv/conda/envs/notebook/lib/python3.7/site-packages/matplotlib/axis.py in _get_tick_bboxes(self, ticks, renderer)
   1172         """Return lists of bboxes for ticks' label1's and label2's."""
   1173         return ([tick.label1.get_window_extent(renderer)
-> 1174                  for tick in ticks if tick.label1.get_visible()],
   1175                 [tick.label2.get_window_extent(renderer)
   1176                  for tick in ticks if tick.label2.get_visible()])

/srv/conda/envs/notebook/lib/python3.7/site-packages/matplotlib/axis.py in <listcomp>(.0)
   1172         """Return lists of bboxes for ticks' label1's and label2's."""
   1173         return ([tick.label1.get_window_extent(renderer)
-> 1174                  for tick in ticks if tick.label1.get_visible()],
   1175                 [tick.label2.get_window_extent(renderer)
   1176                  for tick in ticks if tick.label2.get_visible()])

/srv/conda/envs/notebook/lib/python3.7/site-packages/matplotlib/text.py in get_window_extent(self, renderer, dpi)
    903             raise RuntimeError('Cannot get window extent w/o renderer')
    904 
--> 905         bbox, info, descent = self._get_layout(self._renderer)
    906         x, y = self.get_unitless_position()
    907         x, y = self.get_transform().transform((x, y))

/srv/conda/envs/notebook/lib/python3.7/site-packages/matplotlib/text.py in _get_layout(self, renderer)
    291         _, lp_h, lp_d = renderer.get_text_width_height_descent(
    292             "lp", self._fontproperties,
--> 293             ismath="TeX" if self.get_usetex() else False)
    294         min_dy = (lp_h - lp_d) * self._linespacing
    295 

/srv/conda/envs/notebook/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py in get_text_width_height_descent(self, s, prop, ismath)
    202             fontsize = prop.get_size_in_points()
    203             w, h, d = texmanager.get_text_width_height_descent(
--> 204                 s, fontsize, renderer=self)
    205             return w, h, d
    206 

/srv/conda/envs/notebook/lib/python3.7/site-packages/matplotlib/texmanager.py in get_text_width_height_descent(self, tex, fontsize, renderer)
    448         else:
    449             # use dviread. It sometimes returns a wrong descent.
--> 450             dvifile = self.make_dvi(tex, fontsize)
    451             with dviread.Dvi(dvifile, 72 * dpi_fraction) as dvi:
    452                 page, = dvi

/srv/conda/envs/notebook/lib/python3.7/site-packages/matplotlib/texmanager.py in make_dvi(self, tex, fontsize)
    337                 self._run_checked_subprocess(
    338                     ["latex", "-interaction=nonstopmode", "--halt-on-error",
--> 339                      texfile], tex)
    340             for fname in glob.glob(basefile + '*'):
    341                 if not fname.endswith(('dvi', 'tex')):

/srv/conda/envs/notebook/lib/python3.7/site-packages/matplotlib/texmanager.py in _run_checked_subprocess(self, command, tex)
    316                     prog=command[0],
    317                     tex=tex.encode('unicode_escape'),
--> 318                     exc=exc.output.decode('utf-8'))) from exc
    319         _log.debug(report)
    320         return report

RuntimeError: latex was not able to process the following string:
b'lp'

Here is the full report generated by latex:
This is pdfTeX, Version 3.14159265-2.6-1.40.18 (TeX Live 2017/Debian) (preloaded format=latex)
 restricted \write18 enabled.
entering extended mode
(/home/jovyan/.cache/matplotlib/tex.cache/1acea6f6c115d0ec7a634ed0529287b9.tex
LaTeX2e <2017-04-15>
Babel <3.18> and hyphenation patterns for 7 language(s) loaded.
(/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo))
(/usr/share/texlive/texmf-dist/tex/latex/type1cm/type1cm.sty)

! LaTeX Error: File `type1ec.sty' not found.

Type X to quit or <RETURN> to proceed,
or enter new name. (Default extension: sty)

Enter file name: 
! Emergency stop.
<read *> 
         
l.5 \usepackage
               {type1ec}^^M
No pages of output.
Transcript written on 1acea6f6c115d0ec7a634ed0529287b9.log.

How to reproduce

  1. Open an arbitrary Binder notebook with Python 3 kernel
  2. Paste the following
import matplotlib.pyplot as plt
plt.rc('text', usetex=True)
fig = plt.figure()
ax = fig.add_subplot()
  1. Execute
  2. See error

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
matqrcommented, May 13, 2021

I kept experiencing the same problem and after reading a bit more, I think the issue is regarding the installed version of LaTeX. I solved the problem by installing the cm-super family (e.g., sudo apt install cm-super)

0reactions
berndflemischcommented, Aug 20, 2020

Ok, thanks for pointing me there. In fact, the issue tracker there tells me that I should file an issue at https://github.com/jupyterhub/binder/issues. Which I did: https://github.com/jupyterhub/binder/issues/211.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Latex error when using matplotlib with usetex=True · Issue #211
Bug description. When using matplotlib with usetex=True , a Latex error is produced. Actual behaviour. The example below produces.
Read more >
Matplotlib failing while text.usetex==True - Stack Overflow
I want to use Latex to create greek symbols in plot labels. ... The final error on the pages of errors is that...
Read more >
Matplotlib error: No such file or directory: 'latex': 'latex' - TeX
First check the latex on your system by doing the following : whereis latex. if you got empty output then install these packages...
Read more >
Text rendering with LaTeX — Matplotlib 3.6.2 documentation
Matplotlib can use LaTeX to render text. This is activated by setting text.usetex : True in your rcParams, or by setting the usetex...
Read more >
Text rendering With LaTeX — Matplotlib 1.3.1 documentation
Matplotlib has the option to use LaTeX to manage all text layout. ... The LaTeX option is activated by setting text.usetex : True...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found