Writing Mathematical Expressions in Jupyter Notebooks

Being able to write mathematical expressions in Jupyter notebooks will make your notebooks standout. They will provide clearer equations and your notebooks will look a lot more professional.

Jupyter notebooks are an incredibly powerful tool for data analysis, scientific research, and educational purposes. One of the many useful features of Jupyter Notebooks is the ability to write and render mathematical expressions using LaTeX, a typesetting system that can display complex mathematical notation. In this blog post, we’ll walk you through the process of writing and rendering mathematical expressions in your Jupyter Notebook, with plenty of code examples to help you along the way.

Getting Started with LaTeX in Jupyter Notebooks

Before we begin, make sure you have a Jupyter Notebook up and running. If you’re not familiar with installing and using Jupyter Notebooks, please follow the official installation guide here.

Once you have your Jupyter Notebook open, you can start writing mathematical expressions using LaTeX. To do this, we’ll use Markdown cells, a type of cell in Jupyter Notebooks that allows you to write text and equations using the Markdown language and LaTeX.

Here’s a simple example to get you started:

  1. Create a new Markdown cell by clicking on the ‘+’ button on the toolbar and selecting ‘Markdown’ from the dropdown menu.
  2. In the new Markdown cell, type the following code:
To display an inline equation, use single dollar signs: $E = mc^2$
  1. Press Shift+Enter to render the cell. You should now see the equation displayed inline with the text:

To display an inline equation, use single dollar signs: E = mc^2

Displaying Equations on Separate Lines

In some cases, you may want to display equations on separate lines, centered, and with an optional equation number. To achieve this, you can use double dollar signs or the align environment.

Example 1: Using double dollar signs

  1. Create a new Markdown cell.
  2. In the new Markdown cell, type the following code:
To display an equation on a separate line, use double dollar signs:
$$
E = mc^2
$$
  1. Press Shift+Enter to render the cell. You should now see the equation displayed on a separate line and centered:

To display an equation on a separate line, use double dollar signs:

E = mc^2

Example 2: Using the align environment

  1. Create a new Markdown cell.
  2. In the new Markdown cell, type the following code:
To display an equation with an equation number, use the `align` environment:
\begin{align}
E = mc^2
\end{align}
  1. Press Shift+Enter to render the cell. You should now see the equation displayed on a separate line, centered, and with an equation number:

To display an equation with an equation number, use the align environment:

E = mc^2

Adding Mathematical Symbols and Notation

LaTeX provides a vast array of symbols and notation that you can use to create complex mathematical expressions. In this section, we’ll introduce some common symbols and notation used in LaTeX.

Example 1: Greek letters

To insert Greek letters, simply type a backslash followed by the name of the letter. For uppercase Greek letters, use the first letter of the name in uppercase.

  1. Create a new Markdown cell.
  2. In the new Markdown cell, type the following code:
Lowercase Greek letters: $\alpha, \beta, \gamma, \delta, \epsilon$

Uppercase Greek letters: $\Gamma, \Delta, \Theta, \Lambda, \Pi$
  1. Press Shift+Enter to render the cell. You should see the Greek letters displayed:

Lowercase Greek letters: \alpha, \beta, \gamma, \delta, \epsilon

Uppercase Greek letters: \Gamma, \Delta, \Theta, \Lambda, \Pi

Example 2: Fractions

To create a fraction, use the \frac command followed by the numerator and denominator in curly braces.

  1. Create a new Markdown cell.
  2. In the new Markdown cell, type the following code:
Fractions: $\frac{a}{b}, \frac{1}{2}, \frac{x^2}{x^2 + y^2}$
  1. Press Shift+Enter to render the cell. You should see the fractions displayed:

Fractions: \frac{a}{b}, \frac{1}{2}, \frac{x^2}{x^2 + y^2}

Example 3: Summations and Integrals

To create summations and integrals, use the \sum and \int commands, respectively. You can also specify the limits of the summation or integral using the ^ and _ symbols followed by the desired expressions in curly braces.

  1. Create a new Markdown cell.
  2. In the new Markdown cell, type the following code:
Summation: $\sum_{i=1}^n i = \frac{n(n+1)}{2}$

Integral: $\int_{0}^{1} x^2 dx = \frac{1}{3}$
  1. Press Shift+Enter to render the cell. You should see the summation and integral displayed:

Summation: \sum_{i=1}^n i = \frac{n(n+1)}{2}

Integral: \int_{0}^{1} x^2 dx = \frac{1}{3}

These are just a few examples of the many mathematical symbols and notation available in LaTeX. You can find a comprehensive list of LaTeX symbols here.

Matrices and Arrays

LaTeX also allows you to create matrices and arrays to represent complex mathematical structures. In this section, we’ll show you how to create matrices and arrays using the bmatrix and array environments.

Example 1: Creating a matrix

To create a matrix, use the bmatrix environment inside the math delimiters.

  1. Create a new Markdown cell.
  2. In the new Markdown cell, type the following code:
Matrix example:
$$
\begin{bmatrix}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\end{bmatrix}
$$
  1. Press Shift+Enter to render the cell. You should see the matrix displayed:

Matrix example:
\begin{bmatrix}1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9\end{bmatrix}

Example 2: Creating an array

To create an array, use the array environment inside the math delimiters. You’ll also need to specify the alignment of the columns using l (left), c (center), or r (right).

  1. Create a new Markdown cell.
  2. In the new Markdown cell, type the following code:
Array example:
$$
\left\{
\begin{array}{l}
x = 3y + 4z \\
y = 2x - z \\
z = x + y
\end{array}
\right.
$$
  1. Press Shift+Enter to render the cell. You should see the array displayed:

Array example:

With these examples, you can create various types of matrices and arrays to represent mathematical structures in your Jupyter Notebook.

Customizing Text and Equation Appearance

In this final section, we’ll explore some options for customizing the appearance of your text and equations in Jupyter Notebook.

Example 1: Text formatting

To apply text formatting such as bold, italic, or underline, use the following commands within the math delimiters:

  1. Create a new Markdown cell.
  2. In the new Markdown cell, type the following code:
Bold: $\mathbf{This \; is \; bold}$

Italic: $\mathit{This \; is \; italic}$

Underline: $\underline{This \; is \; underlined}$
  1. Press Shift+Enter to render the cell. You should see the formatted text displayed:

Bold: \mathbf{This \; is \; bold}

Italic: \mathit{This \; is \; italic}

Underline: \underline{This \; is \; underlined}

Example 2: Font sizes

To change the font size of your equations, use the following commands within the math delimiters:

  1. Create a new Markdown cell.
  2. In the new Markdown cell, type the following code:
Small: $\small{E = mc^2}$

Large: $\large{E = mc^2}$

Huge: $\Huge{E = mc^2}$
  1. Press Shift+Enter to render the cell. You should see the equations displayed with different font sizes:

Small: \small{E = mc^2}

Large: \large{E = mc^2}

Huge: \Huge{E = mc^2}

With these customization options, you can create visually appealing and easy-to-read mathematical expressions in your Jupyter Notebook.

Conclusion

We hope this blog post has provided you with a solid understanding of how to write and render mathematical expressions in Jupyter Notebooks using LaTeX. With these tools, you can create professional-looking equations, matrices, and other mathematical structures to help you communicate complex concepts in your research, data analysis, or educational materials. Keep exploring the vast array of symbols and notation available in LaTeX, and don’t hesitate to consult the LaTeX documentation and community resources for further guidance.

I also have another article on writing the Greek Alphabet in a Jupyter Notebook, that article has proven to be my most popular article.

Check it out if you are interested, Happy coding!