LoginSignup
2
3

More than 3 years have passed since last update.

Jupyter Notebook を日本語で書き出す

Posted at

この記事は jupyter notebook を 日本語を含む pdf に書き出す方法についてのものです.

環境

version
OS macOS Big Sur 11.0.1 (Catalina でも動作確認済)
jupyter notebook 6.1.4
jupyter nbconvert 6.0.7
jupyter nbextensions 6.1.4
texlive 2020 (2017 でも動作確認済)
pandoc 2.11

nbconvert をインストールする

conda や pip を使って nbconvert をインストールする.
詳しくは公式ページを参照.

テンプレートを作る

デフォルトでは,notebook を pdf に書き出す際に日本語が埋め込まれない.
これは,.ipynb -> .tex -> .pdf の順に変換を行っているためである.
そのため, latex に変換する際に日本語用のテンプレートを用意する必要がある.

以下のような jinja2 のテンプレートファイル (.tex.j2 多分拡張子はなんでも良い) を作る.名前はなんでもよいが,以下ではja_template.tex.j2 とする.

ja_template.tex.j2(texlive2020)
((*- extends 'index.tex.j2' -*))

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

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

ja_template.tex.j2(texlive2017)
((*- extends 'index.tex.j2' -*))

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

((* block docclass *))
\documentclass[xelatex, 11pt]{bxjsarticle}

\usepackage{zxjatype}
\setjamainfont{ipaexm.ttf}
\setjasansfont{ipaexg.ttf}
\setjamonofont{ipaexg.ttf}
((* endblock docclass *))

texlive 2017 の場合は, ja=standard だと font-not-found のエラーがでた.この場合上記のように対処するとうまくいく.詳しくはtexwikiを参照.

※ 本当は docclass の block の中にフォントの設定を書くのはよろしくないと思いますが,うまく行ってしまったのでちゃんとしたやり方を調べていません...
テンプレートについて詳しく知りたい方は公式ページを参照してください.

ファイルの保存場所カレントディレクトリや ~/.jupyter/ などどこでもよい.

ためしてみる

コマンドラインで以下を実行してうまくいくか確かめる. --no-input はコードを隠すオプションのためお好みで.

jupyter nbconvert --to pdf --no-input --template-file path/to/ja_template.tex.j2 hoge.ipynb

ワンクリックで pdf 化できるようにする

まず nbextensions を入れる.詳しくは公式ページを参照.

jupyter を開き,Nbextensions の設定画面を開く.
Printview を有効にし,nbconvert options に以下を入力する.

--to pdf --no-input --template-file path/to/ja_template.tex.j2

スクリーンショット 2020-11-29 18.49.49.png

あとは pdf 化したい notebook を開いた状態で,プリントマークをクリックしたら OK.

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3