Latex errors if figure labels include underscores
Closed this issue · 3 comments
Bug Report
Describe the bug
I think this issue is a bug with an easy workaround, and probably is a latex issue at heart. However it cost me a lot of pain to discover what the problem was, so it would be good to at least document it if that's not done already.
@chrisjsewell it sounds from your comments like this can be fixed by escaping underscores with "\_"
, and like this is a matplotlib issue - matplotlib should handle the escaping? Should this issue be raised there?
To Reproduce
The following code snippet triggers this error due to the y-axis label (take the underscore out of the label and it's fine):
import matplotlib.pyplot as plt
plt.plot([1,2,3,4],[1,4,9,16])
plt.ylabel('y_values')
The following Pandas/Seaborn code snippet triggers the error due to the y-axis tick labels containing underscores:
import seaborn as sns
import pandas as pd
sns.boxenplot(
data=pd.DataFrame(dict(y=['a_1']*3 + ['a_2']*3, x=[1,2,3,5,6,7])),
x='x',
y='y')
This second example is analogous to the issue I actually ran into: categorical label names in my dataset, and therefore values in my DataFrame, contained underscores.
Minimal Notebook Example
Expected Behaviour
I'd expect the same plots as come up if underscores are removed from the strings in the examples above.
Runtime Information
- IPyPublish: 0.7.1
- Python: 3.7.2
- OS: 10.12.6
- Pandoc: 2.5
Additional context
Logging
As recommended in the documentation, I'm importing
from ipypublish.scripts.ipynb_latex_setup import *
at the top of my Jupyter Notebook.
If I create a plot where the figure labels contain underscores, then run the notebook cell, the figure fails to render and I get latex errors like:
RuntimeError: latex was not able to process the following string:
b'y_values'
Here is the full report generated by latex:
This is pdfTeX, Version 3.14159265-2.6-1.40.19 (TeX Live 2018) (preloaded format=latex)
restricted \write18 enabled.
entering extended mode
(/Users/clare/.matplotlib/tex.cache/7fe104e47212b624ca12a6d46285698e.tex
LaTeX2e <2018-04-01> patch level 2
Babel <3.18> and hyphenation patterns for 22 language(s) loaded.
(/usr/local/texlive/2018basic/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/local/texlive/2018basic/texmf-dist/tex/latex/base/size10.clo))
(/usr/local/texlive/2018basic/texmf-dist/tex/latex/type1cm/type1cm.sty)
(/usr/local/texlive/2018basic/texmf-dist/tex/latex/base/textcomp.sty
(/usr/local/texlive/2018basic/texmf-dist/tex/latex/base/ts1enc.def
(/usr/local/texlive/2018basic/texmf-dist/tex/latex/base/ts1enc.dfu)))
(/usr/local/texlive/2018basic/texmf-dist/tex/latex/base/inputenc.sty)
(/usr/local/texlive/2018basic/texmf-dist/tex/latex/subdepth/subdepth.sty)
(/usr/local/texlive/2018basic/texmf-dist/tex/latex/geometry/geometry.sty
(/usr/local/texlive/2018basic/texmf-dist/tex/latex/graphics/keyval.sty)
(/usr/local/texlive/2018basic/texmf-dist/tex/generic/oberdiek/ifpdf.sty)
(/usr/local/texlive/2018basic/texmf-dist/tex/generic/oberdiek/ifvtex.sty)
(/usr/local/texlive/2018basic/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 7fe104e47212b624ca12a6d46285698e.aux.
(/usr/local/texlive/2018basic/texmf-dist/tex/latex/base/ts1cmr.fd)
*geometry* driver: auto-detecting
*geometry* detected driver: dvips
! Missing $ inserted.
<inserted text>
$
l.15 \fontsize{18.000000}{22.500000}{\rmfamily y_
values}
No pages of output.
Transcript written on 7fe104e47212b624ca12a6d46285698e.log.
The actual stack trace is a lot longer due to the layers of processing going on here.
Thanks, so
for pandas
The raw latex is stored directly in the notebook, and this
was the offending line, which should be:
pd.set_option('display.latex.escape', True)
(unless there was a reason that I've forgotten for not doing it)
For general latex, I may also want to also change this line and this line to:
((( draw_table(cell, resources, output.data['text/latex'] | escape_latex ) )))
for matplotlib
Setting
mpl.rcParams['text.usetex'] = True
Causes matplotlib to actually processes the latex and creates a PDF, so it would be hard to get a hook on it. I don't know if there's a way to make it only save the latex to the notebook?
Changes in new ipypublish version
Please note also, in v0.9.0 (to come shortly!), I will be deprecating ipypublish.scripts.ipynb_latex_setup
, in favour of ipypublish.scripts.nb_setup
:
from ipypublish import nb_setup
plt = nb_setup.setup_matplotlib(
print_errors=True,
output=('pdf',))
pd = nb_setup.setup_pandas(escape_latex=True)
import numpy as np
# sym = nb_setup.setup_sympy()
(unless there was a reason that I've forgotten for not doing it)
There was a reason. If escape_latex=True
then it stops math from being resolved, e.g. $\delta$
goes to \$\\delta\$