Jupyter nbconvert LaTex 导出主题

2024-01-01

我正在使用 Jupyter Notebook nbconvert(另存为菜单)通过 Latex 导出为 pdf。然而,pdf 文件的状态并不好。例如,一些宽表显示得很好。我希望有一个用于将表格大小调整为页面宽度的框。是否有任何样式、模板可以用来生成漂亮的报告以及如何要求 nbconverter 使用该样式?

这是乳胶输出:

我想要这样的东西:


看起来熊猫获得了._repr_latex_()0.23版本中的方法。你需要设置pd.options.display.latex.repr=True来激活它。

Without latex repr:
Without latex repr
With latex repr:
With latex repr

查看options https://pandas.pydata.org/pandas-docs/version/0.23/options.html#options-available使格式接近您想要的。为了准确匹配您所需的输出,您需要使用自定义乳胶模板。


编辑以提供有关模板的更多信息:

从这里开始 https://nbconvert.readthedocs.io/en/latest/customizing.html有关模板的一般信息。您可以创建一个.tplx文件与您的笔记本位于同一路径中,并在运行时将其指定为模板nbconvert从命令行:!jupyter nbconvert --to python 'example.ipynb' --stdout --template=my_custom_template.tplx。或者,您可以通过菜单指定导出为 Latex 时使用的默认模板,方法是修改jupyter_notebook_config.py文件在你的~.jupyter目录。如果该文件尚不存在,您可以通过运行命令生成它jupyter notebook --generate-config从命令行。我的模板位于~/.jupyter目录也是如此,所以我将以下内容添加到我的jupyter_notebook_config.py:

# Insert this at the top of the file to allow you to reference
# a template in the ~.jupyter directory
import os.path
import sys
sys.path.insert(0, os.path.expanduser("~") + '/.jupyter')
# Insert this at the bottom of the file:
c.LatexExporter.template_file = 'my_template' # no .tplx extension here
c.LatexExporter.template_path = ['.', os.path.expanduser("~") + '/.jupyter'] # nbconvert will look in ~/.jupyter

要了解一些模板的工作原理,请首先查看空.tplx https://github.com/jupyter/nbconvert/blob/master/nbconvert/templates/latex/skeleton/null.tplx。线路((*- for cell in nb.cells -*))循环遍历笔记本中的所有单元格。这if接下来的语句检查每个单元格的类型并调用适当的块。

其他模板扩展null.tplx。每个模板定义(或重新定义)一些块。层次结构是null->display_priority->document_contents->base->style_*->article.

您的自定义模板可能应该扩展article.tplx并将一些 Latex 命令添加到标题中,以按照您想要的方式设置表格。看一眼这篇博文 http://blog.juliusschulz.de/blog/ultimate-ipython-notebook有关设置自定义模板的示例。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Jupyter nbconvert LaTex 导出主题 的相关文章