Aligning, numbering and formatting math equations in Jupyter notebook are easy with LaTeX and HTML
One might be familiar with how to insert math equations with LaTeX in a Jupyter notebook, while not so many people know how to format, align them center, right and number them in the Jupyter notebook. Thus, in this post I illustrate how to easily format, align and number math equations in a Jupyter notebook.
Let’s use the following equation, for example.
To create the above equation, we just use the following LaTeX syntax in a Markdown cell in the Jupyter notebook.
$x = {-b \pm \sqrt{b^2-4ac} \over 2a}$
1. Left alignment
Normally, an equation generated using $ $
with LaTeX is left aligned in default in the Jupyter notebook. The result looks as follows in a Jupyter notebook.
2. Center Alignment
An equation can also easily center aligned by using two pairs of $$ $$
.
$x = {-b \pm \sqrt{b^2-4ac} \over 2a}$
The results look as follows:
3. Right Alignment
A convenient way to align an equation right is to use the ‘style=text-align: right;’
attribute inside a HTML tag, say <p> tag.
<p style='text-align: right; '>
$x = {-b \pm \sqrt{b^2-4ac} \over 2a}$
</p>
The outcome looks as follows:
We can use this method to easily align an equation to center and left by just changing the position to center or left.
4. Number equations
For example, let’s add the number (1) to the equation as follows:
We just add \tag{1}
to the end of LaTeX equation as follows:
$x = {-b \pm \sqrt{b^2-4ac} \over 2a} \tag{1}$
Besides, we can also add label and refer it in text by adding \label{eq:special}
to the equation. Let’s take the following instance:
$x$ value will be caculated using equation \eqref{eq:special}.
$
x = {-b \pm \sqrt{b^2-4ac} \over 2a} \tag{1} \label{eq:special}
$
It results in the following outcome:
5. Format equation
We can format the equation with different fonts, font size, color, etc. by using the ‘style’
attribute inside a HTML tag. Let’s see the following example, in which, beside aligning the equation to center, we also use font of Time New Roman
with size of 2.0em
and red color.
<p style='text-align: center;
font-family:Time New Roman;
font-size:2.0em;
color:red;'>
$x = {-b \pm \sqrt{b^2-4ac} \over 2a}$
</p>
It results in the following output:
6. Video tutorials
If you want to learn to create math equations and symbols, aligning and formatting them in details, please watch my video tutorial Learn Creating Math Equations and Symbols using Latex in Jupyter Notebook with 1 Hour on YouTube. This video has gained many views and subscribers since it was published on October 12, 2021.
6. Online course
If you are interested in learning Jupyter notebook in details, you are welcome to enroll one of my course Practical Jupyter Notebook from Beginner to Expert.