Japanese support for Jupyter PDF output (December 2020 version)

I wrote an article "Download Japanese Jupyter Notebook as PDF" (https://qiita.com/masa-ita/items/8d5ebe8afe0d580af184) more than two years ago, but it seems that it has become useless due to specification changes such as nbconvert. Therefore, I will write the revised version.

TeX installation

For Ubuntu

It seems that you should install the following modules.

sudo apt install texlive-lang-japanese
sudo apt install texlive-xetex
sudo apt install pandoc

For Mac

I think it's easiest to install MacTex. In my case, I used MacPorts, so I used the ghostscript installed by MacPorts.

Modify template file

Japanese characters are not displayed in the template attached to nbconvert, so it needs to be corrected.

Placement destination of template file

The template file used to be in the nbconvert installation directory, but now it seems that the directory where it is placed changes depending on the environment. Running the jupyter --path command will bring up a list of directories where jupyter will search for data files, so it's a good idea to look for these locations.

# jupiter --Execution example of path
$ jupyter --path
config:
    /home/basicuser/.jupyter
    /usr/local/etc/jupyter
    /etc/jupyter
data:
    /home/basicuser/.local/share/jupyter
    /usr/local/share/jupyter
    /usr/share/jupyter
runtime:
    /home/basicuser/.local/share/jupyter/runtime

One of the directories listed under data: above is the location. In this example /home/basicuser/.local/share/jupyter/nbconvert/templates/latex The TeX template file (extension .tex.j2) was placed in.

Changes

The contents of the template file index.tex.j2 file,

tex:index.tex.j2


((=- Default to the notebook output style -=))
((*- if not cell_style is defined -*))
    ((* set cell_style = 'style_jupyter.tex.j2' *))
((*- endif -*))

((=- Inherit from the specified cell style. -=))
((* extends cell_style *))


%===============================================================================
% Latex Article
%===============================================================================

((*- block docclass -*))
\documentclass[11pt]{article}
((*- endblock docclass -*))

From, change as follows.

tex:index.tex.j2


((=- Default to the notebook output style -=))
((*- if not cell_style is defined -*))
    ((* set cell_style = 'style_jupyter.tex.j2' *))
((*- endif -*))

((=- Inherit from the specified cell style. -=))
((* extends cell_style *))


%===============================================================================
% Latex Article
%===============================================================================

((*- block docclass -*))
\documentclass[xelatex,ja=standard]{bxjsarticle}
((*- endblock docclass -*))

Be careful

In the case of a virtual environment such as venv, the installation destination of the template file also differs for each virtual environment. For example, in the case of venv venv/share/jupyter/nbconvert/templates/latex Was placed in.

Also, the location of the template file can be specified in jupyter_nbconvert_config.json. It seems that this file may have been generated in the directories listed in config: of jupyter --path above. If you can't find it in the standard location, check for this file.

Referenced site

How to save Jupyter Notebook as PDF (Japanese)

Recommended Posts

Japanese support for Jupyter PDF output (December 2020 version)
Output Django Detail View as PDF (Japanese support)
tesseract-OCR for Python [Japanese version]
[Visual Studio Code] [Python] [Windows] Support for garbled Japanese characters in Python in VS Code task / debug output