Jupyter notebooks to A4 (again)

Pieter van Oostrum pieter-l at vanoostrum.org
Sun Jan 31 14:55:02 EST 2021


Martin Schöön <martin.schoon at gmail.com> writes:

> Hello all,
>
> Some years ago I asked about exporting notebooks to pdf in
> A4 rather than US Letter. I got help, rather detailed
> instructions from you in general and Piet von Oostrum in
> particular. Following the advice helped and I was happy.
>
> Now it does not work any longer:
>
>          nbconvert failed: A4article
>
> I am stumped. I have not changed anything and all
> looks OK.
>
> Today I tried up-dating all things Python and Jupyter
> but that did not help.
>
> I have also tried removing the A4 stuff and after 
> restarting Jupyter I can export to PDF and get US Letter
> paper format.
>
> A quick and (obviously) not very clever internet search
> yielded nothing helpful.

The template system has changed in nbconvert version 6, so the original templates don't work anymore.
And they still haven't supplied standard provisions for A4 paper (like an option or so). Probably a mild case of cultural blindness.

I found out how to do it in the new template system, however, there is a bug that gives some troubles.

To get all your latex (and therefore also PDF) output in A4 format:

Find out where nbconvert expects user templates.

Issue the shell command:
bash-3.2$ jupyter --paths
Look up the data part:
...
data:
    /Users/pieter/Library/Jupyter
...
The first one is your personal directory in my case ~/Library/Jupyter.

In this directory create the directory nbconvert/templates/latex
In this directory create these two files:
conf.json
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: conf.json
URL: <https://mail.python.org/pipermail/python-list/attachments/20210131/05c2a79e/attachment.ksh>
-------------- next part --------------
index.tex.j2
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: index.tex.j2
URL: <https://mail.python.org/pipermail/python-list/attachments/20210131/05c2a79e/attachment-0001.ksh>
-------------- next part --------------

Now jupyter nbconvert --to latex my_nb.ipynb will generate latex with a4paper.

If you don't want this to be the default, but you want a special template, let's say A4article, to do this, place these files in a directory .../nbconvert/templates/A4article rather than .../nbconvert/templates/latex.

Then you would use jupyter nbconvert --to latex --template A4article my_nb.ipynb

However, due to a bug this won't work unless you patch the nbconvert export code.
This is a simple one-line patch.
See https://github.com/jupyter/nbconvert/pull/1496/commits/a61a2241a87912005720d3412ccd7ef7b5fce6dd

-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]


More information about the Python-list mailing list