Jupyter nbconvert LaTex导出主题

4
我正在使用Jupyter Notebook nbconvert(保存菜单)通过Latex导出为pdf。然而,pdf文件的格式不太好。例如,一些宽表格显示得很好。我更喜欢有一个框来调整表格的宽度以适应页面。是否有任何样式、模板可供使用以获得漂亮的报告,并且如何要求nbconverter使用该样式?
这是Latex输出:

enter image description here

我想要这样的东西:

enter image description here

2个回答

5
看起来Pandas在0.23版本中增加了一个`._repr_latex_()`方法。您需要设置`pd.options.display.latex.repr=True`来激活它。
没有latex repr:
Without latex repr
有latex repr:
With latex repr 查看选项以使格式接近您想要的样式。为了完全匹配您想要的输出,您需要使用自定义latex模板。

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

从这里开始获取有关模板的一般信息。您可以在与笔记本相同的路径中创建一个.tplx文件,并在从命令行运行nbconvert时将其指定为模板:!jupyter nbconvert --to python 'example.ipynb' --stdout --template=my_custom_template.tplx。或者,通过修改位于~.jupyter目录中的jupyter_notebook_config.py文件,在导出为Latex时指定默认模板。如果此文件不存在,则可以通过从命令行运行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

为了理解模板的工作原理,首先看一下null.tplx。行((*- for cell in nb.cells -*))循环遍历笔记本中的所有单元格。后面的if语句检查每个单元格的类型并调用适当的块。
其他模板扩展了null.tplx。每个模板定义(或重新定义)一些块。层次结构是null->display_priority->document_contents->base->style_*->article
您的自定义模板应该扩展article.tplx并向标题添加一些Latex命令,以设置您想要的表格。参考this blog post,了解如何设置自定义模板的示例。

太好了!我应该把模板放在哪里?模板的格式是什么,是tex文件吗?有样例吗? @butterwagon - Roo

1
有没有任何设置可以改变表格的大小来适应页面宽度? Latex代码类似于这样:\resizebox*{\textwidth}{!}{%

enter image description here


我也遇到了同样的问题,并且我针对此问题开了一个新的问题链接 - ctrl_z

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接