基本コマンド

これは英語版.

jupyter-nbconvert --to latex foo.ipynb
extractbb foo_files/*.png

テンプレート使う

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

jupyter-nbconvert --to latex foo.ipynb --template bar.tplx

テンプレートの書き方

Jupyter notebookがインストールしてあれば,
PYTHONDIR/site-packages/nbconvert/templates/latex/
あたりにデフォルトテンプレートがあるはず.
今回は article.tplx を参考にして,日本語pLaTeX用テンプレート 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.tplx を foo.tex とおなじディレクトリに置けばOK.

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.

jupyter-nbconvert --to latex foo.ipynb --template style_notebook.tplx
extractbb foo_files/*.png
platex foo.tex
dvipdfmx foo.dvi

呼び出し構造

style_notebook.tplx で ((* extends ‘jsarticle.tplx’ *))

jsarticle.tplx で ((* set cell_style = ‘style_ipython.tplx’ *))

style_ipython.tplx で ((*- extends ‘base.tplx’ -*))

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

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

% 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 *))
bannerAds