Jupyter Notebook PDF输出的中文支持

Jupyter Notebook是什么

Jupyter Notebook是ipython Notebook 的升级。Jupyter能够将实时代码,公式,可视化图表以Cell的方式组织在一起,形成一个对代码友好的笔记本。Jupyter同时支持Markdown语法和LaTeX语法,可以有效输出富文本方式的PDF文档

了解更多: Jupyter Notebook

Jupyter Notebook Users Manual

安装Jupyter Notebook

官方推荐的标准流程:How to Install Jupyter Notebook

我自己是按照下面的顺序安装的,缺少支持的话,pip install xxxxxx

  1. 安装python
  2. 安装ipython by pip pip install ipython
  3. 安装Jupyter by pip pip install jupyter
  4. 安装pandoc
  5. 安装MiKTex

中文支持

Jupyter NoteBook 导出PDF需要Latex支持,但由于默认模版的设定,Latex无法识别中文无法导出。为了解决这个问题,需要将ipynb转为tex,然后修改tex内容,最后由xelatex生成PDF文件。

  • 将ipynb编译为tex
    ipython nbconvert --to latex Example.ipynb
  • 修改tex,增加中文支持

在documentclass{article}后面插入

  • usepackage{fontspec, xunicode, xltxtra}
    setmainfont{Microsoft YaHei}
  • 编译tex,生成pdf
  • xelatex Example.tex

注:这种方式生成的pdf无法进行自动换行!

推荐选择另一种方法——直接修改tex模版文件

用文本编辑器打开article.tplx (Python27Libsite-packages bconvert emplateslatex),修改为documentclass{ctexart}。

生成的pdf如下:百度文库

感谢

本文的解决思路来源于

  1. IPython Notebook 转成 LaTeX 时的中文问题解决方案
  2. 使用XeLaTeX/XeTeX编译中文文档
  3. Ctex宏包
  4. Changing style of PDF-Latex output through IPython Notebook conversion

已知的问题

插入的图片在用xelatex编译时会自动设定为页面宽度。

解决方法:修改tex,加入合适的缩放比例。

includegraphics{figpath}

修改为:

includegraphics[scale = .5]{figpath}

想更了解Latex,推荐阅读包老师的 LaTeX Notes

原文地址:https://www.cnblogs.com/SC-CS/p/Jupyter-PDF_Chinese_Support.html