Jupyter Notebook

How to use Latex in Jupyter notebook

Most people usually face difficulty in writing mathematical equations on the computer. They need an appropriate application or software to overcome this problem for writing algebraic and scientific equations. LaTeX is a high-quality software for preparing scientific documentations. It consists of TeX macros and programs to process Latex technical documents. Now, Latex is available free in most online source code editors. One of the most popular is Jupyter Notebook that uses MathJax to render the Latex syntax inside the markdown/HTML. To use LaTeX in the Jupyter notebook, put the Latex math content inside the ‘$ … $’ double ‘$$ … $$’ symbols. The latex file is saved with the (.tex) extension.

This guide will briefly demonstrate how to use Latex in the Jupyter Notebook.

What is MathJax? 

MathJax is a JavaScript cross-browser library that displays mathematical or scientific notations in web browsers using Latex and MathML markup. You do not need to install or download extra plugins so that it helps you to write web documents and include mathematics using JavaScript.

Before preparing the document in the Latex syntax in the Jupyter notebook, make sure that MathJax should be included in your python environment. You can include the MathJax in your python environment by using the pip or npm package manager as follows:

> Pip3 install mathjax

Below, some Latex notations and their purpose are demonstrated that we will implement in Jupyter Notebook for writing mathematical equations:

Latex notations for writing Mathematical Equations

First, select the markdown cells in the jupyter notebook. Now, use dollar symbols $$, to start and end the mathematics mode in the Jupyter notebook.

Add basic Math equations

The following notations help you to write basic mathematical equations in Latex jupyter notebook:

  • In math mode, use \, symbol to add a little spacing.
  • For adding a new line, use \\
  • For displaying fraction values, use \frac{value 1}{value 2}
  • For superscripts or power, use ^{}
  • For adding subscripts or indices, use _{}
  • For taking square root of a value, use \sqrt{n}{value}

By using the above Latex notations, we have written the following mathematical equations:

$$

\frac{a+b}{X+Y} , x^4, e^{i\pi} , X_i , Y_{ij}\\ \sqrt[4]{5}

$$

Output

Add Roman Letters

To add roman letters in Jupyter notebook, use the following symbols:

$$

\sin(-\alpha)=-\sin(\alpha)\\

\arccos(x)=\arcsin(u)\\

\log_n(n)=1\\

\tan(x) = \frac{\sin(x)}{\cos(x)}

$$

Output

Add Greek Letters

To add greek letters in an equation, use \ and type the name of the Greek letter. Some greek letters are available in both capital and lowercase. To include the capital greek letter, type the first letter after \ symbol in uppercase as follows:

\sigma, \Sigma \\
\delta, \Delta \\
\omega, \Omega \\
\gamma, \Gamma \\
\Lamda, \Lambda \\

Output

See the following examples that will elaborate how to write Greek letters in Jupyter notebook using Latex:

$$

\alpha = \frac{2\pi}{3}\, rad , \pi = 3.14

$$

Output

$$

4\omega = 3\pi f ,

f = \frac{2c}{\lambda} ,

\Delta_\pi=\theta^3+\lambda ,

\Delta = \frac{1}{\lambda^3}

$$

Output

Add Sets and Logics

To add sets and logical symbols in jupyter notebook, use the following notations:

$$\mathbb{N} \subset \mathbb{Z} \subset \mathbb{D} \subset \mathbb{Q} \subset \mathbb{R} \subset \mathbb{C}$$

Output

Type the ‘\’ and then write the name of the notation as follows:

\subset , \cap , \cup , \forall , \exists , \varnothing , \emptyset

Add Derivatives and Integrals

To add integrals and derivatives using Latex in the jupyter notebook, use the following symbols:

$$f'(x) \\

f^{(k)}(x) \\

\frac{\partial f}{\partial x} \\

\frac{\partial^{k} f}{\partial x^k} $$

$$\int_{a}^b f(x)dx \\

\int_{a}^b\int_{c}^d f(x,y)\,dxdy \\

\iiint \\

\oint$$

Add Curly Braces

We can also add vertical and horizontal curly braces using Latex in the jupyter notebook. To start the left curly brace, use the following attribute:

\left\{

To close the vertical curl brace use

\right\}

$$
sign(y) = \left\{
    \begin{array}\\
        2 & \mbox{if } \ y \in \mathbf{Z}^* \\
        0 & \mbox{if } \ y = 0 \\
        -2 & \mbox{else.}
    \end{array}
\right.
$$

\\

$$
 \left.
    \begin{array} \\
        \beta^2 = \sqrt4 \\
        \beta \geq 0
    \end{array}
\right \} \beta = 5
$$

Output

For horizontal brace, use the following attributes:

$$

\underbrace{\ln \left( \frac{3}{4} \right)}_{\simeq -0.1456}

< \overbrace{\exp (3)}^{\simeq 6.8390}

$$

Output

Add Matrix

We can add different types of matrices using Latex in the jupyter notebook.

For adding plain matrices:

\begin{matrix}

5 & 4 & 3\\

x & y & z

\end{matrix}

For round brackets:

\begin{pmatrix}

5 & 4 & 3\\

x & y & z

\end{pmatrix}

For pipes:

\begin{vmatrix}

5 & 4 & 3\\

x & y & z

\end{vmatrix}

For curly brackets:

\begin{Bmatrix}

1 & 2 & 3\\

x & y & z

\end{Bmatrix}

Output

Add Function Limits

To apply a limit function, use the ‘\limit’ inside the dollar symbol. The following notations mostly used for writing function Limits:

$$\lim_{y \to -\infty} f(y)$$

$$\max_{y \in [a,b]}f(y)$$

$$\min_{y \in [\alpha,\beta]}f(y)$$

Output

Add Sum and Product

To add sum and product functions using Latex in jupyter notebook, use the following notations:

For Sum rule:

$$\sum_{j=0}^{+\infty} A_{j}$$

For adding Product rule

$$\prod_{j=1}^k A_{\alpha_j}$$

Output

Conclusion

We have explained how to use Latex in a Jupyter notebook. We have seen how different notations help us write the mathematical equations in a Jupyter notebook. We explored almost all attributes that are used in Latex for writing scientific and mathematical equations. Always select the markdown cells option to write all Latex notations in the Jupyter notebook.

About the author

Samreena Aslam

Samreena Aslam holds a master’s degree in Software Engineering. Currently, she's working as a Freelancer & Technical writer. She's a Linux enthusiast and has written various articles on Computer programming, different Linux flavors including Ubuntu, Debian, CentOS, and Mint.