Skip to content

RuntimeError: LaTeX was not able to process the following string: 'z=$\\\\mathregular{{}^{}_{\\\\}}$' in matplotlib #5076

Closed
@neuronphysics

Description

@neuronphysics

I have been trying to generate Latex style plots for publication by

from __future__ import unicode_literals
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.colors as mplcol
import matplotlib.cm as cmx
import numpy as np
plt.switch_backend('PS') 
matplotlib.use('PS')
matplotlib.style.use('ggplot')
matplotlib.rcParams['axes.edgecolor']='black'
matplotlib.rcParams['axes.facecolor']= 'white'
matplotlib.rcParams['axes.grid']= False
matplotlib.rcParams['ytick.major.size']= 2
matplotlib.rcParams['ytick.minor.pad']= 0.05
matplotlib.rcParams['xtick.major.size']= 2
matplotlib.rcParams['xtick.minor.pad']= 0.25
matplotlib.rcParams['legend.frameon'] = False
matplotlib.rcParams['text.usetex']=True
matplotlib.rcParams['text.latex.unicode']=True
matplotlib.rcParams['ps.usedistiller']='xpdf'
matplotlib.rcParams['ps.distiller.res']= 9000
matplotlib.rcParams['savefig.dpi']= 500
value = "{" + str(round(r.value, 2)) + "}"
upper = "{+" + str(round(r.upper_error, 2)) + "}"
lower = "{\,-" + str(round(r.lower_error, 2)) + "}"
# format this in a format LaTeX can parse. Use mathregular so that the
# font is the same as all the other fonts on the plot.
ext = r"z=$\mathregular{{{value}^{upper}_{lower}}}$".format(
        value=value, upper=upper, lower=lower)
save_as_one_pdf(figures, params["plot_directory"] +
                    self.name+'.pdf' )

where I want to plot different figures in multiple pages with this function:

def save_as_one_pdf(figs, filename):
    """
    Save the figures into one long PDF file

    :param figs: list of figures to be saved as PDFs
    :param filename: place where the PDFs will be saved
    :return: none
    """

    # Save the pdfs as one file
    pp = PdfPages(filename)
    for fig in figs:
        pp.savefig(fig)
    pp.close()
    for fig in figs:
        plt.close(fig)

But when I ran my script I've got the following errors:

 File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_pdf.py", line 2431, in savefig
    figure.savefig(self, format='pdf', **kwargs)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/figure.py", line 1476, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 2211, in print_figure
    **kwargs)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_pdf.py", line 2485, in print_pdf
    self.figure.draw(renderer)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/artist.py", line 59, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/figure.py", line 1085, in draw
    func(*args)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/artist.py", line 59, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/axes/_base.py", line 2110, in draw
    a.draw(renderer)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/artist.py", line 59, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/text.py", line 595, in draw
    bbox, info, descent = self._get_layout(renderer)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/text.py", line 320, in _get_layout
    ismath=ismath)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_pdf.py", line 2044, in get_text_width_height_descent
    renderer=self)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/texmanager.py", line 670, in get_text_width_height_descent
    dvifile = self.make_dvi(tex, fontsize)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/texmanager.py", line 417, in make_dvi
    report))
RuntimeError: LaTeX was not able to process the following string:
'z=$\\\\mathregular{{0.27}^{+0.01}_{\\\\,-0.01}}$'
Here is the full report generated by LaTeX: 

This is pdfTeX, Version 3.1415926-2.5-1.40.14 (TeX Live 2013/Debian)
 restricted \write18 enabled.
entering extended mode
(./384610b05118db25834f6dd15bc90717.tex
LaTeX2e <2011/06/27>
Babel <3.9h> and hyphenation patterns for 62 languages loaded.
(/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2007/10/19 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)
(/usr/share/texlive/texmf-dist/tex/latex/psnfss/helvet.sty
(/usr/share/texlive/texmf-dist/tex/latex/graphics/keyval.sty))
(/usr/share/texlive/texmf-dist/tex/latex/psnfss/courier.sty)
(/usr/share/texlive/texmf-dist/tex/latex/base/textcomp.sty
(/usr/share/texlive/texmf-dist/tex/latex/base/ts1enc.def))
(/usr/share/texlive/texmf-dist/tex/latex/ucs/ucs.sty
(/usr/share/texlive/texmf-dist/tex/latex/ucs/data/uni-global.def))
(/usr/share/texlive/texmf-dist/tex/latex/base/inputenc.sty
(/usr/share/texlive/texmf-dist/tex/latex/ucs/utf8x.def))
(/usr/share/texlive/texmf-dist/tex/latex/geometry/geometry.sty
(/usr/share/texlive/texmf-dist/tex/generic/oberdiek/ifpdf.sty)
(/usr/share/texlive/texmf-dist/tex/generic/oberdiek/ifvtex.sty)
(/usr/share/texlive/texmf-dist/tex/generic/ifxetex/ifxetex.sty)

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


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

)
No file 384610b05118db25834f6dd15bc90717.aux.
(/usr/share/texlive/texmf-dist/tex/latex/base/ts1cmr.fd)
(/usr/share/texlive/texmf-dist/tex/latex/psnfss/ot1pnc.fd)
(/usr/share/texlive/texmf-dist/tex/latex/ucs/ucsencs.def)
*geometry* driver: auto-detecting
*geometry* detected driver: dvips
(/usr/share/texlive/texmf-dist/tex/latex/psnfss/ot1phv.fd)
! Undefined control sequence.
<recently read> \mathregular 

l.13 ...0000}{12.500000}{\sffamily z=$\mathregular
                                                  {{0.27}^{+0.01}_{\,-0.01}}$}
[1] (./384610b05118db25834f6dd15bc90717.aux) )
(see the transcript file for additional information)

even after I changed the text to
text = r"z=${value}^{upper}_{lower}$".format(
value=value, upper=upper, lower=lower)

I've got the following error:

 File "cluster.py", line 762, in save_as_one_pdf
    pp.savefig(fig)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_pdf.py", line 2431, in savefig
    figure.savefig(self, format='pdf', **kwargs)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/figure.py", line 1476, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 2211, in     print_figure
**kwargs)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_pdf.py", line 2485, in print_pdf
    self.figure.draw(renderer)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/artist.py", line 59, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/figure.py", line 1085, in draw
func(*args)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/artist.py", line 59, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/axes/_base.py", line 2110, in draw
a.draw(renderer)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/artist.py", line 59, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/axis.py", line 1116, in draw
renderer)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/axis.py", line 1065, in _get_tick_bboxes
extent = tick.label1.get_window_extent(renderer)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/text.py", line 798, in get_window_extent
    bbox, info, descent = self._get_layout(self._renderer)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/text.py", line 311, in _get_layout
ismath=False)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_pdf.py", line 2044, in get_text_width_height_descent
renderer=self)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/texmanager.py", line 670, in get_text_width_height_descent
dvifile = self.make_dvi(tex, fontsize)
  File "/vol/anaconda/lib/python2.7/site-packages/matplotlib/texmanager.py", line 417, in make_dvi
report))
RuntimeError: LaTeX was not able to process the following string:
'lp'
Here is the full report generated by LaTeX: 

! Package unicode-math Error: Cannot be run with pdfLaTeX!
(unicode-math)                Use XeLaTeX or LuaLaTeX instead..

See the unicode-math package documentation for explanation.
Type  H <return>  for immediate help.

How can I make this all work or avoid this error?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions