Jupyter nbconvert LaTex导出主题 [英] Jupyter nbconvert LaTex Export Theme
问题描述
我正在使用Jupyter Notebook nbconvert(另存为菜单)通过Latex导出为pdf.但是,pdf文件格式不佳.例如,很好地显示了一些宽表.我希望有一个用于将表调整为页面宽度的框.是否可以使用任何样式,模板来制作精美的报告,以及如何要求nbconverter使用该样式?
I am using Jupyter Notebook nbconvert (Save as menu) to export as pdf via Latex. However, the pdf file is not in a good shape. For example, some wide tables are shown well. I would prefer to have a box for tables to be resized to the width of the page. Is there any style, template that I can use to have nice reports and how I may ask nbconverter to use that style?
这是Latex输出:
我想要这样的东西:
推荐答案
类似Pandas的版本在0.23版中获得了._repr_latex_()
方法.您需要将pd.options.display.latex.repr=True
设置为激活它.
Looks like Pandas gained a ._repr_latex_()
method in version 0.23. You'll need to set pd.options.display.latex.repr=True
to activate it.
没有乳胶代表:
与乳胶代表:
Without latex repr:
With latex repr:
查看选项使格式接近您想要的格式.为了完全匹配所需的输出,您需要使用自定义的乳胶模板.
Check out the options to get the formatting close to what you want. In order to match your desired output exactly, you'll need to use a custom latex template.
进行编辑以提供有关模板的更多信息:
Edited to provide more information on templates:
从此处开始以获取有关模板的一般信息.您可以在与笔记本相同的路径中创建一个.tplx
文件,并在以下命令行中运行nbconvert
时将其指定为模板:!jupyter nbconvert --to python 'example.ipynb' --stdout --template=my_custom_template.tplx
.另外,您可以通过修改~.jupyter
目录中的jupyter_notebook_config.py
文件,通过菜单指定导出为乳胶时要使用的默认模板.如果此文件尚不存在,则可以通过从命令行运行命令jupyter notebook --generate-config
来生成它.我的模板也位于~/.jupyter
目录中,因此我在jupyter_notebook_config.py
中添加了以下内容:
Start here for general information about templates. You can create a .tplx
file in the same path as your notebook and specify it as the template when running nbconvert
from the command line: !jupyter nbconvert --to python 'example.ipynb' --stdout --template=my_custom_template.tplx
. Alternatively, you can specify a default template to use when exporting as Latex via the menu by modifying the jupyter_notebook_config.py
file in your ~.jupyter
directory. If this file doesn't exist already, you can generate it by running the command jupyter notebook --generate-config
from the command line. I have my template sitting in the ~/.jupyter
directory as well, so I added the following to my 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
To understand a bit about how the templates work, start by taking a look at null.tplx. The line ((*- for cell in nb.cells -*))
loops over all the cells in the notebook. The if
statements that follow check the type of each cell and call the appropriate block.
其他模板扩展null.tplx
.每个模板定义(或重新定义)一些块.层次结构为null->display_priority->document_contents->base->style_*->article
.
The other templates extend null.tplx
. Each template defines (or redefines) some of the blocks. The hierarchy is null->display_priority->document_contents->base->style_*->article
.
您的自定义模板可能应该扩展article.tplx
,并向标头中添加一些Latex命令,以所需的方式设置表.在此博客文章中查看有关设置自定义模板的示例
Your custom template should probably extend article.tplx
and add some Latex commands to the header that sets up the tables the way you want. Take a look at this blog post for an example of setting up a custom template.
这篇关于Jupyter nbconvert LaTex导出主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!