13

I'm working on my thesis using LaTeX (via overleaf.com). I have a number of Jupyter Notebooks (formerly IPython Notebook) that I would like to include as appendices. I've tried 2 approaches:

1) Using nbconvert to convert the notebook to LaTeX and putting it into my project using \include{}.

2) Using nbconvert to make pdfs and include them with pdfpages.

Option 1 is problematic because the tex output has lots of preamble statements (like \usepackage) that I need to move in order to get to document to build. Option 2 is difficult because I can't figure out how to get the included pdf pages to be numbered correctly or to show up in the table of contents.

Essentially, I'm just wondering if anyone out there has figured out a good way (or even a not-so-good way that at least works) to include Jupyter notebooks as pages in large LaTeX documents.

Background

Jupyter Notebook is a "a web application that allows you to create and share documents that contain live code, equations, visualizations and explanatory text." In my case, I'm using it to provide demonstrations of Python code for optical remote sensing so there's Python code, explanatory text, and outputs (tables, images, plots, etc.).

Jupyter has built in functionality that, as I understand it, uses pandoc to convert it's normal html output into LaTeX. However, it seems there are some limitations in that process that are preventing me from generating LaTeX that is suitable for my purpose. I'm relatively new to using LaTeX so I'm having trouble even coming up with a sensible way to approach the problem.

7

For Option 2, does it do what you want if you use something like this?

\includepdf[
    %% Include all pages of the PDF
    pages=-,
    %% make this page have the usual page style
    %% (you can change it to plain etc). By default pdfpages
    %% sets the pagecommand to \pagestyle{empty}
    pagecommand={\pagestyle{headings}},  
    %% Add a "section" entry to the ToC with the heading
    %% "Quilling Shapes" and the label "sec:shapes"
    addtotoc={1,section,1,Quilling Shapes,sec:shapes}]
%% The pdf file itself
{QuillingShapes.pdf}
  • 2
    This seems like it's going to work for my purposes. I found more information about the options for addtotoc here. I'm still going to have a few issues with the formating of the pdfs exported from the Jupyter Notebook but that's down to Jupyter, not LaTeX. Thanks! – jkibele Jan 31 '16 at 22:55
5

Here is how I like to include Jupyter notebooks in a LaTeX document with \include{}.

Outline

  1. In the Jupyter Notebook, use the raw option for non-code exposition which contains LaTeX. This is achieved by selecting the cell and then pressing ESC followed by R. The cell might look like this:
This is some text in the notebook which contains LaTeX.  
So we can write $\theta \in (0,1)$ or cite \ref{chapter1}  
from elsewhere in the Latex file.

Your LaTeX can reference whatever you like in the overall LaTeX document.

  1. Use nbconvert to output to markdown first. Suppose your notebook is named test.ipynb. Then use
jupyter nbconvert --to markdown test.ipynb

This code creates test.md which is the intermediate file.

  1. Use Pandoc to convert the markdown into LaTeX, as in
pandoc --listings -f markdown -t latex test.md -o test.tex

This will create test.tex which can then be inserted into your main LaTeX document with \include{test.tex}.
Note: The listings package must be available to your LaTeX installation. Dropping the --listings option works, but produces Tex which includes many additional command sequences you may not want in your main Tex file. Add the standalone option in order to preview the pdf or the see which command sequences and packages you might want to include in your main Latex document:

pandoc --listings -f markdown -t latex test.md -s -o test.tex
pdflatex test.tex

You can then open the pdf test.pdf for a preview.

4

Take a look at Authorea (http://www.authorea.com) - it's similar to Overleaf, but offers a few more advanced features, including easy integration of ipython notebooks. See https://www.authorea.com/users/9932/articles/11070 for more info

  • This looks more like the level of integration I was hoping for, but Authorea doesn't seem to have the level of support for complex LaTeX that's available in Overleaf and ShareLatex. From what I can tell in a few minutes of research, it looks like it would be difficult for me to port my big template-using-multi-file thesis over to Authorea. Thanks for the answer. – jkibele Jan 31 '16 at 23:02
  • Hi- one of the creators of Authorea here. I just wanted to let you know that since last year, we have vastly improved the interface as well as LaTeX support. As discussed above, the real advantage is that Authorea articles are web-native and data-driven. Here's a quick demonstration of what is possible in Authorea today (via LaTeXML): authorea.com/… – Alberto Pepe Apr 5 '17 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.