5

When I want to save Jupyter notebook as a pdf file, I receive the following error:

nbconvert failed: PDF creating failed, captured latex output:
This is XeTeX, Version 3.14159265-2.6-0.99999 (TeX Live 2018/W32TeX) (preloaded format=xelatex)
 restricted \write18 enabled.
entering extended mode
! Undefined control sequence.
<*> .\notebook
              .tex
? 
! Emergency stop.
<*> .\notebook
              .tex
No pages of output.
Transcript written on ?.
xuan
  • 59
  • 5
  • You should probably use `./notebook.tex`, not `.\notebook.tex`. – Werner Nov 23 '18 at 18:13
  • The error is made by the Jupyter notebook. I have just written some Python codes in Jupyter and when I want to save notebook as a pdf file, it shows me the error – xuan Nov 23 '18 at 18:49
  • 1
    @Werner The notice `! Undefined control sequence`, followed by the command in question, in this case `\notebook`, is LaTeX's way of telling the user, that they have not defined the command being called. Jupyter clearly tries to call said command when running LaTeX in order to create a PDF, but fails. This is because Jupyter's authors have not included this essential Jupter-specific command needed to produce PDFs. An oversight, probably. – sesodesa Mar 17 '19 at 18:04
  • Or maybe there's a Jupyter-related package included in either TeXLive or MikTeX, that @xuan has not installed? – sesodesa Mar 17 '19 at 18:22
  • @TheSodesa: I understand; I have some experience with LaTeX. This seems like a typo though... – Werner Mar 18 '19 at 15:33

2 Answers2

1

This is due to an unfortunate incompatibility between Windows and Unix-like systems. Jupyter creates a temporary notebook.tex document in the current directory and then calls XeTeX to compile this document.

XeTeX tries to parse the command line with roughly the following algorithm after being called:

  1. XeTeX <arg1> <arg2> ...
  2. Check if <arg1> is a .tex file,

    a) if it is a file compile it

    b) if it not a file assume this is the start of an actual document given on the command line

On Unix-like systems ./notebook.tex is parsed by XeTeX into current_directory/notebook.tex which exists and is then compiled. On Windows systems the command line because of the different directory separator is .\notebook.tex which XeTeX does not recognize as a file and therefore tries and fails to parse as a LaTeX document.

A temporary remedy (could break other things) is to change the build directory in pdf.py from nbconvert (line 66) from

writer = Instance("nbconvert.writers.FilesWriter", args=(), kw={'build_directory': '.'})

to

writer = Instance("nbconvert.writers.FilesWriter", args=(), kw={'build_directory': ''})

Alexander
  • 2,174
  • 3
  • 16
  • 25
0

As an emergency solution, you can save the notebook as markdown (.md) and open it in RStudio, where you can Knit it and save it as pdf.

ForEverNewbie
  • 357
  • 2
  • 10