2
import numpy as np
import matplotlib
matplotlib.rcParams['text.usetex'] = True
import matplotlib.pyplot as plt


t = np.linspace(0.0, 1.0, 100)
s = np.cos(4 * np.pi * t) + 2

fig, ax = plt.subplots(figsize=(6, 4), tight_layout=True)
ax.plot(t, s)

ax.set_xlabel(r'\textbf{time (s)}')
ax.set_ylabel('\itshape {Velocity (\N{DEGREE SIGN}/sec)}', fontsize=16)
ax.set_title(r'\TeX\ is Number $\displaystyle\sum_{n=1}^\infty'
             r'\frac{-e^{i\pi}}{2^n}$!', fontsize=16, color='r')
plt.show()

This is the Tex example on Matplotlib site. But, I got this messauntimeError:

latex was not able to process the following string:
'\\\\itshape {Velocity (\\\\N{DEGREE SIGN}/sec)}'

Here is the full report generated by latex:

This is pdfTeX, Version 3.14159265-2.6-1.40.16 (TeX Live 2015/Debian) (preloaded format=latex)
 restricted \write18 enabled.
entering extended mode
(/home/joshua/.cache/matplotlib/tex.cache/88a38ed565fe9aac24ebc44c9a0be443.tex
LaTeX2e <2016/02/01>
Babel <3.9q> and hyphenation patterns for 81 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)
(/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/latex/graphics/keyval.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.

) (./88a38ed565fe9aac24ebc44c9a0be443.aux)
(/usr/share/texlive/texmf-dist/tex/latex/base/ts1cmr.fd)
(/usr/share/texlive/texmf-dist/tex/latex/ucs/ucsencs.def)
*geometry* driver: auto-detecting
*geometry* detected driver: dvips

LaTeX Font Warning: Font shape `OT1/cmss/m/it' in size <16> not available
(Font)              Font shape `OT1/cmss/m/sl' tried instead on input line 15.

! Undefined control sequence.
l.15 ...0.000000}{\sffamily \itshape {Velocity (\N
                                                  {DEGREE SIGN}/sec)}}
No pages of output.
Transcript written on 88a38ed565fe9aac24ebc44c9a0be443.log.ge over and over.

Is there anyone to solve this problem?

CarLaTeX
  • 62,716
joshua
  • 21
  • 2
    Welcome to TeX.SE! \N is interpreted as a command by LaTeX, which is not defined. I don't know Matplotlib, could you add the LaTeX code generated by it? – CarLaTeX Nov 23 '18 at 05:08

1 Answers1

4

There's an issue between how python is trying to handle \N{DEGREE SIGN} (attempt to put in a unicode degree symbol) and how TeX is receiving that. It might have been valid for an older version of python on matplotlib that has since been lost. In general you're better off taking the TeX approach and using $\circ$ instead of \N{DEGREE SIGN}, making that line ax.set_ylabel(r'\itshape {Velocity ($\circ$/sec)}', fontsize=16). Note the switch there to add an r to the start of the label command, that's to treat the line as a raw string instead of trying to treat backslashes as python escapes (which is where the error message came from).

The current matplotlib LaTeX example is also different from yours, it reads

import numpy as np
import matplotlib.pyplot as plt


# Example data
t = np.arange(0.0, 1.0 + 0.01, 0.01)
s = np.cos(4 * np.pi * t) + 2

plt.rc('text', usetex=True)
plt.rc('font', family='serif')
plt.plot(t, s)

plt.xlabel(r'\textbf{time} (s)')
plt.ylabel(r'\textit{voltage} (mV)',fontsize=16)
plt.title(r"\TeX\ is Number "
          r"$\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!",
          fontsize=16, color='gray')
# Make room for the ridiculously large title.
plt.subplots_adjust(top=0.8)

plt.savefig('tex_demo')
plt.show()

Source: https://matplotlib.org/users/usetex.html

aoi
  • 325