将IPython notebook mhchem markdown转换为pdf

时间:2015-05-13 00:43:11

标签: pdf latex ipython-notebook mathjax

已经想出如何将mhchem插入到IPython笔记本中的MathJax表达式中(来自此question)我很高兴直到我想将笔记本转换为PDF。我的降价单元格包含:

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

但是尝试使用以下终端命令将笔记本转换为PDF

ipython nbconvert untitled.ipynb --to pdf

导致! Undefined Control Sequence错误 - 与此post类似。但是,建议的解决方案(\texorpdfstring)不会被降价单元格中的MathJax解释器识别。解决问题的任何想法或解决方法?

IPython 3.0.0 / MiKTeX 2.9 / win7x86

1 个答案:

答案 0 :(得分:3)

要包含创建自定义模板所需的mhchem包。此外,有必要处理pdflatex中没有的\require mathjax命令。

可能的模板(chemtempl.tplx)看起来像

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

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

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

此模板通过覆盖包和命令块扩展到默认文章模板。 super调用类似于python命令,包括原始块内容。 \require命令通过定义一个新命令来解决,该命令接受一个参数并且什么都不做 - >这样它就可以保留在笔记本中。

模板用作

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

如果您使用的是IPython 3.x,请将jupyter替换为ipython

要将模板与笔记本中的下载为pdf 功能一起使用, 必须使用配置文件。为此,使用以下内容为IPython 3.x或.ipython/profile_default/ipython_config.py为IPython 4.x(Jupyter)创建配置文件(.jupyter/jupyter_notebook_config.py

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

重新启动Jupyter后,应加载新配置并使用模板。

(我猜对于IPython 3.x,该文件也可以命名为ipython_notebook_config.py,但目前我没有3.x版本来测试它。)

相关问题