物理の駅 Physics station by 現役研究者

テクノロジーは共有されてこそ栄える

Windows上のPython Jupyter LabでPDF出力したときの500 : Internal Server Error

PythonのJupyter Lab等でPDF出力するには、pandocを使ってLaTeXファイルを生成してから、それをXeLaTeXを通してPDF化するという手順を踏む。

pandocをインストールしていないと、

500 : Internal Server Error
The error was:

nbconvert failed: Pandoc wasn't found.
Please check that pandoc is installed:
https://pandoc.org/installing.html

と出るので、pandocをインストールする。Installing pandoc simplest wayでインストールすると、PATHも通してくれる。

XeLaTeXにPathが通っていないと、

500 : Internal Server Error
The error was:

nbconvert failed: xelatex not found on PATH, if you have not installed xelatex you may need to do so. Find further instructions at https://nbconvert.readthedocs.io/en/latest/install.html#installing-tex.

というエラーが出る。LaTeXのインストールについては、下記の記事たちを参考にして欲しい。

LaTeX カテゴリーの記事一覧 - 物理の駅 by 現役研究者

where xelatex でpathが帰ってくればXeLaTeXにPathが通っている。これで、PDFでExportできる。

日本語が出力できない問題については次の記事を参考にして欲しい。

Jupyter Labで日本語を含むNotebookをXeLaTeXでPDF化 - 物理の駅 by 現役研究者


なお、過去のjupyterのバージョン (nbconvert 5.6.1)だと、PDFでExportすると、Windowsではエラーが帰って来た。最新版では実行する必要がない。

500 : Internal Server Error
The error was:

nbconvert failed: PDF creating failed, captured latex output:
Failed to run "xelatex .\notebook.tex -quiet" command:
This is XeTeX, Version 3.14159265-2.6-0.999991 (TeX Live 2019/W32TeX) (preloaded format=xelatex)
 restricted \write18 enabled.
entering extended mode
! Undefined control sequence.
<*> .\notebook
              .tex -quiet
? 
! Emergency stop.
<*> .\notebook
              .tex -quiet
No pages of output.
Transcript written on ?.

このエラーは、LinuxとWindowsのPath指定の違いによるものである。修正方法は、Githubで議論中らしいが結論は出ていない。

github.com

対処療法としては、 \Lib\site-packages\nbconvert\exporters\pdf.py の98行目(当方の環境における)

        command = [c.format(filename=filename) for c in command_list]

        command = [c.format(filename=filename.replace("\\","/")) for c in command_list]

に変える良い。とりあえずコンパイルはできるようになる。