看起来熊猫获得了._repr_latex_()
0.23版本中的方法。你需要设置pd.options.display.latex.repr=True
来激活它。
Without 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有关设置自定义模板的示例。