Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

10/6/2020 Publication ready scientific reports and presentations with Jupyter notebooks: Live editing of LaTeX documents

This lesson is being piloted (Beta version)

 Publication ready scientific reports and presentations with Jupyter 


(../02- notebooks (../) (../04-
git/index.html) publis

Live editing of LaTeX documents


 Overview

Teaching: 30 min
Exercises: 30 min
Questions
How to use JupyterLab LaTeX extension?
How to compile LaTeX documents within JupyterLab?
How to generate LaTeX documents from jupyter notebooks?
Objectives
Learn about Jupyterlab LaTeX extension
Learn about the generation of LaTeX documents from jupyter notebooks

We have been using JupyterLab to keep our research work and are now ready to write a scientific paper associated with our computing
results and for writing our paper, we will be using LaTeX (https://www.latex-project.org/).

JupyterLab extension for live editing of LaTeX


documents
The JupyterLab LaTeX (https://github.com/jupyterlab/jupyterlab-latex) extension has already been installed in the VM. In case, you need to
set it up on your laptop, follow instructions given here (https://github.com/jupyterlab/jupyterlab-latex/blob/master/README.md).

Create a new LaTeX document


Create a new empty file

https://annefou.github.io/jupyter_publish/03-latex/index.html 1/6
10/6/2020 Publication ready scientific reports and presentations with Jupyter notebooks: Live editing of LaTeX documents

Rename it to report_example.tex

Then we will create a very simple LaTeX document and check it out with the JupyterLab:

\documentclass{article}

\begin{document}

\title{A very Simple \LaTeX document}


\date{}
\maketitle

\end{document}

Save it and then right click to select “Show LaTeX preview” as shown in the figure below:

https://annefou.github.io/jupyter_publish/03-latex/index.html 2/6
10/6/2020 Publication ready scientific reports and presentations with Jupyter notebooks: Live editing of LaTeX documents

The pdf preview of your document will appear on the right hand side of JupyterLab:

✏ Update your LaTeX document

Add new sections in your LaTeX document. What happens?

 Solution 

✏ Add a bibliography (BibTeX)

Create a new bibtex file with at least one reference and reference it in report_example.tex

 Solution 

See https://fr.overleaf.com/learn/latex/Bibliography_management_with_bibtex
(https://fr.overleaf.com/learn/latex/Bibliography_management_with_bibtex) to get more examples.

https://annefou.github.io/jupyter_publish/03-latex/index.html 3/6
10/6/2020 Publication ready scientific reports and presentations with Jupyter notebooks: Live editing of LaTeX documents

Generate LaTeX from our jupyter notebooks


Being able to create LaTeX live documents with JupyterLab is great but we also need to make use of our jupyter notebooks so we can
create a reproducible research work, share it and publish it.

Let’s go back to our jupyter notebook example and let’s create a nice LaTeX document from it.

Close report_example.tex and report_example.pdf and open example.ipynb .


Add a new code cell at the end with the following information:

!nbpublish -f latex_ipypublish_all -pdf example.ipynb

Execute this cell and check converted directory. It should contain a list of files and in particular:
example.tex
example.pdf

nbpublish can be called directly from your jupyter notebook or from the jupyterLab Terminal.

Cell inspector
It is sometimes convenient to inspect and modify metadata of a given cell. For instance, to ignore the cell where nbpublish is run:

{
"ipub": {
"ignore": true
}
}

Add it in the metadata of the corresponding cell, as shown in the image below:

Do not forget to commit changes to data.

There are several options (not only latex_ipypublish_all) to generate and customize what you would like to see in your report.

Check the ipypublish documentation (https://ipypublish.readthedocs.io/en/latest/) for more information. Please note that the ipypublish
python package is still under development (beta version avaiable only).

https://annefou.github.io/jupyter_publish/03-latex/index.html 4/6
10/6/2020 Publication ready scientific reports and presentations with Jupyter notebooks: Live editing of LaTeX documents

✏ Add generated files into your Github repository

Add the updated example.ipynb, example.pdf, report_example.tex, report_example.bib and report_example.pdf to your Github repository
research-bazaar-jupyter-2019 using JupyterLab git extension and/or JupyterLab Git Terminal.

Additional ipypublish example


In this short tutorial, all the functionalities of ipypublish are not demonstrated. However, feel free to browse the following example
(https://github.com/annefou/ipypublish_example).

Combine our Jupyter notebooks and LaTeX


documents
Now we can combine our jupyter notebook and our LaTeX document to generate a single publication ready scientific report.

A very simple way to combine them is to merge the pdf generated files. Create a new text file and rename it report_main.tex :

\documentclass{report}
\usepackage{pdfpages}
\usepackage{fancyhdr}
\fancyhf{}
\renewcommand{\headrulewidth}{0pt}
\pagestyle{fancy}
\rfoot{\thepage}
\begin{document}
\tableofcontents
\chapter{A chapter}
\section{Introduction}
\includepdf[pages=1-,pagecommand={\thispagestyle{fancy}}]{report_example.pdf}
\includepdf[pages=1-,pagecommand={\thispagestyle{fancy}}]{example.pdf}

\end{document}

Then generate the resulting pdf files (“Show LaTeX preview”).

✏ Add generated files into your Github repository

As before, add report_main.tex and report_main.pdf to your Github repository research-bazaar-jupyter-2019 using JupyterLab git
extension and/or JupyterLab Git Terminal.

 Key Points

jupyterlab LaTeX extension

 
(../02- (../04-
git/index.html) publis
Licensed under CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/) 2018–2019 by The Carpentries (https://carpentries.org/)
Licensed under CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/) 2016–2018 by Software Carpentry Foundation (https://software-
carpentry.org)

https://annefou.github.io/jupyter_publish/03-latex/index.html 5/6
10/6/2020 Publication ready scientific reports and presentations with Jupyter notebooks: Live editing of LaTeX documents
Edit on GitHub (https://github.com/annefou/jupyter_publish/edit/gh-pages/_episodes/03-latex.md) / Contributing
(https://github.com/annefou/jupyter_publish/blob/gh-pages/CONTRIBUTING.md) / Source (https://github.com/annefou/jupyter_publish/) /
Cite (https://github.com/annefou/jupyter_publish/blob/gh-pages/CITATION) / Contact (mailto:annefou@geo.uio.no)
Using The Carpentries style (https://github.com/carpentries/styles/) version 9.5.3 (https://github.com/carpentries/styles/releases/tag/v9.5.3).

https://annefou.github.io/jupyter_publish/03-latex/index.html 6/6

You might also like