# Towards a book printable jupyter notebook

TLDR: this page contains information how to produce from a notebook a PDF with:

• Inclusion of input cells
• Size A5 paper, one-column
• Markdown images placed here [h] instead of floating
• Two-page book printing and less vertical whitespace with the LaTeX book document class
• Summarized in the template a5_book.tplx that is listed at the bottom of this post

This year I started to write lecture notes from the MOOC Analyzing the Universe with Python notebooks. It would be nice to have something tangible after finishing the course. The idea of having just one button to save a book-printable PDF was appealing. However, I also remember days from the past getting LaTeX content ready for print, and several days is prohibitively long these days. Except now it is holiday 🙂

Yesterday I time-boxed one day to turn a notebook into a PDF suitable for two-page print format. Though this endeavor is far from finished, the things I learned might help others save some time, and myself in the future for further refinement.

I started with the instructions provided by Making publication ready Python Notebooks by Julius Schulz. The main takeout of his article was to write an additional template that extends an nbconvert provided LaTeX template, and fine tune your own work from there. I needed some more fine tuning, since I definitely wanted to keep the input cells with astropy calculations. Another big difference with image conversion was that I do not have images that are output of cells such as plots; all images in my notebook are inline markdown images.

## TODO for future versions

1. Having equations numbered with the equation numbering extension that now is part of the jupyter contrib nbextensions project.
2. Refine the python markup so cell input is rendered more like it is on the screen, or at least some thin lines are shown to indicate start and end of a code cell.
3. Finding the right paper weight so images are invisible on the other side.
4. Maybe a little big bigger than a5, perhaps b5paper with 8pt font size.
5. Combine multiple notebooks into as chapter per notebook into one book.
6. Replacing the [width=.8\maxwidth] default setting from upstream nbconvert and set it in my custom template or configurable per image.

## Running nbconvert from the command line

! Package pdftex.def Error: File images/week6_lecture1_m31_nebula.png'
not found.

If your images are specified with relative paths from a notebook that is not in the root directory of the jupyter notebook server, you need to run the command jupyter nbconvert in the same directory as the notebook. Running from the command line is also required to specify a custom latex template.

## a5_book.tplx

The final template I ended up with was the following:

((*- extends 'article.tplx' -*))

((* block docclass *))
\documentclass[9pt, reprint, floatfix, groupaddress, prb, twoside]{book}

% Use a wider inner margin for the two-sided book
\usepackage[a5paper, margin=0.5in, inner=1in]{geometry}

% Ignore future geometry commands with optional and mandatory arguments
\usepackage{xparse}
\RenewDocumentCommand{\geometry}{om}{%
}

% Let all figures float 'H'ere
\usepackage{float}
\floatplacement{figure}{H}

((* endblock docclass *))

% Author and Title from metadata
((* block maketitle *))

((*- endif *))
((*- endif *))

((*- endif *))
((*- else -*))
((*- endif *))

\date{\today}
\maketitle

`