##// END OF EJS Templates
added tests of passing alternative formatter functions
added tests of passing alternative formatter functions

File last commit:

r8567:2d0fdb4b
r8632:b24126ce
Show More
Typesetting Math Using MathJax.ipynb
346 lines | 10.8 KiB | text/plain | TextLexer
/ docs / examples / notebooks / Typesetting Math Using MathJax.ipynb

The Markdown parser included in IPython is MathJax-aware. This means that you can freely mix in mathematical expressions using the MathJax subset of Tex and LaTeX. Some examples from the MathJax site are reproduced below, as well as the Markdown+TeX source.

Motivating Examples


The Lorenz Equations

Source

\begin{aligned}
\dot{x} & = \sigma(y-x) \\
\dot{y} & = \rho x - y - xz \\
\dot{z} & = -\beta z + xy
\end{aligned}

Display

\begin{aligned} \dot{x} & = \sigma(y-x) \\ \dot{y} & = \rho x - y - xz \\ \dot{z} & = -\beta z + xy \end{aligned}

The Cauchy-Schwarz Inequality

Source

\begin{equation*}
\left( \sum_{k=1}^n a_k b_k \right)^2 \leq \left( \sum_{k=1}^n a_k^2 \right) \left( \sum_{k=1}^n b_k^2 \right)
\end{equation*}

Display

\begin{equation*} \left( \sum_{k=1}^n a_k b_k \right)^2 \leq \left( \sum_{k=1}^n a_k^2 \right) \left( \sum_{k=1}^n b_k^2 \right) \end{equation*}

A Cross Product Formula

Source

\begin{equation*}
\mathbf{V}_1 \times \mathbf{V}_2 =  \begin{vmatrix}
\mathbf{i} & \mathbf{j} & \mathbf{k} \\
\frac{\partial X}{\partial u} &  \frac{\partial Y}{\partial u} & 0 \\
\frac{\partial X}{\partial v} &  \frac{\partial Y}{\partial v} & 0
\end{vmatrix}  
\end{equation*}

Display

\begin{equation*} \mathbf{V}_1 \times \mathbf{V}_2 = \begin{vmatrix} \mathbf{i} & \mathbf{j} & \mathbf{k} \\ \frac{\partial X}{\partial u} & \frac{\partial Y}{\partial u} & 0 \\ \frac{\partial X}{\partial v} & \frac{\partial Y}{\partial v} & 0 \end{vmatrix} \end{equation*}

The probability of getting (k) heads when flipping (n) coins is

Source

\begin{equation*}
P(E)   = {n \choose k} p^k (1-p)^{ n-k} 
\end{equation*}

Display

\begin{equation*} P(E) = {n \choose k} p^k (1-p)^{ n-k} \end{equation*}

An Identity of Ramanujan

Source

\begin{equation*}
\frac{1}{\Bigl(\sqrt{\phi \sqrt{5}}-\phi\Bigr) e^{\frac25 \pi}} =
1+\frac{e^{-2\pi}} {1+\frac{e^{-4\pi}} {1+\frac{e^{-6\pi}}
{1+\frac{e^{-8\pi}} {1+\ldots} } } } 
\end{equation*}

Display

\begin{equation*} \frac{1}{\Bigl(\sqrt{\phi \sqrt{5}}-\phi\Bigr) e^{\frac25 \pi}} = 1+\frac{e^{-2\pi}} {1+\frac{e^{-4\pi}} {1+\frac{e^{-6\pi}} {1+\frac{e^{-8\pi}} {1+\ldots} } } } \end{equation*}

A Rogers-Ramanujan Identity

Source

\begin{equation*}
1 +  \frac{q^2}{(1-q)}+\frac{q^6}{(1-q)(1-q^2)}+\cdots =
\prod_{j=0}^{\infty}\frac{1}{(1-q^{5j+2})(1-q^{5j+3})},
\quad\quad \text{for $|q|<1$}. 
\end{equation*}

Display

\begin{equation*} 1 + \frac{q^2}{(1-q)}+\frac{q^6}{(1-q)(1-q^2)}+\cdots = \prod_{j=0}^{\infty}\frac{1}{(1-q^{5j+2})(1-q^{5j+3})}, \quad\quad \text{for $|q|<1$}. \end{equation*}

Maxwell's Equations

Source

\begin{aligned}
\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\   \nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\
\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\
\nabla \cdot \vec{\mathbf{B}} & = 0 
\end{aligned}

Display

\begin{aligned} \nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\ \nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\ \nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\ \nabla \cdot \vec{\mathbf{B}} & = 0 \end{aligned}

Equation Numbering and References


These equation reference examples are adapted from an example page in the MathJax documentation. Note that it's okay to reference equations across cells. Click inside this cell to see the source.

Labeled equations and references

Here is a labeled equation: \begin{equation} x+1\over\sqrt{1-x^2}\label{ref1} \end{equation}

with a reference to ref1: \ref{ref1}, and another numbered one with no label: \begin{equation} x+1\over\sqrt{1-x^2} \end{equation}

\nonumber and equation*

This one uses \nonumber: \begin{equation} x+1\over\sqrt{1-x^2}\nonumber \end{equation}

Here's one with the equation* environment: \begin{equation*} x+1\over\sqrt{1-x^2} \end{equation*}

Forward references

This is a forward reference [\ref{ref2}] and another \eqref{ref2} for the following equation:

\begin{equation} x+1\over\sqrt{1-x^2}\label{ref2} \end{equation}

More math: \begin{equation} x+1\over\sqrt{1-x^2} \end{equation}

References inline and in environments

Here is a ref inside math: $\ref{ref2}+1$ and text after it.

\begin{align} x& = y_1-y_2+y_3-y_5+y_8-\dots && \text{by \eqref{ref1}}\\ & = y'\circ y^* && \text{(by \eqref{ref3})}\\ & = y(0) y' && \text {by Axiom 1.} \end{align}

Missing references

Here's a bad ref [\ref{ref4}] to a nonexistent label.

Numbering align environments

An alignment: \begin{align} a&=b\label{ref3}\cr &=c+d \end{align} and a starred one: \begin{align*} a&=b\cr &=c+d \end{align*}

Inline Typesetting (Mixing Markdown and TeX)


While display equations look good for a page of samples, the ability to mix math and formatted text in a paragraph is also important.

Source

This

Display

This expression $\sqrt{3x-1}+(1+x)^2$ is an example of a TeX inline equation in a Markdown-formatted sentence.

Other Syntax


You will notice in other places on the web that $$ are needed explicitly to begin and end MathJax typesetting. This is not required if you will be using TeX environments, but the IPython notebook will accept this syntax on legacy notebooks.

Source

$$
\begin{array}{c}
y_1 \\\
y_2 \mathtt{t}_i \\\
z_{3,4}
\end{array}
$$
$$
\begin{array}{c}
y_1 \cr
y_2 \mathtt{t}_i \cr
y_{3}
\end{array}
$$
$$\begin{eqnarray} 
x' &=& &x \sin\phi &+& z \cos\phi \\
z' &=& - &x \cos\phi &+& z \sin\phi \\
\end{eqnarray}$$
$$
x=4
$$

Display

$$ \begin{array}{c} y_1 \\\ y_2 \mathtt{t}_i \\\ z_{3,4} \end{array} $$$$ \begin{array}{c} y_1 \cr y_2 \mathtt{t}_i \cr y_{3} \end{array} $$$$\begin{eqnarray} x' &=& &x \sin\phi &+& z \cos\phi \\ z' &=& - &x \cos\phi &+& z \sin\phi \\ \end{eqnarray}$$$$ x=4 $$
In [ ]: