Jupyter直接转pdf(含中文)

今天突发奇想,想直接将Jupter转换pdf,之前一直是先转成md再转pdf(傻不傻),第一次转换没有出现中文,因为之前也有使用过latex的一点经验,感觉这是模板的问题,在问了ai之后,给出来以下解决方案。

需要安装Jupyter nbconvert 中文 PDF 模板(github地址' https://github.com/AllanChain/nb-tmpl-ctex 给作者点个免费的星星吧)

安装

pip install nb-tmpl-ctex

调用

使用,通过 nbconvert 调用:

# 默认 ctexart
jupyter nbconvert example.ipynb --to pdf --template ctex
# 使用 ctexrep
jupyter nbconvert example.ipynb --to pdf --template ctex --template-file report

默认使用

要让 Jupyter Notebook 在使用 jupyter nbconvert 命令时自动使用 CTeX 模板,可以通过配置文件来设置默认模板。这样,你就不需要每次在命令行中手动指定 --template ctex 选项。

查找或创建 Jupyter 配置文件:

查找或创建 Jupyter 配置文件:

默认情况下,Jupyter 配置文件位于用户主目录下的 .jupyter 文件夹中。配置文件名为 jupyter_nbconvert_config.py。
如果该文件不存在,你可以通过以下命令创建一个:

jupyter nbconvert --generate-config

编辑配置文件

这将在 ~/.jupyter 目录下生成一个 jupyter_nbconvert_config.py 文件。
Ctrl+F搜索c.LatexExporter.template_name,在下面一行添加

c.LatexExporter.template_name = "ctex"
# c.LatexExporter.template_file = "report"

验证配置

image

搞定完事,撒花结尾

posted @ 2025-10-19 15:55  zrc2024  阅读(30)  评论(0)    收藏  举报