Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ipython notebook align Latex equations in Ipython.Display module

I am using ipython notebook to write latex equations with the following modules

from IPython.display import display, Math, Latex

a simple example code might look like:

display(Math('a = \\frac{1}{2}'))
display(Math('b = \\frac{1}{3}'))
display(Math('c = \\frac{1}{4}'))

Which would output:

a = 1/2
b = 1/3
c = 1/4

in a pretty print format. Is there a way that I can somehow align this in columns like:

a = 1/2      b = 1/3      c = 1/4

? I know that the markup allows HTML usage, but this markup is entered into the input for a code-based cell. Any help/advice is greatly appreciated!!

like image 399
Charles Avatar asked Apr 24 '14 01:04

Charles


People also ask

How do you align a formula in a Jupyter Notebook?

You separate lines in the equations with a double backslash ( // ). Insert an ampersand ( & ) in each line at the alignment point. All equations will be aligned at the location of the ampersand symbols (and, of course, the ampersands will not appear in the rendered equations).

How do I display LaTeX in Jupyter?

The Jupyter Notebook uses MathJax to render LaTeX inside HTML / Markdown. Just put your LaTeX math inside $ $ . Or enter in display math mode by writing between $$ $$ . The [n] is optional.

How do you align math equations in LaTeX?

Inside the equation environment, use the split environment to split the equations into smaller pieces, these smaller pieces will be aligned accordingly. The double backslash works as a newline character. Use the ampersand character & , to set the points where the equations are vertically aligned.

What does %% do in Jupyter Notebook?

Both ! and % allow you to run shell commands from a Jupyter notebook. % is provided by the IPython kernel and allows you to run "magic commands", many of which include well-known shell commands. ! , provided by Jupyter, allows shell commands to be run within cells.


1 Answers

I'm not sure how familiar you are with LaTeX, but the notebook does allow you to use things like \begin{align}, allowing you to organize things into columns by separating them with & symbols. For example, this works if you enter it into a Markdown cell:

$$
\begin{align}
a = \frac{1}{2} && b = \frac{1}{3} && c = \frac{1}{4} \\
a && b && c
\end{align}
$$

This also works using display(Math()), e.g.:

display(Math(
r"""
\begin{align}
a = \frac{1}{2} && b = \frac{1}{3} && c = \frac{1}{4} \\
a && b && c \\
1 && 2 && 3
\end{align}
"""))

Giving you this output:

enter image description here

like image 57
Marius Avatar answered Sep 27 '22 21:09

Marius