Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeError: LaTeX was not able to process the following string #2

Closed
taneta opened this issue Apr 27, 2019 · 6 comments
Closed

RuntimeError: LaTeX was not able to process the following string #2

taneta opened this issue Apr 27, 2019 · 6 comments

Comments

@taneta
Copy link

taneta commented Apr 27, 2019

Trying to reproduce the example:

import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
plt.style.use(['science','ieee'])

x = np.linspace(0.75, 1.25, 201)

def model(x, p):
    return x ** (2 * p + 1) / (1 + x ** (2 * p))

fig, ax = plt.subplots()
for p in [10, 15, 20, 30, 50, 100]:
    ax.plot(x, model(x, p), label=p)
ax.legend(title=r'Order')
ax.set(xlabel=r'$V_0 / V_\mathrm{{gap}}$')
ax.set(ylabel=r'$I_\mathrm{{dc}}^0 / I_\mathrm{{gap}}$')
ax.autoscale(tight=True)
plt.show()


RuntimeError                              Traceback (most recent call last)
~/anaconda3/lib/python3.6/site-packages/IPython/core/formatters.py in __call__(self, obj)
    330                 pass
    331             else:
--> 332                 return printer(obj)
    333             # Finally look for special method names
    334             method = get_real_method(obj, self.print_method)

~/anaconda3/lib/python3.6/site-packages/IPython/core/pylabtools.py in <lambda>(fig)
    235 
    236     if 'png' in formats:
--> 237         png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
    238     if 'retina' in formats or 'png2x' in formats:
    239         png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))

~/anaconda3/lib/python3.6/site-packages/IPython/core/pylabtools.py in print_figure(fig, fmt, bbox_inches, **kwargs)
    119 
    120     bytes_io = BytesIO()
--> 121     fig.canvas.print_figure(bytes_io, **kw)
    122     data = bytes_io.getvalue()
    123     if fmt == 'svg':

~/anaconda3/lib/python3.6/site-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
   2198                     orientation=orientation,
   2199                     dryrun=True,
-> 2200                     **kwargs)
   2201                 renderer = self.figure._cachedRenderer
   2202                 bbox_inches = self.figure.get_tightbbox(renderer)

~/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py in print_png(self, filename_or_obj, *args, **kwargs)
    543 
    544     def print_png(self, filename_or_obj, *args, **kwargs):
--> 545         FigureCanvasAgg.draw(self)
    546         renderer = self.get_renderer()
    547         original_dpi = renderer.dpi

~/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py in draw(self)
    462 
    463         try:
--> 464             self.figure.draw(self.renderer)
    465         finally:
    466             RendererAgg.lock.release()

~/anaconda3/lib/python3.6/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     61     def draw_wrapper(artist, renderer, *args, **kwargs):
     62         before(artist, renderer)
---> 63         draw(artist, renderer, *args, **kwargs)
     64         after(artist, renderer)
     65 

~/anaconda3/lib/python3.6/site-packages/matplotlib/figure.py in draw(self, renderer)
   1142 
   1143             mimage._draw_list_compositing_images(
-> 1144                 renderer, self, dsu, self.suppressComposite)
   1145 
   1146             renderer.close_group('figure')

~/anaconda3/lib/python3.6/site-packages/matplotlib/image.py in _draw_list_compositing_images(renderer, parent, dsu, suppress_composite)
    137     if not_composite or not has_images:
    138         for zorder, a in dsu:
--> 139             a.draw(renderer)
    140     else:
    141         # Composite any adjacent images together

~/anaconda3/lib/python3.6/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     61     def draw_wrapper(artist, renderer, *args, **kwargs):
     62         before(artist, renderer)
---> 63         draw(artist, renderer, *args, **kwargs)
     64         after(artist, renderer)
     65 

~/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_base.py in draw(self, renderer, inframe)
   2424             renderer.stop_rasterizing()
   2425 
-> 2426         mimage._draw_list_compositing_images(renderer, self, dsu)
   2427 
   2428         renderer.close_group('axes')

~/anaconda3/lib/python3.6/site-packages/matplotlib/image.py in _draw_list_compositing_images(renderer, parent, dsu, suppress_composite)
    137     if not_composite or not has_images:
    138         for zorder, a in dsu:
--> 139             a.draw(renderer)
    140     else:
    141         # Composite any adjacent images together

~/anaconda3/lib/python3.6/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     61     def draw_wrapper(artist, renderer, *args, **kwargs):
     62         before(artist, renderer)
---> 63         draw(artist, renderer, *args, **kwargs)
     64         after(artist, renderer)
     65 

~/anaconda3/lib/python3.6/site-packages/matplotlib/axis.py in draw(self, renderer, *args, **kwargs)
   1136         ticks_to_draw = self._update_ticks(renderer)
   1137         ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw,
-> 1138                                                                 renderer)
   1139 
   1140         for tick in ticks_to_draw:

~/anaconda3/lib/python3.6/site-packages/matplotlib/axis.py in _get_tick_bboxes(self, ticks, renderer)
   1076         for tick in ticks:
   1077             if tick.label1On and tick.label1.get_visible():
-> 1078                 extent = tick.label1.get_window_extent(renderer)
   1079                 ticklabelBoxes.append(extent)
   1080             if tick.label2On and tick.label2.get_visible():

~/anaconda3/lib/python3.6/site-packages/matplotlib/text.py in get_window_extent(self, renderer, dpi)
    965             raise RuntimeError('Cannot get window extent w/o renderer')
    966 
--> 967         bbox, info, descent = self._get_layout(self._renderer)
    968         x, y = self.get_unitless_position()
    969         x, y = self.get_transform().transform_point((x, y))

~/anaconda3/lib/python3.6/site-packages/matplotlib/text.py in _get_layout(self, renderer)
    351         tmp, lp_h, lp_bl = renderer.get_text_width_height_descent('lp',
    352                                                          self._fontproperties,
--> 353                                                          ismath=False)
    354         offsety = (lp_h - lp_bl) * self._linespacing
    355 

~/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py in get_text_width_height_descent(self, s, prop, ismath)
    228             fontsize = prop.get_size_in_points()
    229             w, h, d = texmanager.get_text_width_height_descent(s, fontsize,
--> 230                                                                renderer=self)
    231             return w, h, d
    232 

~/anaconda3/lib/python3.6/site-packages/matplotlib/texmanager.py in get_text_width_height_descent(self, tex, fontsize, renderer)
    674         else:
    675             # use dviread. It sometimes returns a wrong descent.
--> 676             dvifile = self.make_dvi(tex, fontsize)
    677             dvi = dviread.Dvi(dvifile, 72 * dpi_fraction)
    678             try:

~/anaconda3/lib/python3.6/site-packages/matplotlib/texmanager.py in make_dvi(self, tex, fontsize)
    421                      'string:\n%s\nHere is the full report generated by '
    422                      'LaTeX: \n\n' % repr(tex.encode('unicode_escape')) +
--> 423                      report))
    424             else:
    425                 mpl.verbose.report(report, 'debug')

RuntimeError: LaTeX was not able to process the following string:
b'lp'
Here is the full report generated by LaTeX: 



<matplotlib.figure.Figure at 0x7fbdfc553a20>```
@garrettj403
Copy link
Owner

garrettj403 commented Apr 30, 2019

Hi @taneta,

This error seems to be caused by LaTeX. You can disable LaTeX by using:

from matplotlib import rc
rc("text", usetex=False)

Otherwise, I've also added a new style to disable LaTeX:

plt.style.use(['science','ieee','no-latex'])

Please let me know if that works!

@garrettj403
Copy link
Owner

This issue seems to have been caused by the fontenc package in the LaTeX preamble (see #4 ).

I removed this package in the latest commit, which should fix your issue.

@drcege
Copy link

drcege commented Jul 24, 2020

This error still exists only when using jupyter notebook. Adding rc("text", usetex=False) is not helpful. But using no-latex style is fine.

This is strange, because the same code works well (with latex) in pure python interpreter.

@drcege
Copy link

drcege commented Jul 24, 2020

I found this is caused by the U+2212 MINUS SIGN.

The temporary tex files produced by jupyter notebook is slightly different from that by pure python interpreter. Sometimes, for some reason (I don't know), jupyter notebook generates

\fontsize{10.000000}{12.500000}{\sffamily $2$}

rather than

\fontsize{10.000000}{12.500000}{\rmfamily lp}

or other positive values. Then the latex engine throws error:

! Package inputenc Error: Unicode char − (U+2212)
(inputenc)                not set up for use with LaTeX.

A workaround of disabling unicode_minus works for me:
(from https://stackoverflow.com/a/32020370)

matplotlib.rcParams['axes.unicode_minus'] = False

@ZhangYuef
Copy link

I faced this problem when I used Jupyter notebook.

jacopo-chevallard pushed a commit to jacopo-chevallard/PyP-BEAGLE that referenced this issue Mar 22, 2021
@MustafaAlotbah
Copy link

Somehow, this problem occurs when using PowerShell, but not when using Jupyter Lab. Not sure why this problem is occurring, despite using the same environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants