9

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

Charles
  • 947
  • 1
  • 15
  • 39

1 Answers1

22

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

Marius
  • 58,213
  • 16
  • 107
  • 105
  • Marius, my problem with that is that I am using a module that relies on display(Math()) to make the equations and unfortunately, it seems that adding in the && into the display(Math()) results in the output not working.. – Charles Apr 24 '14 at 01:49
  • `display(Math())` works fine for me, at least for your simple example- see the new code I've added. If you can figure out the difference between my example and the thing you're actually having trouble with, I might be able to help, otherwise I'm not really sure what's going on. – Marius Apr 24 '14 at 01:55
  • Excellent! Thank you! It looks like the \begin{align} and \end{align} are necessary for the Math() to work. Thank you so much! – Charles Apr 24 '14 at 01:55
  • 1
    It's possible there's something different about my environment, but I found that double dollar signs $$ ... $$ automatically inserted \begin{equation} tags. Using single dollar signs $ ... $ resolved the problem. I also only needed single &s for alignment – RedPanda Oct 17 '19 at 03:30