LoginSignup
39
37

More than 5 years have passed since last update.

jupyter notebookをLaTeXに変換

Last updated at Posted at 2015-02-05

基本コマンド

これは英語版.

TeXへ変換
jupyter-nbconvert --to latex foo.ipynb
画像のbb作成
extractbb foo_files/*.png

テンプレート使う

テンプレート bar.tplx があれば次のようにする.

TeXへ変換
jupyter-nbconvert --to latex foo.ipynb --template bar.tplx

テンプレートの書き方

Jupyter notebookがインストールしてあれば,
PYTHONDIR/site-packages/nbconvert/templates/latex/
あたりにデフォルトテンプレートがあるはず.

今回は article.tplx を参考にして,日本語pLaTeX用テンプレート jsarticle.tplx を作る.

jsarticle.tplx
% Default to the notebook output style
((* if not cell_style is defined *))
    ((* set cell_style = 'style_ipython.tplx' *))
((* endif *))

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

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

((* block docclass *))
\documentclass[a4paper,dvipdfmx]{jsarticle}
((* endblock docclass *))

この jsarticle.tplxfoo.tex とおなじディレクトリに置けばOK.

日本語pLaTeXコンパイル手順
jupyter-nbconvert --to latex foo.ipynb --template bar.tplx
extractbb foo_files/*.png
platex foo.tex
dvipdfmx foo.dvi

これで foo.pdf の出来上がり.

かっこいいテンプレートの使い方

テンプレート集から以下のテンプレートを利用する.
https://github.com/ipython/nbconvert-examples/blob/master/notebook_cell_style/style_notebook.tplx

冒頭部を以下のように書き換える.

style_notebook.tplx
--- style_notebook.tplx 2015-02-04 09:31:00.000000000 +0900
+++ style_notebook.tplx 2015-02-04 11:13:14.000000000 +0900
@@ -1,6 +1,6 @@
 ((= Notebook input/output style =))

-((* extends 'latex_base.tplx' *))
+((* extends 'jsarticle.tplx' *))

 % Custom packages
 ((* block packages *))    

で,このテンプレートを使うなら以下でOK.

日本語pLaTeXコンパイル手順
jupyter-nbconvert --to latex foo.ipynb --template style_notebook.tplx
extractbb foo_files/*.png
platex foo.tex
dvipdfmx foo.dvi

呼び出し構造

  1. style_notebook.tplx((* extends 'jsarticle.tplx' *))
  2. jsarticle.tplx((* set cell_style = 'style_ipython.tplx' *))
  3. style_ipython.tplx((*- extends 'base.tplx' -*))

style_ipython.tplxbase.tplxPYTHONDIR/site-packages/IPython/nbconvert/templates/latex/ にある.
いろいろ手を加えたければ,上位のテンプレートを見て,その中で必要な部分を抜き出して修正して
jsarticle.tplx の最後の方に付け加えればOK.

例えばfigure環境の部分を変えたければ,base.tplx から以下の部分を抜き出して
jsarticle.tplx の最後の方に付け加える.

figure環境の生成部分.ここではmaxsizeを変更している.
% Draw a figure using the graphicx package.
((* macro draw_figure(filename) -*))
((* set filename = filename | posix_path *))
((*- block figure scoped -*))
    \begin{center}
    \adjustimage{max size={0.3\linewidth}{0.2\paperheight}}{((( filename )))}
    \end{center}
    { \hspace*{\fill} \\}
((*- endblock figure -*))
((*- endmacro *))
39
37
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
39
37