Convert ipynb to pdf in Jupyter

2019-03-18 20:30发布

问题:

I am new to ipython notebook, and I would like to convert my ipynb to pdf. But I get the following error when I try to Download as PDF via LaTex.

nbconvert failed: pdflatex not found on PATH

There is no documentation anywhere how to add pdflatex to my PATH. I use windows. Thank you!

回答1:

As said by Thomas K in the comments, you need to have Latex installed, and after add the path to the directory containing pdflatex.exe file to the PATH variable of your system.

I have looked for a lightweight distribution and tried installing TeXworks, but I didn't find any pdflatex.exe file.
So I have tried TeX Live, which worked fine creating the pdflatex.exe file under the target installation directory. This path should be like C:\...\texlive\2016\bin\win32.
Finally, you should just add this path to the PATH environment variable of your system (you can use the link shared by Thomas K).

In Jupyter, you can check your environment variables by running the following (refer to this link for details):

    import os
    os.environ['PATH']

and check if it contains the path to pdflatex.exe file.

If you get some trouble when exporting your notebook to pdf due to missing files/packages (this happened to me), refer to this link to search and install them under TeX Live.



回答2:

A simple and surprisingly good solution is to print the notebook to pdf through the browser with ctrl+p. Just make sure your plots and figures are not on interactive mode otherwise they will not be displayed (set them to %matplotlib inline).

Exporting jupyter notebooks through latex is quite troublesome and takes a lot of tinkering to get something remotely close to publish ready. When I absolutely need publication quality I do it on a latex editor, but this tutorial goes in great length about doing it on jupyter.

A few useful tips to get better results:

  • Higher resolution plots
  • Hide your code-cells from the pdf
  • Take a look at these extensions to improve your jupyter documents


回答3:

For Linux, the reported error is due to the lack of XeLatex, part of the texlive-xetex package.

Installation in ubuntu will be: sudo apt install texlive-xetex



回答4:

For Mac OS X, the solution for me was to install MacTex first and then export the path to find it:

### TeX
export PATH="/Library/TeX/Distributions/.DefaultTeX/Contents/Programs/texbin:$PATH"

You can add this to your .bash_profile or similar config file to load it every time.

See more here https://github.com/jupyter/nbconvert/issues/406