jupyterhub / mybinder.org-user-guide

Turn a Git repo into a collection of interactive notebooks. This is Binder's user documentation repository.
https://mybinder.readthedocs.io
BSD 3-Clause "New" or "Revised" License
151 stars 103 forks source link

Latex error when using matplotlib with usetex=True #211

Closed berndflemisch closed 3 years ago

berndflemisch commented 4 years ago

Apparently, this is related to https://github.com/binder-examples/latex, but the issue tracker there points to the one here.

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 a Binder notebook with Python 3 kernel, I used this one.
  2. Paste the following
    import matplotlib.pyplot as plt
    plt.rc('text', usetex=True)
    fig = plt.figure()
    ax = fig.add_subplot()
  3. Execute
  4. See error
welcome[bot] commented 4 years ago

Thank you for opening your first issue in this project! Engagement like this is essential for open source projects! :hugs:
If you haven't done so already, check out Jupyter's Code of Conduct. Also, please try to follow the issue template as it helps other other community members to contribute more effectively. welcome You can meet the other Jovyans by joining our Discourse forum. There is also an intro thread there where you can stop by and say Hi! :wave:
Welcome to the Jupyter community! :tada:

meeseeksmachine commented 4 years ago

This issue has been mentioned on Jupyter Community Forum. There might be relevant details there:

https://discourse.jupyter.org/t/binder-examples-latex-fails/5149/4

betatim commented 4 years ago

Do you know if this error is specific to the repo (particular version of a package we use?) or a misconfiguration?

PedroTayer commented 3 years ago

Does someone have any news about this issue? Just for you to know, I just used usetex=False and still the latex variables were shown

support[bot] commented 3 years ago

Hi there @berndflemisch :wave:!

I closed this issue because it was labelled as a support question.

Please help us organize discussion by posting this on the http://discourse.jupyter.org/ forum.

Our goal is to sustain a positive experience for both users and developers. We use GitHub issues for specific discussions related to changing a repository's content, and let the forum be where we can more generally help and inspire each other.

Thanks you for being an active member of our community! :heart:

adamwangdata commented 3 years ago

The issue can be fixed by adding the line cm-super to your apt.txt file. This installs type1ec.sty when Binder builds the Docker image. A complete apt.txt adapted from https://github.com/binder-examples/latex then looks like this:

dvipng
ghostscript
texlive-fonts-recommended
texlive-generic-recommended
texlive-latex-base
texlive-latex-extra
texlive-latex-recommended
texlive-publishers
texlive-science
texlive-xetex
cm-super
berndflemisch commented 3 years ago

This solves the issue, thanks!