python - Jupyter nbconvert LaTex 导出主题

标签 python css latex jupyter-notebook nbconvert

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

这是 latex 输出:

enter image description here

我想要这样的东西:

enter image description here

最佳答案

看起来 Pandas 在 0.23 版本中获得了 ._repr_latex_() 方法。您需要设置 pd.options.display.latex.repr=True 才能激活它。

没有 latex 代表:
Without latex repr
与 latex 代表:
With latex repr

查看 options使格式接近您想要的。为了完全匹配您所需的输出,您需要使用自定义 latex 模板。

<小时/>

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

Start here有关模板的一般信息。您可以在与笔记本相同的路径中创建一个 .tplx 文件,并在从命令行运行 nbconvert 时将其指定为模板: !jupyter nbconvert --到 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 语句检查每个单元格的类型并调用适当的 block 。

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

您的自定义模板可能应该扩展 article.tplx 并向 header 添加一些 Latex 命令,以按照您想要的方式设置表格。看看this blog post有关设置自定义模板的示例。

关于python - Jupyter nbconvert LaTex 导出主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52501459/

相关文章:

python - Oracle 11g - 即使有 NOCACHE 提示,查询似乎也会缓存

javascript - Angularjs 更改 mouseenter 上的类(悬停)

python - 两个 Python 2.7 Mac OS X 磁盘镜像安装程序之间有什么区别?

python - 如何将输出值转换为字典

javascript - 在 javascript/jquery 中更改视频源

html - 如何在图片的右上角放置按钮

latex - Pandoc 使用 Markdown 正文文本和 YAML 元数据文件生成 PDF 时出错

r - pdf、docx、html 输出中的 bookdown 中数学方程编号一致

matplotlib - Matplotlib-在LateX表达式中使用变量

python - 如何获取scrapy失败的URL?