Skip to content

Instantly share code, notes, and snippets.

@gsanchezd
Last active November 23, 2018 21:39
Show Gist options
  • Save gsanchezd/5a19298cff5c035a1b99c678b910e8f6 to your computer and use it in GitHub Desktop.
Save gsanchezd/5a19298cff5c035a1b99c678b910e8f6 to your computer and use it in GitHub Desktop.
Custom template for Jupyter notebooks

Custom Template for jupyter notebook

Add Logo to templates

In the file articles.tplx in ~/.jupyter/templates folder (if the file or folder doesn't exist we can create it)

% Default to the notebook output style
((* if not cell_style is defined *))
    ((* set cell_style = 'style_ipython.tplx' *))
((* endif *))
% Inherit from the specified cell style.
((* extends cell_style *))
%===============================================================================
% Latex Article
%===============================================================================
((* block docclass *))
\documentclass[11pt]{article}
\usepackage{tikz}
\usepackage{everypage}
\makeatletter
\global\let\tikz@ensure@dollar@catcode=\relax
\makeatother
\AddEverypageHook{%
  \begin{tikzpicture}[remember picture,overlay]
    \node[anchor=north east,yshift=-20pt,xshift=-30pt] at (current page.north east)
      {\includegraphics{logo.png}};
    \end{tikzpicture}}
((* endblock docclass *))

To use the custom template we can compile to PDF with:

jupyter nbconvert --to=pdf --PdfExporter.template_file=~/.jupyter/templates/article.tplx notebook.ipynb

Fix UTF-8

This case is a little more complex, we need to change a package added in the base template file.

At the beginning of this file we can see that the file called style_ipython.tplx is extended. And this file extends a file called base.tplx we can search this file using the terminal (or finder).

In my case the file was in: ~/miniconda3/lib/python3.6/site-packages/nbconvert/templates/latex/base.tplx

We need to remove the pacakge that cause the problem.

\usepackage[utf8x]{inputenc} % Allow utf-8 characters in the tex document

And add a new one for the support of UTF-8

\usepackage{listings,fontspec}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment