2

Having figured out how to insert mhchem into MathJax expressions in an IPython notebook, (from this question) I was pretty pleased until I wanted to convert the notebook to PDF. My markdown cell contains:

$\require{mhchem}$
$\ce{CH2O2->[k_1]H2 + CO2}$

But attempting to convert the notebook to PDF using the following terminal command

ipython nbconvert untitled.ipynb --to pdf

results in an ! Undefined Control Sequence error - similar to this post. However the solution proposed (\texorpdfstring) isn't recognized by the MathJax interpreter in the markdown cell. Any ideas or workarounds to resolve the issue?

IPython 3.0.0 /MiKTeX 2.9 /win7x86

Community
  • 1
  • 1
chepyle
  • 976
  • 1
  • 9
  • 16
  • Could you be more specific about the problem, maybe share an example notebook and/or post the relevant latex output. Have you placed the \ce in a heading cell? – Jakob May 13 '15 at 20:59
  • you can check out the notebook (a single markdown cell) here: http://nbviewer.ipython.org/github/chepyle/ipynb-test/blob/master/mhchem_test.ipynb – chepyle May 15 '15 at 16:22
  • Does your tex document compiles fine if you comment out the `$\require{mhchem}$` part? – Jakob May 16 '15 at 20:38
  • No, because there are ``\ce`` in the notebook cells then and latex will complain that ``\ce`` is undefined control sequence (obviously ``\usepackage{mhchem}`` is not added on nbconvert). – nepix32 Sep 17 '15 at 12:24
  • As my edit has been rejected, trying to specify the issue: error message is ``! Undefined control sequence. l.230 \[\require {mhchem}\]``. I very strongly suppose that this is the real error message of the OP. My system is (comparable to his) IPython3.2/LiveTex 2015/Win7x86/pandoc 0.15. – nepix32 Sep 17 '15 at 13:14

2 Answers2

3

To include the mhchem package you need to create a custom template. Moreover it is necessary to handle the \require mathjax command which is not available in pdflatex.

A possible template (chemtempl.tplx) could look like

((*- extends 'article.tplx' -*))

((* block packages *))
((( super() )))
\usepackage{mhchem}
((* endblock packages *))

((* block commands *))
((( super() )))
% disable require command
\newcommand{\require}[1]{}
((* endblock commands *))

This template extends to default article template by overriding the package and commands block. The super call is similar to the python command and includes the original block content. The \require command is tackled by defining a new command which takes one argument and does nothing -> this way it can remain in the notebook.

The template is used like

jupyter nbconvert --template chemtempl.tplx --to pdf notebook.ipynb

if you are using IPython 3.x just replace jupyter with ipython.

To use the template also with the download as pdf feature in the notebook, a config file has to be used. To this end create a config file (.ipython/profile_default/ipython_config.py for IPython 3.x or .jupyter/jupyter_notebook_config.py for IPython 4.x (Jupyter)) with the following content:

c = get_config()
c.LatexExporter.template_path = ['/home/path/to/template/']
c.LatexExporter.template_file = 'chemtempl.tplx'

After restarting Jupyter the new config should be loaded and the template be used.

(I guess for IPython 3.x the file could also be named ipython_notebook_config.py but currently I have no 3.x version to test this.)

Jakob
  • 19,815
  • 6
  • 75
  • 94
  • IPython 3.1 (notebook) seems to have neither the command line flag ``--template`` nor ``--post``. Can you point to jupyter documentation where this is explained? – nepix32 Sep 18 '15 at 08:07
  • sorry my mistake! It has to be ``ipython nbconvert ...``. Edited my answer. – Jakob Sep 18 '15 at 09:51
  • And this is the [documentation of nbconvert](http://nbconvert.readthedocs.org/en/latest/) – Jakob Sep 18 '15 at 09:52
  • Ok, thanks, ``--post pdf`` does not work (misses a library), but ``--to pdf`` does. Is it possible to specify the template inclusion from the notebook interface? – nepix32 Sep 18 '15 at 10:39
  • Sorry again, the ``--post pdf`` call is from ipython 2.x since 3.x it's ``--to pdf``. Yes, you can specify the template to be used when downloading a notebook as pdf. I'll update my answer in a few hours. – Jakob Sep 18 '15 at 18:18
  • 1
    I think you have earned the bounty already, but as you seem to be a knowledgeable jupyter user: Do you see a possibility to set the configuration for a specific notebook from **within** a notebook (analoguous to the ``$\require{mhchem}$`` directive? – nepix32 Sep 19 '15 at 06:46
  • this is a tricky question :) currently I don't know any way, but I will dig into this a bit deeper, maybe there is one. – Jakob Sep 20 '15 at 07:15
  • I awarded the bounty because it is fully deserved. -1 for me, because I did not require the solution for the question to be "inline" in the notebook. – nepix32 Sep 21 '15 at 14:15
  • I had created an issue [here](https://github.com/jupyter/notebook/issues/356) and asked for advice how to include the template information in the notebook. However, it is currently not possible due to security issues. – Jakob Sep 21 '15 at 19:23
0

After failing to follow the answer above, I discovered an easy workaround.

You can simply edit the default LaTex template used by nbconvert to include

\usepackage{mhchem}

in the preamble. This means that all chem equations will work just like any math equations in your notebook.

For me (macOS) the template was found at /Users/USER/opt/anaconda3/share/jupyter/nbconvert/templates/latex/base.tex.j2

I'm not sure if this was related, but I got a few compile errors once I modified the preamble related to spaces next to my $ in math blocks. It was all fixed after making sure there was no space within the $'s and one space outside them.

Pepsi-Joe
  • 447
  • 1
  • 10