NBConvert to PDF: works from command line, doesn't work from Notebook Interface

Hello,

I’m trying to get the notebook -> LaTeX -> PDF conversion to work on a Windows 10 environment with

  • Jupyter 4.4.0 with
    • nbconvert 5.4.1; and
    • nbextensions 0.5.1
  • Pandoc 1.19.2.1
  • Anaconda 4.6.14
  • Python 3.7.3
  • MiKTeX 2.9

I was able to get it to work on the command line ( jupyter nbconvert notebook.ipynb --to pdf). At first I received an error saying xelatex was not on the PATH. After some investigation, I determined that the issue was with my MiKTeX path, which has a Unicode character on it. In order to solve that, I

  1. set up a directory junction C:\Users\example (no Unicode chars) to point to C:\Users\exãmple; and
  2. used ~\.jupyter\jupyter_nbconvert_config.py to point to the non-Unicode paths:
    c.PDFExporter.latex_command = [ r'C:\Users\example\AppData\Local\Programs\MiKTeX 2.9\miktex\bin\x64\xelatex.exe', '{filename}']
    c.PDFExporter.bib_command = [ r'C:\Users\example\AppData\Local\Programs\MiKTeX 2.9\miktex\bin\x64\bibtex.exe', '{filename}']
    

However, when converting from the Notebook interface (clicking on menu File -> Download as -> PDF via LaTeX), the conversion fails with a xelatex not on PATH error, the same as when initially trying to convert from the command line. So it seems as though nbconvert ignores the configuration file when called from the interface.

Do you guys have any tips on how to get the PDF conversion from the interface to work?

Cheers!

I have found this solution for me. This code snippet build a pdf file from the JupyterLab. If you use Docker you can take a look at my https://hub.docker.com/r/joergklein/jupyterhub repo. I will make an update in one hour with some additional Latex packages.

library(knitr)
library(kableExtra)
library(IRdisplay)

kable(alpha, format = "html", caption = "Caption", longtable = TRUE) %>%
kable_styling(c("striped", "bordered", "hover", "condensed", "responsive"), full_width = F, font_size = 12 ) %>%
save_kable(file = "alpha.html")
as.character() %>%
display_html()

pandoc('alpha.html', format='latex')
1 Like

I’m glad you found a solution, but I just wanted to mention an alternative that might be useful for people who aren’t using R.

I was getting a similar error when trying to export notebooks in Jupyter Lab as PDFs even after trying to follow the instructions for setting up nbconvert. The issue was that even though xelatex was on my system path, it wasn’t ending up on my path in the kernel where it needed to be.

I fixed this by setting the PATH environment variable in my kernel.json file. You can locate this by running the command jupyter kernelspec list and editing the kernel.json file. It ended up looking something like this:

{
“argv”: [
“/opt/conda/bin/python”,
“-m”,
“ipykernel_launcher”,
“-f”,
“{connection_file}”
],
“env”: {“PATH”:“/opt/conda/bin:/opt/conda/condabin:/usr/local/texlive/2019/bin/x86_64-linux:/usr/local/bin:/usr/bin:/usr/local/sbin”},
“display_name”: “Python 3”,
“language”: “python”
}

Once I added that and restarted the kernel I was able to export to PDF.

I’m honestly am not quite sure if this is the best way to do this, and I’ve found it pretty difficult to identify the right areas of the documentation on this. My kernel.json file didn’t have any path specified before I added it, so I don’t know where it was getting set in the first place or why it didn’t match my user’s path. Maybe something to do with how I’m launching jupyter lab in the first place?

If anybody knows where this is documented for other people who run into this issue I’d love to know so I can understand it better.

1 Like

Each Jupyter app has it’s own config file. To set nbconvert configuration inside of the notebook add these configuration options to the ~\.jupyter\jupyter_notebook_config.py file. Or reference the nbconvert config file from the notebook config file with load_subconfig. This is the proper/official way to do this.

1 Like