Documentation
Documentation
J.-F. Bercher
November 4, 2016
Contents
1 Introduction 2
1.1 ** What's new ** . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 Main features 5
2.1 Implementation principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Support for simple LaTeX commands . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.3 Available environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.4 Automatic numerotation, labels and references . . . . . . . . . . . . . . . . . . . . . 5
2.5 Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.5.1 Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.5.2 Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.6 Figure environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.7 gcaption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.8 Other features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.9 User interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.9.1 Buttons on main toolbar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.9.2 Conguration toolbar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.10 The LaTeX_envs insertion menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4 Installation 13
5 Customization 14
5.1 Conguration parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
5.2 User environments conguration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
5.3 Styling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1 Introduction
This extension for IPython 4.x or Jupyter enables to use some LaTeX commands and environments
in the notebook's markdown cells.
• gure environment,
limited support for a
A simple illustration is as follows: one can type the following in a markdown cell
Let $u$ and $v$ be two vectors of $ \ mathbb {R}^n $ . The dot product can be
expressed as
\ begin { equation }
\ l a b e l { eq : dotp }
\ end { e q u a t i o n }
\ end { t h e o r e m }
Page 2/19
and have it rendered as
The dot-product is dened by equation (1) in theorem 1 just below:
Theorem 1 (Dot Product) . Let u and v be two vectors of Rn . The dot product can be expressed as
• Support for book/report style numbering of environments, e.g. Example 4.2 is example
2 in section 4.
• Support for \author, \title, and maketitle. Author and title are saved in notebook
metadata, used in html/latex exports. The maketitle command also formats a title in the
LiveNotebook.
• Support for user personal LaTeX denitions le (latexdefs.tex in current directory).
This le is included by the html and latex export templates.
• Cross references now use the true environment number instead of the refer-
ence//label key. References are updated immediately. This works document wide
and works for pre and post references
• Support for optional parameters in theorem-like environments
• Support for spacings in textmode, eg \par, \vspace, \hspace
• Support for LaTeX comments - Reworked loading and merging of system/document congu-
ration parameters
• Added support for nested environments of the same type. Nesting environments of
dierent type was already possible, but there was a limitation for nesting environments of
Page 3/19
the same kind; eg itemize in itemize in itemize. This was due to to the fact that regular
expressions are not suited to recursivity. I have developped a series of functions that enable
to extract nested environments and thus cope with such situations.
• Corrected various issues, eg #731, #720 where the content of nested environments was incor-
rectly converted to markdown.
• Added system parameters that can be specied using the nbextensions_congurator. Thus
reworked the conguration loading/saving.
• Reworked extension loading. It now detects if the notebook is fully loaded before loading
itself.
• Added a template to also keep the toc2 features when exporting to html:
• Added a dropdown menu that enables to insert all main LaTeX_envs environments using a
simple click. Two keybards shortcuts (Ctrl-E and Ctrl-I) for equations and itemize are also
provided. More environments and shortcuts can be added in the le envsLatex.js.
• Added a link in the general help menu that points to the documentation.
• In this version I have reworked equation numbering. In the previous version, I used a
specialized counter and detected equations rendering for updating this counter. Meanwhile,
this feature has been introduced in MathJax and now we rely on MathJax implementation.
rendering is signicantly faster. We still have keep the capability of displaying only equation
labels (instead of numbers). The numbering is automatically updated and is document-wide.
• I have completely reworked the notebook conversion to plain LATEXand html. We provide
specialized exporters, pre and post processors, templates. We also added entry-points to
simplify the conversion process. It is now as simple as
to convert FILE.ipynb into html while keeping all the features of the latex_envs notebook
extension in the converted version.
Page 4/19
2 Main features
More environments can be added easily in the user_envs cong le user_envs.json or di-
rectly in the javascript source le thmsInNb4.js. The rendering is done according to the stylesheet
latex_env.css, which can be customized.
Remark 1. When exporting to html, the latex_env.css le honored is the le on the github CDN.
However, customized css can be added in a custom.css le that must reside in the same directory
as the notebook itself. The reason for that is that the css le must be in the same directory as the
notebook le for inclusion, which means copying it in each working directory. As the rendering of
the html le obtained is done using the original javascript code, the same is true for the source les;
therefore it is better to curstomize environments in user_envs.json which is taken into account
when exporting to html.
Page 5/19
was that numbering was updated (incremented) each time a cell is rendered. Document-wide auto-
matic updating is implemented since version 1.3. A toolbar button is provided to reset the counters
and refresh the rendering of the whole document (this is still useful for citations and bibliography
refresh).
A simple example is as follows, featuring automatic numerotation, and the use of labels and
references. Also note that standard markdown can be present in the environment and is interpreted.
The rendering is done according to the stylesheet latex_env.css, which of course, can be customized
to specic uses and tastes.
\ begin { d e f i n i t i o n } \ l a b e l { d e f : FT}
given by
\ begin { equation }
\ l a b e l { e q : FT}
\ end { e q u a t i o n }
\ end { d e f i n i t i o n }
Denition 1. Let x[n] be a sequence of length N . Then, its Fourier transform is given by
N −1
1 ∑
x[n]e−j2π N
kn
X[k] = (2)
N
n=0
It is now possible to refer to the denition and to the equation by their labels, as in:
$$
$$
As an example of Denition 1, consider the Fourier transform (2) of a pure cosine wave given by
1
X[k] = (δ[k − k0 ] + δ[k − k0 ]) ,
2
modulo N.
2.5 Bibliography
2.5.1 Usage
It is possible to cite bibliographic references using the standard LaTeX \cite mechanism. The
extension looks for the references in a bibTeX le, by default biblio.bib in the same directory as
the notebook. The name of this le can be modied in the conguration toolbar. It is then possible
to cite works in the notebook, e.g.
The main paper on IPython is denitively [1]. Other interesting references are certainly [2, 3].
Interestingly, a presentation of the IPython notebook has also be published recently in Nature [4].
Page 6/19
Figure 1: This is an example of gure included using LaTeX commands.
2.5.2 Implementation
The implemention uses several snippets from the nice icalico-document-tools extension that also
considers the rendering of citations in the notebook. We also use a modied version of the bibtex-
js parser for reading the references in the bibTeX le. The dierent functions are implemented
in bibInNb4.js. The rendering of citations calls can adopt three styles (Numbered, by key or
apa-like) this can be selected in the conguration toolbar. It is also possible to customize the
rendering of references in the reference list. A citation template is provided in the beginning of le
latex_envs.js:
var cit_tpl = {
// feel free to add more types and customize the templates
'INPROCEEDINGS': '%AUTHOR:InitialsGiven%, ``_%TITLE%_\'\', %BOOKTITLE%, %MONTH% %YEAR%.',
... etc
The keys are the main types of documents, eg inproceedings, article, inbook, etc. To each key is
associated a string where the %KEYWORDS% are the elds of the bibtex entry. The keywords are
replaced by the correponding bibtex entry value. The template string can formatted with additional
words and eects (markdown or LaTeX are commands are supported)
\ begin { f i g u r e }
\ c e n t e r l i n e { \ i n c l u d e g r a p h i c s [ w i d t h =10cm ] { e x a m p l e . png } }
LaTeX commands . }
\ end { f i g u r e }
which renders as
Of course, this Figure can now be referenced:
Figure 1 shows a second lter with input X2 , output Y2 and an impulse response denoted as
h2 (n)
2.7 gcaption
For Python users, we have added in passing a simple function in the latex_envs.py library.
Page 7/19
This function can be from latex_envs.latex_envs import
imported classically, eg
figcaption (or from jupyter_contrib_nbextensions.nbconvert_support.latex_envs import
figcaption if you installed from the jupyter_contrib repo).
Then, this function enables to specify a caption and a label for the next plot. In turn, when
A
exporting to L TEX, the corresponding plot is converted to a nice gure environement with a label
and a caption.
%m a t p l o t l i b inline
figcaption
[<matplotlib.lines.Line2D at 0x7f8a1872ba20>]
% This is a comment
\ b e g i n { enumerate }
\ item A ∗∗ n e s t e d enumerate ∗∗
\ item second item
Page 8/19
\ b e g i n { enumerate }
\ b e g i n { enumerate }
\ end { e n u m e r a t e }
\ end { e n u m e r a t e }
\ end { e n u m e r a t e }
\ end { p r o o f }
which results in
1.
{p1 , p2 , p3 . . . pn } (3)
{p1 , p2 , p3 . . . pn }
2. A nested enumerate
3. second item
(a) {p1 , p2 , p3 . . . pn }
(b) And another one
(c) second item
i.
{p1 , p2 , p3 . . . pn }
ii. second item
The rst one can be used to refresh the numerotation of equations and references in all the docu-
ment. The second one res the reading of the bibliography bibtex le and creates (or updates) the
reference section. Finally the third one is a toogle button that opens or closes the conguration
toolbar.
Page 9/19
2.9.2 Conguration toolbar
Page 10/19
2.10 The LaTeX_envs insertion menu
The extension works in the live-notebook. Since it relies on a bunch of javascript, the notebook
does not render as is in services such as nbviewer or github viewer. Similarly, nbconvert does not
know of the LaTeX constructs which are used here and therefore does not fully convert notebooks
using this extension.
Therefore, we provide specialized templates and exporters to achieve these conversions.
It should be noted that the rendering is done exactly in the same way as in the livenotebook.
Actually, it is the very same javascript which is run in the html le. The javascript functions are
Page 11/19
available on the extension github as well as in the jupyter_notebook_extensions CDN, which
means that the rendering of the html les requires an internet connection (this is also true for the
rendering of equations with MathJax).
Another template latex_envs_toc.tpl is provided which enables to also keep the toc2 features
when exporting to html (it even works if you do not have the toc2 extension! ):
Page 12/19
4 Installation
The extension consists in a package that includes a javascript notebook extension. Since Jupyter
4.2, this is the recommended way to distribute nbextensions. The extension can be installed
• from the master version on the github repo (this will be always the most recent version)
• via pip for the version hosted on Pypi
• as part of the great Jupyter-notebook-extensions collection. Follow the instructions there for
installing. Once this is done, you can open a tab at http://localhost:8888/nbextensions
to enable and congure the various extensions.
For Jupyter versions before 4.2, the situation is more tricky since you will have to nd the
location of the source les (instructions from @jcb91 found here): execute
Then, issue
Page 13/19
5 Customization
"myenv": {
"title": "MyEnv",
"counterName": "example"
},
Available counters are problem, exercise, example, property, theorem, lemma, corollary, propo-
sition, denition, remark, and gure.
5.3 Styling
The document title and the document author (as specied by \title and \author are formatted
using the maketitle command according to the .latex_maintitle and .latex_author div styles.
Each environment is formatted according to the div style .latex_environmentName, e.g.
.latex_theorem, .latex_example, etc. The titles of environments are formatted with respect
to .latex_title and the optional parameter wrt .latex_title_opt. Images are displayed using
the style specied by .latex_img and thir caption using .caption. Finally, enumerate environ-
ments are formatted according to the .enum style. Similarly, itemize environments are formatted
using .item style.
These styles can be customized either in the latex_envs.css le, or better in a custom.css in
the document directory.
Page 14/19
processing, We put this in evidence using the textboxa environment which is dened here in the
css, and that one should dene in the LaTeX counterpart:
\ begin { textboxa }
\ end { t e x t b o x a }
1
X[k] = (δ[k − k0 ] + δ[k − k0 ]) ,
2
modulo N. This is illustrated in the following simple script:
%m a t p l o t l i b inline
import numpy as np
x=np . s i n ( 2 ∗ np . p i ∗ k 0 ∗ n /N)
X= f f t ( x )
p l t . s t e m ( k , np . a b s (X) )
p l t . xlim ( [ 0 , 20])
$$
Page 15/19
\ l a b e l { eq : d i f f e q }
$$
\ end { d e f i n i t i o n }
\ begin { property }
diffeq } are zero , then the filter has a ∗∗ f i n i t e impulse response ∗∗.
\ end { p r o p e r t y }
\ begin { proof }
$$
\ l a b e l { eq : f i r }
$$
\ end { p r o o f }
\ b e g i n { theorem }
The poles of a causal stable filter are located within the unit circle in
\ end { t h e o r e m }
\ b e g i n { example } \ l a b e l { ex : IIR 1}
\ end { example }
In the following exercise , you will check that the filter is stable i f f $a
$ <1.
\ b e g i n { e x e r c i s e }\ l a b e l { ex : e x o f i l t e r }
several values of $a $ .
\ end { e x e r c i s e }
The lines above are rendered as follows (of course everything can be tailored in the stylesheet):
Properties of the lter are linked to the coecients of the dierence equation. For instance, an
immediate property is
Property 1. If all the ak in equation (4) of denition 2 are zero, then the lter has a nite
impulse response.
Proof. Let δ[n] denote the Dirac impulse. Take x[n] = δ[n] in (4). This yields, by denition, the
impulse response:
∑
q
h[n] = bi δ[n − i], (5)
i=0
Page 16/19
which has nite support.
Theorem 2. The poles of a causal stable lter are located within the unit circle in the complex
plane.
Example 2. Consider y[n] = ay[n − 1] + x[n]. The pole of the transfer function is z = a. The
impulse response h[n] = an has innite support.
In the following exercise, you will check that the lter is stable i a<1.
Exercise 1. Consider the lter dened in Example 2. Using the function lfilter of scipy,
compute and plot the impulse response for several values of a.
%m a t p l o t l i b inline
import numpy as np
for a in alist :
h= l f i l t e r ( [ 1 ] , [1 , −a ] , d)
p l t . ylim ( [ 0 , 1 . 5 ] )
_=p l t . l e g e n d ( )
Page 17/19
The following \ t e x t i t { environments } are available :
\ begin { itemize }
\ b e g i n { enumerate }
\ item theorem ,
\ item lemma ,
\ item corollary
\ item . . .
\ end { e n u m e r a t e }
\ b e g i n { enumerate }
\ item problem ,
\ item example ,
\ item exercise
\ end { e n u m e r a t e }
\ end { i t e m i z e }
which gives. . .
The following environments are available:
1. theorem,
2. lemma,
3. corollary
4. . . .
• exercises
1. problem,
2. example,
3. exercise
Originally, I used a piece of code from the nice online markdown editor stackedit
https://github.com/benweet/stackedit/issues/187, where the authors also considered the problem
of incorporating LaTeX markup in their markdown.
I also studied and used examples and code from https://github.com/ipython-contrib/IPython-
notebook-extensions.
• This is done in the hope it can be useful. However there are many impovements possible,
in the code and in the documentation. Contributions will be welcome and deeply
appreciated.
• If you have issues, please post an issue at https://github.com/jfbercher/jupyter_latex_envs/issues
here.
Self-Promotion Like latex_envs? Please star and follow the repository on GitHub.
Page 18/19
References
[1] F. Pérez and B. E. Granger, IPython: a system for interactive scientic computing,
Computing in Science and Engineering, vol. 9, no. 3, pp. 2129, May 2007. [Online]. Available:
http://ipython.org
[2] W. McKinney, Python for data analysis: Data wrangling with Pandas, NumPy, and IPython. "
O'Reilly Media, Inc.", 2012.
[3] C. Rossant, Learning IPython for interactive computing and data visualization. Packt Publishing
Ltd, 2013.
[4] H. Shen, Interactive notebooks: Sharing the code, Nature, vol. 515, no. 7525, pp. 151152,
2014.
Page 19/19