'How to have convert notebook command support Chinese character on Mac? I am willing to contribute some code to jupyter notebook

I am running jupyter notebook on Mac.

This command converts a notebook file that does not contain Chinese character to pdf successfully.

jupyter nbconvert test.ipynb --to pdf

When dealing with a notebook file that contains Chinese characters, all the Chinese characters are missing in the generated pdf file.

Is there a way to fix this?

I am willing to contribute some code to jupyter notebook project for this, I just don't know where to start.



Solution 1:[1]

I ran across this question while researching a similar problem on Windows. Here's my solution.

(1) I converted the ipynb file to latex using NBConvert via command line

jupyter nbconvert --to latex "your_path_to_doc.ipynb"

(2) I installed the xecjk package using MikTeX

(3) I opened the .tex file from (1) in TeXworks

(4) I inserted

\usepackage[slantfont, boldfont]{xeCJK}
\setCJKmainfont{Microsoft YaHei}

into to the document preamble.

(5) I compiled the document to pdf using XeLaTeX+MakeIndex+BibTeX in TeXworks.

You're on MacOS, so your font will differ, as will (I imagine) how you install xecjk.

Hope this helps.

Solution 2:[2]

jupyter uses nbconvert to convert notebook with template to PDF files with process like:

notebook -(nbconvert)-> latex -(XeLatex)-> PDF

The template that the nbconvert used do not support Chinese, causing the problem. By adding package like xeCJK and its font in the template, the problem solved.

You can find the base template under ...share/jupyter/nbconvert/templates/latex and named base.tex.j2. Add the following snippet in the template file to make it support Chinese (MacOS):

    \usepackage{xeCJK}
    \setCJKmainfont{STSong}

If you want to contribute, you can read the nbconvert document here and check the open source code on Github:Jupyter/nbconvert

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel Keller
Solution 2 Lushang