0% found this document useful (0 votes)
17 views

Documentation

Uploaded by

alfdiaz2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Documentation

Uploaded by

alfdiaz2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

(some) LATEX environments in Jupyter notebook

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

3 Conversion to LaTeX and HTML 11


3.1 Conversion to html . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.2 Conversion to LaTeX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

4 Installation 13
5 Customization 14
5.1 Conguration parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
5.2 User environments conguration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
5.3 Styling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

6 Usage and further examples 14


6.1 First example (continued) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
6.2 Second example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
6.3 Third example: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
7 Disclaimer, sources and thanks 18
(some) LaTeX environments
for Jupyter notebook@jfbercher

1 Introduction

This extension for IPython 4.x or Jupyter enables to use some LaTeX commands and environments
in the notebook's markdown cells.

1. LaTeX commands and environments


• support for some LaTeX commands within markdown cells, e.g. \textit, \textbf,
\underline, author, \title, LaTeX comments

• theorems-like environments, support for labels and cross references


support for

• support for lists: enumerate, itemize,

• gure environment,
limited support for a

• support for an environment listing,


• additional textboxa environment

2. Citations and bibliography


• support for \cite with creation of a References section

3. it is possible mix markdown and LaTeX markup

4. Document-wide numbering of equations and environments, support for \label and


\ref
5. Conguration toolbar
6. LaTeX_envs dropdown menu for a quick insertion of environments
7. Support for User LATEXdenitions le
8. Environments title/numbering can be customized by users in user_envs.json cong le

9. Export to HTML and LaTeX with a customized exporter


10. Styles can be customized in the latex_env.css stylesheet

A simple illustration is as follows: one can type the following in a markdown cell

The dot −p r o d u c t is defined by equation ( \ r e f { eq : dotp } ) in theorem \ref {

theo : dotp } just below :

\ b e g i n { t h e o r e m } [ Dot Product ] \ l a b e l { theo : dotp }

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 }

u^Tv = |u || v| \ cos \ theta ,

\ end { e q u a t i o n }

where $\ t h e t a $ is the angle between $u$ and $v$ . . .

\ 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

uT v = |u||v| cos θ, (1)

where θ is the angle between u and v . . .

1.1 ** What's new **


November 2, 2016 - version 1.3.1

• Support for user environments conguration le (user_envs.json in nbextensions/la-


tex_envs directory). This le is included by the html export template.

• 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.

• Added a Toogle menu in the cong toolbar to:

 toggle use of user's environments cong le


 toggle report-style numbering

September 18, 2016 - version 1.3

• Support for user personal LaTeX denitions le (latexdefs.tex in current directory).
This le is included by the html and latex export templates.

• Style for nested enumerate environments added in latex_envs.css


• Added a Toogle menu in the cong toolbar to:

 toggle the display of the LaTeX_envs dropdown menu,


 toggle the display of labels keys,
 toggle use of user's LaTeX denition le

• 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

August 28, 2016 - version 1.2

• 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.

• Completely reworked the conguration toolbar. Re-added tips.

• Added a toggle-button for the LaTeX_envs menu

• 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.

August 03, 2016 - version 1.13

• Added a template to also keep the toc2 features when exporting to html:

jupyter nbconvert --to html_toclenvs FILE.ipynb

• 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.

July 27, 2016 - version 1.1

• 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

jupyter nbconvert --to html_with_lenvs FILE.ipynb

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

2.1 Implementation principle


The main idea is to override the standard Markdown renderer in order to add a small parsing of
LaTeX expressions and environments. This heavily uses regular expressions. The LaTeX expression
are then rendered using an html version. For instance \underline {something} is rendered as
<u> something </u>, that is something. The environments are replaced by an html tag with a
class derived from the name of the environment. For example, a definition denvronment will be
replaced by an html rendering corresponding to the class latex_definition. The styles associated
with the dierent classes are specied in latex_env.css. These substitutions are implemented in
thsInNb4.js.

2.2 Support for simple LaTeX commands


We also added some LaTeX commands (e.g. \textit, \textbf, \underline)  this is useful in the
case of copy-paste from a LaTeX document. The extension also supports some textmode spacings,
namely \par, \vspace, \hspace as well as \title, \author, maketitle and LaTeX comments %
in markdown cells. Labels and cross-references are supported, including for equations.

2.3 Available environments


• theorems-like environments: property, theorem, lemma, corollary, proposition, deni-
tion,remark, problem, exercise, example,
• lists: enumerate, itemize,

• limited support for a gure environment,


• an environment listing,
• textboxa, wich is a textbox environment dened as a demonstration (see below).

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.

2.4 Automatic numerotation, labels and references


Several counters for numerotation are implemented: counters for problem, exercise, example, prop-
erty, theorem, lemma, corollary, proposition, denition, remark, and gure are available. Mathjax-
equations with a label are also numbered document-wide. An anchor is created for any label which
enables to links things within the document: \label and \ref are both supported. A limitation

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}

Let $x [ n ] $ be a sequence of length $N $ . Then , its ∗∗ F o u r i e r transform ∗∗ is

given by

\ begin { equation }

\ l a b e l { e q : FT}

X [ k ]= \ f r a c { 1 } {N} \sum_{n=0}^{N −1} x[n] e ^{− j 2\ p i \ f r a c { kn } {N} }

\ 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 Definition \ r e f { d e f : FT} , consider the Fourier transform

( \ r e f { e q : FT2 } ) of a pure cosine wave given by

$$

x [ n ]= \ cos (2\ pi k_0 n /N) ,

$$

where $ k_0$ is an integer .

As an example of Denition 1, consider the Fourier transform (2) of a pure cosine wave given by

x[n] = cos(2πk0 n/N ),


where k0 is an integer. Its Fourier transform is 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 definitively −


\ c i t e {PER GRA: 2 0 0 7 } . Other

interesting references are certainly \ c i t e { mckinney 2012 python , rossant

2013 l e a r n i n g } . Interestingly , a presentation of the IPython notebook

has also be published recently in Nature \ c i t e { shen 2014 i n t e r a c t i v e } .

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)

2.6 Figure environment


Finally, it is sometimes useful to integrate a gure within a markdown cell. The standard markdown
markup for that is ![link](image), but a limitation is that the image can not be resized, can not
be referenced and is not numbered. Furthermore it can be useful for re-using existing code. Threfore
we have added a limited support for the figure environment. This enables to do something like

\ 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 } }

\ c a p t i o n {\ l a b e l { f i g : example } This is an example of figure included using

LaTeX commands . }

\ end { f i g u r e }

which renders as
Of course, this Figure can now be referenced:

Figure \ r e f { f i g : example } shows a second filter with input $X_ 2 $ , output $Y

_2$ and an impulse response denoted as $ h _2(n ) $

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

import matplotlib . pyplot as plt

from jupyter_contrib_nbextensions . nbconvert_support . latex_envs import

figcaption

from numpy import pi , sin , arange

p l t . p l o t ( s i n (2 ∗ pi ∗0.01∗ arange (100) ) )

[<matplotlib.lines.Line2D at 0x7f8a1872ba20>]

Figure 2: This is a nice sine wave

2.8 Other features


• As shown in the examples, eg 2.4 (or just below), it is possible to mix LaTeX and
markdown markup in environments
• Support forline-comments: lines beginning with a be masked when rendering - Support for
linebreaks: \par_, where _ denotes any space, tab, linefeed, cr, is replaced by a linebreak
• Environments can be nested. egg:

This is an example of nested environments , with equations i n s i d e \\

\ begin { proof } Demo

% This is a comment

\ b e g i n { enumerate }

\ item $$ \ l e f t \{ p _1 , p _2 , p_3 \ ldots p_n \ r i g h t \} $$

\ item A ∗∗ n e s t e d enumerate ∗∗
\ item second item

Page 8/19
\ b e g i n { enumerate }

\ item $ \ l e f t \{ p _1 , p _2 , p_3 \ ldots p_n \ r i g h t \} $

\ item And ∗ another one ∗


\ item second item

\ b e g i n { enumerate }

\ item $$ \ l e f t \{ p _1 , p _2 , p_3 \ ldots p_n \ r i g h t \} $$

\ item second item

\ 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

This is an example of nested environments, with equations inside

Proof. Demo comment

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

2.9 User interface


2.9.1 Buttons on main toolbar
On the main toolbar, the extension provides three buttons

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

The conguration toolbar


enables to enter some conguration options for the extension.
First, the LaTeX\_envs title links to this documentation. Then, the bibliography text input can
be used to indicate the name of the bibtex le. If this le is not found and the user creates the
reference section, then this section will indicate that the le was not found. The References drop-
down menu enables to choose the type of reference calls. The Equations input box enables to initiate
numbering of equations at the given number (this may be useful for complex documents in several
les/parts). The Equations drop-down menu let the user choose to number equation or to display
their label instead. The two next buttons enable to toogle display of the LaTeX_envs environments
insertion menu or to toggle the displau of LaTeX labels. Finally The Toogles dropdown menu
enable to toogle the state of several parameters. All these conguration options are then stored in
the notebook's metadata (and restored on reload).

The Toggles dropdown menu


enables to toggle the state of several conguration options:

• display the LaTeX_envs insertion menu or not,


• show labels anchors,
• A
use L TEXuser own LaTeX dentions (loads latexdefs.tex le from current document direc-
tory),
• load user's environments conguration (le user_envs.json in nbextensions/latex_envs
directory),
• select report style numbering of environments

Page 10/19
2.10 The LaTeX_envs insertion menu

The LaTeX_envs insertion menu enables a


quick insertion of LaTeX environments, some with a keyboard shorcut (this can be customized
in envsLatex.js). Besides, selected text will be inserted in the environment.

3 Conversion to LaTeX and HTML

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.

3.1 Conversion to html


We provide a template latex_envs.tpl and an exporter class LenvsHTMLExporter (in library
latex_envs.py). Using that class, conversion simply amounts to

jupyter nbconvert --to latex_envs.LenvsHTMLExporter FILE.ipynb

A shortcut is also provided

jupyter nbconvert --to html_with_lenvs FILE.ipynb

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! ):

jupyter nbconvert --to html_with_toclenvs FILE.ipynb


If you use the version included in the jupyter_notebook_extensions collection, the
entry-points (conversion shortcuts) are a little dierent: use instead

• jupyter nbconvert --to html_lenvs FILE.ipynb


• jupyter nbconvert --to html_toclenvs FILE.ipynb

3.2 Conversion to LaTeX


We provide two templates thmsInNb_article.tplx and thmsInNb_report.tplx for article and
report styles respectively. Anyway one can also use the standard article, report, book templates
provided with nbconvert. Simply, we have improved some of the internals styles. More importantly,
we provide an exporter class LenvsLatexExporter (also in library latex_envs.py). Using that
class, conversion simply amounts to

jupyter nbconvert --to latex_envs.LenvsLatexExporter FILE.ipynb


A shortcut is also provided

jupyter nbconvert --to latex_with_lenvs FILE.ipynb


In addition, we provide several further options:

• removeHeaders: Remove headers and footers, (default false)


• gcaptionProcess: Process gcaptions, (default true)
• tocrefRemove Remove tocs and ref sections, + some cleaning, (default true),

These options can be specied on the command line as, eg,

jupyter nbconvert --to latex_with_lenvs --LenvsLatexExporter.removeHeaders=True -- LenvsLatexExp


If you use the version included in the jupyter_notebook_extensions collection, the
entry-points (conversion shortcuts) are a little dierent: use instead

jupyter nbconvert --to latex_lenvs FILE.ipynb


Example 1. As for an example, the present document has been converted using
jupyter nbconvert --to latex_with_lenvs --LenvsLatexExporter.removeHeaders=True latex_env_doc.ip
Then the resulting le (without header/footer) has been included in the main le
documentation.tex, where some LaTeX denitions of environments are done (namely listings,
colors, etc) and compiled using
• xelatex -interaction=nonstopmode documentation
• bibTeX documentation
• xelatex -interaction=nonstopmode documentation
The output can be consulted here.

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.

From the github repo or from Pypi,

• step 1: install the package

 pip3 install https://github.com/jfbercher/jupyter_latex_envs/archive/master.zip


[--user][--upgrade]
 or pip3 install jupyter_latex_envs [--user][--upgrade]
 or clone the repo and install git clone https://github.com/jfbercher/jupyter_latex_envs.git
python3 setup.py install

With Jupyter >= 4.2,

• step 2: install the notebook extension

jupyter nbextension install --py latex_envs [--user]

• step 3: and enable it

jupyter nbextension enable latex_envs [--user] --py

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

python -c "import os.path as p; from jupyter_highlight_selected_word import __file__ as f, _jupy

Then, issue

jupyter nbextension install <output source directory>


jupyter nbextension enable latex_envs/latex_envs

where <output source directory> is the output of the python command.

Page 13/19
5 Customization

5.1 Conguration parameters


Some conguration parameters can be specied system-wide, using the
nbextension_configurator. For that, open a browser at http://localhost:8888/nbextensions/ 
the exact address may change eg if you use jupyterhub or if you use a non standard port. You
will then be able to change default values for the boolean values - LaTeX_envs menu (insert
environments) present - Label equation with numbers (otherwise with their key) - Number
environments as section.num - Use customized environments as given in `user_envs.json' (in the
extension directory) and enter a default lename for the bibtex le (in document directory).
All these values can also be changed per documents and these values are stored in the notebook's
metadata.

5.2 User environments conguration


Environments can be customized in the le user_envs.json, located in the
nbextensions/latex_envs directory. It is even possible to add new environments. This le
is read at startup (or when using the corresponding toggle option in the Toggles menu) and
merged with the standard conguration. An example is provided as example_user_envs.json.
For each (new/modied) environment, one has to provide (i) the name of the environment (ii) its
title (iii) the name of the associated counter for numbering it; eg

"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.

6 Usage and further examples

6.1 First example (continued)


We continue the rst example on fthe Fourier transform denition 1 in order to show that, of course,
we can illustrate things using a simple code. Since the Fourier transform is an essential tool in signal

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 }

The Fourier transform is an extremely useful tool to have in your toolbox !

\ end { t e x t b o x a }

The Fourier transform is an extremely useful tool to have in your toolbox!

The Fourier transform of a pure cosine is given by

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

import matplotlib . pyplot as plt

from numpy . f f t import fft

k0 =4; N= 1 2 8 ; n=np . a r a n g e (N) ; k=np . a r a n g e (N)

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])

plt . t i t l e (" Fourier transform of a cosine ")

_=p l t . x l a b e l ( " F r e q u e n c y index (k)")

6.2 Second example


This example shows a series of environments, with dierent facets; links, references, markdown
or/and LaTeX formatting within environments. The listing of environments below is typed
using the environment listing . . .

\ begin { d e f i n i t i o n } \ label { def : d i f f e q }

We call \ textbf { difference equation } an equation of the form

$$

Page 15/19
\ l a b e l { eq : d i f f e q }

y [ n ]= \sum_{k=1}^{p } a_k y[n −k ] + \ sum_{ i =0}^q b_ i x[n −i ]

$$

\ end { d e f i n i t i o n }

\ begin { property }

If all the $ a_k $ in equation ( \ r e f { eq : d i f f e q } ) of definition \ r e f { def :

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 }

Let $\ d e l t a [ n ] $ denote the Dirac impulse . Take $ x [ n ]=\ d e l t a [ n ] $ in (\ r e f {

eq : d i f f e q } ) . This yields , by definition , the impulse response :

$$

\ l a b e l { eq : f i r }

h [ n ]= \sum_{ i =0}^q b_ i \ delta [n −i ] ,

$$

which has finite support .

\ 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

the complex plane .

\ end { t h e o r e m }

\ b e g i n { example } \ l a b e l { ex : IIR 1}

Consider $ y [ n ]= a y[n −1] + x[n]$. The pole of the transfer function is $z

=a $ . The impulse response $ h [ n ]= a^n $ has infinite support .

\ 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 }

Consider the filter defined in Example \ r e f { ex : IIR 1 } . Using the ∗∗ f u n c t i o n


∗∗ ` lfilter ` of scipy , compute and plot the impulse response for

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):

Denition 2. We call dierence equation an equation of the form



p ∑
q
y[n] = ak y[n − k] + bi x[n − i] (4)
k=1 i=0

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.

The solution of exercise \ r e f { ex : e x o f i l t e r } , which uses a difference

equation as in Definition \ r e f { def : d i f f e q }:

The solution of exercise 1, which uses a dierence equation as in Denition 2:

%m a t p l o t l i b inline

import numpy as np

import matplotlib . pyplot as plt

from scipy . signal import l f i l t e r

d=np . z e r o s ( 1 0 0 ) ; d[0]=1 #d i r a c impulse

a l i s t =[0.2 , 0.8 , 0.9 , 0.95 , 0.99 , 0.999 , 1.001 , 1.01]

for a in alist :

h= l f i l t e r ( [ 1 ] , [1 , −a ] , d)

_=p l t . p l o t ( h , l a b e l =" a={} " . f o r m a t ( a ) )

p l t . ylim ( [ 0 , 1 . 5 ] )

p l t . x l a b e l ( ' Time ' )

_=p l t . l e g e n d ( )

6.3 Third example:


This example shows that environments like itemize or enumerate are also available. As already
indicated, this is useful for copying text from a TeX le. Following the same idea, text formating
commands \textit, \textbf, \underline, etc are also available.

Page 17/19
The following \ t e x t i t { environments } are available :

\ begin { itemize }

\ item \ t e x t b f { Theorems and likes }

\ b e g i n { enumerate }

\ item theorem ,

\ item lemma ,

\ item corollary

\ item . . .

\ end { e n u m e r a t e }

\ item \ textbf { exercises }

\ 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:

• Theorems and likes

1. theorem,

2. lemma,

3. corollary

4. . . .

• exercises

1. problem,

2. example,

3. exercise

7 Disclaimer, sources and thanks

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

You might also like