The revtex template file

The file revtex.tplx (download) is a custom template for converting jupyter notebooks to latex with nbconvert. Note that this is not so much for converting a notebook into a proper article (this is best done through markdown as an intermediary). Instead, the goal is to generate a printable pdf of a notebook (via latex).

Compared to the default template, it has the following features:

  • Use the revtex document class (single column). This results in a more condensed and readable output

  • Support for the braket package

  • Fixed title block. The document will be dated. The title, author, and affiliation may be defined in the notebook metadata. This is based on Julius Schulz’ template. For example you might put the following in the notebook metadata (Edit → Edit Notebook Metadata):

    "latex_metadata": {
        "author": "M H Goerz",
        "affiliation": "Stanford University",
        "title": "An example notebook",
        "bib": "notebook.bib"
  • Support for hiding input cells. Technically, this looks for hide_input either in the notebook metadata (hide all cells), or in cell metadata (hide a specific cell).

    In practice, instead of editing the metadata directly, cells can be hidden via the “Hide input” and “Hide input all” extensions that may be installed through the jupyter_contrib_nbextensions package

  • Support for bibtex bibliographies. You can insert a citation in a markdown cell as e.g.

    <cite data-cite="GoerzNPJQI2017">[GoerzNPJQI2017]</cite>

    The generated tex file will then include a \cite{GoerzNPJQI2017} command, which should be defined in a file references.bib. An alternative bibtex file may be set in the notebook metadata, in "latex_metadata" > "bib" (see above)

    Alternatively, you can add a raw cell at the end of the notebook that contains the tex code for the bibliography, e.g.

        Michael H. Goerz, Felix Motzoi, K. Birgitta Whaley, and Christiane P. Koch.
        npj Quant. Info. \bf{3}, 37 (2017)

Manual usage

To use the template, download and store it locally, then call nbconvert as e.g.

jupyter nbconvert --to=latex --template=./revtex.tplx file.ipynb

Note that this only works if you do not set up templates in a config file (see below). If you do, you may to use

jupyter nbconvert --to=latex --LatexExporter.template_file=./revtex.tplx file.ipynb


Then compile the resulting file with pdflatex.

You can also convert directly to pdf, using

jupyter nbconvert --to=pdf --PdfExporter.template_file=./revtex.tplx file.ipynb

Out of the box, this does not work with a bibliography file, however (because the bib file is not copied to the temporary directory in which the compilation happens). You can use a raw cell containing the bibliography, though.

Setting up the template as a default

The template can be set as a default in the ~/.jupyter/ config file. It should contain the following:

custom_path = os.path.expanduser("~/.jupyter/nbconvert_templates")
c.LatexExporter.template_file = 'revtex'
c.PDFExporter.latex_count = 3
c.PDFExporter.template_file = 'revtex'
c.PDFExporter.latex_command = ['xelatex', '{filename}']

The file revtex.tplx must be placed in ~/.jupyter/nbconvert_templates/.

Using the template from inside the notebook interface

The notebook interface allows to directly download converted versions of the notebook (File → Download as). However, the settings for this are not taken from ~/.jupyter/ Instead, the same settings as above must be duplicated in the ~/.jupyter/ config file.


No comments

You may format you comment with Markdown. If your comment is a valid contribution, it may be posted on this page. Your email address will not be made public.