49

Whenever I try to export a Jupyter notebook as a PDF I get the following error in a separate window:

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.

I am running macOS Sierra 10.12.6.

Things I have tried:

  • Re-installing nbconvert through pip (in the Jupyter notebook) and conda (terminal)
  • Installing xelatex for Mac
  • Re-installing Mactex
  • Installing pandoc (both through pip and through "conda install -c conda-forge pandoc")

Running

!echo $PATH

Yields:

/Users/ed/anaconda/bin:/Users/ed/anaconda/bin:/usr/bin:/bin:/usr/sbin:/sbin

I located the directory containing xelatex as per @einsweniger suggestion here: /usr/local/texlive/bin/x86_64-darwin. I copied and pasted XeLatex into the bin directory above, and get a new error:

nbconvert failed: PDF creating failed, captured latex output: warning: kpathsea: configuration file texmf.cnf not found in these directories: /Users/e/anaconda/bin:/Users/ed/anaconda/bin/share/texmf-local/web2c:/Users/ed/anaconda/bin/share/texmf-dist/web2c:/Users/ed/anaconda/bin/share/texmf/web2c:/Users/ed/anaconda/bin/texmf-local/web2c:/Users/ed/anaconda/bin/texmf-dist/web2c:/Users/ed/anaconda/bin/texmf/web2c:/Users/ed/anaconda:/Users/edefilippis/anaconda/share/texmf-local/web2c:/Users/e/anaconda/share/texmf-dist/web2c:/Users/ed/anaconda/share/texmf/web2c:/Users/ed/anaconda/texmf- This is XeTeX, Version 3.14159265-2.6-0.99999 (TeX Live 2018) (preloaded format=xelatex)

kpathsea: Running mktexfmt xelatex.fmt I can't find the format file `xelatex.fmt'!

I also put xelatex.fmt in the directory, but am still getting the error.

outis
  • 75,655
  • 22
  • 151
  • 221
Parseltongue
  • 11,157
  • 30
  • 95
  • 160
  • 1
    I think you need to make sure that xelatex is on the PATH. Did you check the answers here: https://stackoverflow.com/questions/36916093/convert-ipynb-to-pdf-in-jupyter ? – Abhinav Upadhyay Sep 15 '18 at 18:52
  • Basicslly, as above, why not adding the xelatex path directly? – Evgeny Sep 15 '18 at 20:53
  • 2
    I have installed BasicTeX, which is supposed to contain xelatex. I cannot actually find where it installs xelatex to, however. So I can't put it in my path – Parseltongue Sep 17 '18 at 21:33

14 Answers14

36

I encountered the same issue. As people previously stated, the problem lies in that xlatex isn't found in your PATH environment variable.

A solution that worked for me was to run the following from the notebook:

!export PATH=/Library/TeX/texbin:$PATH

Or in a command line simply:

export PATH=/Library/TeX/texbin:$PATH

And then run the export to pdf from a command line (within your virtual environment if there is one in place) as follows:

 jupyter nbconvert your_notebook.ipynb --to pdf

This should create a pdf of your notebook on the same directory it is running.

Nico
  • 743
  • 7
  • 19
  • Just to add that I only needed to do this once (run de export command once in the terminal), without having to reload it every time a new session is started. – tsando Dec 04 '20 at 21:40
  • 2
    I get a new error: `LaTeX Error: File 'adjustbox.sty' not found.` – ah bon May 14 '21 at 14:21
16

In terminal type

$ sudo apt-get install texlive-xetex texlive-fonts-recommended texlive-plain-generic

just as Nico mentioned. run the following commands

$ export PATH=/Library/TeX/texbin:$PATH

to convert notebook to pdf run

$ jupyter nbconvert your_notebook.ipynb --to pdf
Danial Shabbir
  • 612
  • 8
  • 18
  • 1
    For future reference, after doing this, write in a terminal: `export PATH=/Library/TeX/texbin:$PATH` – Mohamed Jan 21 '23 at 22:36
10

I'm not accustomed with Mac install paths, from what the basictex faq says, I gather the executeables should live in /usr/local/texlive/bin/x86_64-darwin Running which xelatex in a terminal might also help finding where the command is, but that only works if the directory is already in your PATH variable.

Another method would be running find / -name 'xelatex' if it is not within your PATH so you might find it that way (might take a while as this will search you whole harddrive).

Once you've found it and added the path to your PATH variable, you should also check if the jupyter has the correct PATH by running

import os
print(os.environ['PATH'])

within a notebook.

einsweniger
  • 611
  • 6
  • 16
  • Thanks so much for the help. I have identified that the Latex path is not in my Python PATH, so I just copied and pasted all the requisite files into the "/Users/ed/anaconda/bin" directory, which is in my path. Unfortunately, this still doesn't work, as I get the error that I appended to my question. Any thoughts on how to add the path? I tried editing my bash profile and following multiple tutorials on it, and nothing actually changes the path. – Parseltongue Sep 18 '18 at 22:04
  • 1
    Don't go copying executables around to different directories. You'll just make a worse mess of things, possibly break things in unknowable ways. `PATH` is an environment variable. It's associated with your login session and its value is passed along to other programs (including, but not limited to, Python). If a program like xelatex needs to be on your PATH the solution is not to copy files into a directory that happens to already be on your `PATH`. The solution is to update your `PATH` variable itself before starting Python. Possibly even permanently. – Iguananaut Sep 18 '18 at 22:53
  • Maybe try this guide for adding the path where the files were originally https://www.tech-recipes.com/rx/2621/os_x_change_path_environment_variable/ – einsweniger Sep 19 '18 at 07:05
  • For Windows users, use `where xelatex`. – datalifenyc Dec 04 '19 at 15:35
3

I know this question is long answered, but I just ran into this issue and found a very easy, codeless solution: print to PDF.

Thought I might post this here for anyone else who doesn't feel like figuring out path variables or updating a zillion different things. Your browser's print function probably works just fine.

eva bacas
  • 65
  • 4
2

On mac, you can install mactex using homebrew with the following: brew install --cask mactex

Yaakov Bressler
  • 9,056
  • 2
  • 45
  • 69
1

I've run into similar issues in the past with paths in python when using tensorflow in Windows and Linux. As is pointed out by others, using PATH is the way to go (i.e update in .bashrc if it was linux), but I've found the code below to resolve path issues on a script by script basis.

import sys
if "\your\path\to\xelatex" not in sys.path:
    print('adding path') # I just add this to know if the path was present or not.
    sys.path.append("\your\path\to\xelatex")

essentially would check PATH for what it is you are looking for and then adds it if it is missing. You probably don't even need the 'if' statement.
Perhaps not the most practical way has worked well for me where I know the path in question is perhaps a 'rarely used' or 'one time use' PATH add...

So as was pointed out in comments above (and I think you already have) find the location of the file and add its location using the above code.

Some more reading on it: https://docs.python.org/3/tutorial/modules.html 6.2 Standard Modules

IamSierraCharlie
  • 504
  • 5
  • 10
1

If (You are using jupyter the easier way I found was to)

  • Download the file in latex.
  • Upload it to overleaf.
  • And then download it as a pdf

Else if ( You are using google collab)

  • Download as ipynb
  • Upload to jupyter and repeat the above steps.
1

How to install latex and xelatex on Mac so that Jupyter "Download as PDF" will work

brew install pandoc
brew tap homebrew/cask
brew cask install basictex
eval "$(/usr/libexec/path_helper)"
# Update $PATH to include `/usr/local/texlive/2020basic/bin/x86_64-darwin`
sudo tlmgr update --self
sudo tlmgr install texliveonfly
sudo tlmgr install xelatex
sudo tlmgr install adjustbox
sudo tlmgr install tcolorbox
sudo tlmgr install collectbox
sudo tlmgr install ucs
sudo tlmgr install environ
sudo tlmgr install trimspaces
sudo tlmgr install titling
sudo tlmgr install enumitem
sudo tlmgr install rsfs

More info here

dembelet
  • 11
  • 1
1

Windows users can save the .ipynb file as a HTML file and print the HTML file to save it as PDF with custom settings.

0

If there is an update available maybe try and update Jupyter

0

Add "env": {"PATH":"$PATH"} to your kernel.json, for me that was in /opt/anaconda3/share/jupyter/kernels/python3/kernel.json.

JupyterLab or Anaconda seems to override the system PATH by default, if you set this then the PATH will be the same in JupyterLab and in your shell, so if you have anything custom installed like xelatex, it will show up in JupyterLab.

0

Run this command in your jupyter terminal (or in your environment if you have any), close all jupyter notebook tabs and reopen it then convert the notebook to pdf

pip install --upgrade --user nbconvert
0

Close all the anaconda runing programs and then,open conda command prompt and try the following command: conda install -c conda-forge pandoc

-1
  1. Update/Reinstall Anaconda if not updated.
  2. now install chromium through link https://chromium.woolyss.com/download/en/ and make it default browser.
  3. Then run jupyter notebook using this browser.
  4. Lastly use option "Download as" in jupyter notebook and then select the option using PDF via pyppeteer (.html)

HOLA!! It worked..

Supriya
  • 1
  • 2