IT管理のマメ知識

ITについて個人の感想をアウトプットしているブログ

JupyterNoteBookからのPDF出力で日本語を表示可能にする(MikTexの設定)

  • 以下のサイトを参考にさせて頂きました。

ZXjatype パッケージ ~XeLaTeX でまともな日本語組版を~

Windowsに「IPAフォント」をインストール

「zxjatype」パッケージのインストール

  • MikTexコンソールから、「zxjatype」パッケージをインストールします

f:id:persimoon:20210120152515j:plain

(Jupyter Notebook側)デフォルトのLatext定義ファイルの編集

  • Jupyter Notebook からPDF出力する際に、既定値として使用される Latext定義ファイルに相当するもの(documentclassを定義しているもの)が存在している筈です。
  • 該当するファイルを検索するために、VSCode等でキーワード「documentclass」を検索します。
    VSCodeの場合:Open Folderで仮想環境パスを開いた後、ctrl+shift+Uキー、キーワードを入力してください。)

  • 筆者の環境の場合、デフォルトのLatext定義ファイルの保存場所は、以下の通りでした。
    (使用している仮想環境名は「test1」です)
    「C:\Anaconda_Datas\envs\test1\share\jupyter\nbconvert\templates\latex\index.tex.j2
(仮想環境パス)\share\jupyter\nbconvert\templates\latex\index.tex.j2
  • 以下のような内容になっています。(Latex Article の部分が対象になります。)
[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 -*))

  • これを、以下のように書き換えます。(ファイルを書き換える前に、必ずバックアップを取っておいてください。) エンコードは「UTF-8」から変更しないようにしてください。
[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[a4paper]{bxjsarticle}
\usepackage{fontspec}
\usepackage{zxjatype}
\setjamainfont{ipam.ttf}
\setjasansfont{ipag.ttf}
\setjamonofont{ipag.ttf}
\usepackage{xltxtra}
((*- endblock docclass -*))

日本語が表示されるようになりました。

  • 定義ファイルを編集する以前は、日本語の部分が表記されずにスキップされていましたが、編集後は、きちんとコメント行の欠落が解消されています。
[編集前]
# Python (欠落部分) datetime (欠落部分) 

[編集後]
# Pythonデータ型は自動でdatetime型に変換されます

左:編集前 右:編集後(日本語表示あり)

外部リンク

IPAフォント

Texパッケージ