📜 ⬆️ ⬇️

Setting up LaTeX templates for Jupyter notebook

There is a great tool for learning / reporting / writing smart books about code - Jupyter Notebook . If a report or a book, for example, is written in Cyrillic, and you need to quickly make PDF of it with beautiful formulas and a dash of the correct length, then the problem is immediately revealed: in the standard template that Jupyter uses to convert notebooks to PDF via LaTeX, there is no connection of the necessary packages with the necessary parameters, so LaTeX just does not compile and not get a PDF.

Constant assumptions: let's talk about Jupyter, which was disconnected from the main IPython project in the IPython 4 release; if you want to talk about IPython 3.x, replace jupyter in ipython and check for possible file name mismatches. To generate PDF, we use either the command line ( jupyter nbconvert --to pdf myfile.ipynb ) or the button from the Download as -> PDF web interface via LaTeX.

The easiest way to solve the problem with missing packages is jupyter nbconvert --to latex myfile.ipynb , open the resulting TeX source and append the missing packages. Compile the source ( pdflatex myfile.tex or whatever you like), the goal is achieved.

If the problem is not solved in one line (you need to seriously change the template and do not want to dig into the unpleasant result of converting to LaTeX) / I have many notebooks / I want a more general solution, then continue.
')
TL; DR : in order to get pdf with Cyrillic, add two files to yourself (the link leads to a comment with the shortest description).

It is necessary to explain to the converter that in order to convert the notepad to LaTeX it should not use the standard template, but ours, with the expanded title, the most correct font and the corporate theme of the design.

 jupyter nbconvert --to pdf --template mytemplate.tplx myfile.ipynb 

Problems: mytemplate.tplx successfully located only in the directory from which nbconvert launched, through the web interface it is not clear how to write a template is not clear.

Settings Files


First, about the configuration and location of files. This is quickly solved by writing your own settings file. The configuration file for nbconvert is a file with Python code. How to specify the settings file when invoking the converter:

 jupyter nbconvert --to pdf --config cfg.py myfile.ipynb 

The settings file looks like this:

 c = get_config() c.NbConvertApp.export_format = 'pdf' c.TemplateExporter.template_path = ['.'] c.Exporter.template_file = 'article' 

Here, export_format is the default value for - to, template_path is a list of directories with templates, here it says that template files should be searched in the directory from which nbconvert is launched, template_file means that, if not specified otherwise, you need to use the article.tplx template .

Now, if you run jupyter notebook --config cfg.py , then all the settings for the conversion will be taken first from the configuration file, which is what we need. If you want these settings to be the default every time nbconvert run nbconvert this user, you need to put them in the file ~/.jupyter/jupyter_nbconvert_config.py . Accordingly, for notebook general settings file is ~/.jupyter/jupyter_notebook_config.py .

Templates


The most interesting part is how to write templates. Templates are written using the Jinja2 template engine; To avoid conflicts with special LaTeX characters, the template's overhead sequences are redefined (the first { replaced by (( , others { by ( , with back brackets mirrored). The default set of templates is in NBCONVERT_INSTALLATION_DIR/nbconvert/templates/latex/ . They are well documented, it makes sense to inherit from them when creating your templates. More examples of templates are in the nbconvert-examples repository on Github . Screenshots of various design options can be viewed in the readme to one of the sections of this repository.

How to write your template in which all the necessary things for you personally will be performed? Create a file mytemplate.tplx , in which to write a few necessary things.

Firstly, to carefully inherit from the pattern that defines the specific style of drawing cells with a code ( as in the example ):

 % Default to the notebook output style ((* set cell_style = 'style_notebook.tplx' *)) % Inherit from the specified cell style. ((* extends cell_style *)) 

Here I inherit from the style_notebook.tplx template, which is not standard, but lies in nbconvert-examples . This template is also written, apparently, for the old version of nbconvert, so it will need to change the line ((* extends 'latex_base.tplx' *)) to ((* extends 'base.tplx' *)) .

Secondly, define the \documentclass future LaTeX file and do not make a header (you can instead write code that takes a header from the metadata of the .ipynb file or from somewhere else):

 ((* block docclass *)) \documentclass{article} ((* endblock docclass *)) ((* block maketitle *))((* endblock maketitle *)) 

Thirdly - to connect the desired packages with Cyrillic support (and a dozen or two other favorites):

 ((* block packages *)) ((( super() ))) % load all other packages \usepackage[T2A]{fontenc} \usepackage[english, russian]{babel} \usepackage{mathtools} ((* endblock packages *)) 

The full list of blocks is described in nbconvert/templates/latex/skeleton/null.tplx and nbconvert/templates/latex/base.tplx (and these are also GitHub links).

If we want to use the resulting template not in a specific project, but in all notebooks, we can put it, for example, in ~/.jupyter/templates/ and change the corresponding line in the settings file (thanks to spitty for noting that the relative paths are just will not work):

 import os c.TemplateExporter.template_path = ['.', os.path.expanduser('~/.jupyter/templates/')] 

After some minor fixes in the template for drawing cells with code (I didn’t like In [*]), I’ve got the following reports (screenshot from a PDF file):

Source: https://habr.com/ru/post/279601/


All Articles