26

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.

Parseltongue
  • 9,008
  • 26
  • 78
  • 131
  • 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

7 Answers7

20

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
  • 551
  • 4
  • 15
  • 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
  • I get a new error: `LaTeX Error: File 'adjustbox.sty' not found.` – ah bon May 14 '21 at 14:21
8

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
  • 591
  • 5
  • 14
  • 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
  • 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
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

0

If there is an update available maybe try and update Jupyter

Xnero
  • 1,698
  • 6
  • 14
  • 28
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

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

Yaakov Bressler
  • 4,437
  • 2
  • 19
  • 41
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
Sidrah
  • 488
  • 6
  • 11