Use environments to cluster mathematical expressions
MathJax provides a basic subset of environments for clustering mathematical expressions. For a complete list, see here, but be warned it is a very big page.
Environments are entered with the following syntax, some of them do not need to be explicitly entered with mathmode, but it is no harm doing so.
\begin{<environment>}
...
\end{<environment>}
In such environments a double backslash \\
or a carriage return \cr
separates rows.
The environments can be used in conjunction with the mhchem
commands.
The most basic one is gather
which is essentially the same as using mathmode without an environment. The only difference is that this environment allows multiple uses of \tag{...}
. If you use gathered
instead, then only one tag for the whole cluster will be created; it does not matter where the tag is placed. This environment comes without alignment possibilities.
Most of the time the typeset can actually benefit from alignment, so this environment is maybe not the best to choose. See here for example uses.
Basic alignment for multiple equations
Probably the most useful environment is align
. It can be used to align equations vertically, but also horizontally. Ampersands &
indicate the desired alignment position. Everything left of an odd instance is shoved to the right, and everything to the right is shoved to the left. Even uses of ampersands therefore can be used to separate equations on the same line.
Note that alignment characters also work inside chemical expressions \ce{...}
.
The following example code (% ...
produces a comment and everything following it will be ignored by MathJax, cannot be used within \ce{...}
. &
is the alignment character.)
\begin{align}
\ce{2H2 + O2 &<=> 2H2O} & %no line break
K & % align here
=\frac{a(\ce{H2O})^2}{a(\ce{H2})^2\,a(\ce{O2})} \tag{1}\\ % break line here
\ce{3H2 + N2 &<=> 2NH3} & %no line break
K & %align here
=\frac{a(\ce{NH3})^2}{a(\ce{H2})^3\,a(\ce{O2})} \tag{2} % no \\ required
\end{align}
yields:
\begin{align}
\ce{2H2 + O2 &<=> 2H2O} & %no line break
K & % align here
=\frac{a(\ce{H2O})^2}{a(\ce{H2})^2\,a(\ce{O2})} \tag{1}\\ % break line here
\ce{3H2 + N2 &<=> 2NH3} & %no line break
K & %align here
=\frac{a(\ce{NH3})^2}{a(\ce{H2})^3\,a(\ce{O2})} \tag{2} % no \\ required
\end{align}
You can use this environment to separate equations on the same line. The code
\begin{align}
c^2 &= a^2 + b^2 & % separate here
&\implies&
c &= \pm\sqrt{a^2 + b^2}
\end{align}
yields:
\begin{align}
c^2 &= a^2 + b^2 & % separate here
&\implies&
c &= \pm\sqrt{a^2 + b^2}
\end{align}
Equations too long for one line
Occasionally you have an equations that are too long for one line. Her the environment multline
comes in handy. In this the first line will be aligned to start at the left margin, while the last line will be aligned to end at the right margin. Lines between those will be centered. A \tag{...}
will be produced for the whole set, the placement does not matter. The following code
\begin{multline}\tag{3}
\sum_{n=0}^{\infty} \frac{f^{(n)}(a)}{n!}\,(x-a)^{n} = \\ % aligned left
f(a) +\frac{f'(a)}{1!}(x-a) +\frac{f''(a)}{2!}(x-a)^2\\ % centred
+\frac{f'''(a)}{3!}(x-a)^3 +\frac{f''''(a)}{4!}(x-a)^4 +\cdots. % right
\end{multline}
yields
\begin{multline}\tag{3}
\sum_{n=0}^{\infty} \frac{f^{(n)}(a)}{n!}\,(x-a)^{n} = \\ % aligned left
f(a) +\frac{f'(a)}{1!}(x-a) +\frac{f''(a)}{2!}(x-a)^2\\ % centred
+\frac{f'''(a)}{3!}(x-a)^3 +\frac{f''''(a)}{4!}(x-a)^4 +\cdots. % right
\end{multline}
A different possibility is tweaking the align
environment, like it is shown at the end of this answer. Alternatively you can use the split
environment, which works almost the same; however, it does not allow a tag. The following code
\begin{split}
\sum_{n=0}^{\infty} \frac{f^{(n)}(a)}{n!}\,(x-a)^{n} &=
f(a) +\frac{f'(a)}{1!}(x-a) +\frac{f''(a)}{2!}(x-a)^2\\
&\quad +\frac{f'''(a)}{3!}(x-a)^3 +\frac{f''''(a)}{4!}(x-a)^4 +\cdots.
\end{split}
yields:
\begin{split}
\sum_{n=0}^{\infty} \frac{f^{(n)}(a)}{n!}\,(x-a)^{n} &=
f(a) +\frac{f'(a)}{1!}(x-a) +\frac{f''(a)}{2!}(x-a)^2\\
&\quad +\frac{f'''(a)}{3!}(x-a)^3 +\frac{f''''(a)}{4!}(x-a)^4 +\cdots.
\end{split}
This environment can be encased in an align
environment if necessary. It might be beneficial if you want to sort terms. Entries before the first alignment character will be oriented to the right, but in every other instance it will be oriented to the left. In this environment multiple tags are allowed, hence the following code
\begin{align}
f(x):&= (1+x)\mathrm{e}^x \tag{4}\\ % first line
& = \mathrm{e}^x + x\mathrm{e}^x \tag{4a}\\
& = \sum_{n=0}^\infty \frac{x^n}{n!} %
+ \sum_{n=0}^\infty \frac{x^{n+1}}{n!} \tag{4b}\\
& = \left(%
\begin{split} % the following counts as one line
& 1 &+ x &+ \frac{x^2}{2!} &+ \frac{x^3}{3!} &+ \cdots\\
+~& x &+ x^2 &+ \frac{x^3}{2!} &+ \frac{x^4}{3!} &+ \cdots\\
\end{split}\right)\tag{4c}\\
& = \sum_{n=0}^\infty \frac{n+1}{n!}x^n \tag{4d}\\ % last line
\end{align}
yields:
\begin{align}
f(x):&= (1+x)\mathrm{e}^x \tag{4}\\ % first line
& = \mathrm{e}^x + x\mathrm{e}^x \tag{4a}\\
& = \sum_{n=0}^\infty \frac{x^n}{n!} %
+ \sum_{n=0}^\infty \frac{x^{n+1}}{n!} \tag{4b}\\
& = \left(%
\begin{split} % the following counts as one line
& 1 &+ x &+ \frac{x^2}{2!} &+ \frac{x^3}{3!} &+ \cdots\\
+~& x &+ x^2 &+ \frac{x^3}{2!} &+ \frac{x^4}{3!} &+ \cdots\\
\end{split}\right)\tag{4c}\\
& = \sum_{n=0}^\infty \frac{n+1}{n!}x^n \tag{4d}\\ % last line
\end{align}
Adding labels and referring to them
In a maths environment you can add labels \label
to tags \tag
, which then can be referred \eqref
or \ref
to later.
For example the following code
The equilibrium constant is defined by the expression
$$
K_x=\prod_{\ce{B}} x_{\ce{B}}^{\nu_{\ce{B}}}.
\tag{5}\label{equ-constant}
$$
[...] As seen in equation \ref{equ-constant} [...]
[...] insert into \eqref{equ-constant} [...]
produces the following output:
The equilibrium constant is defined by the expression
$$
K_x=\prod_{\ce{B}} x_{\ce{B}}^{\nu_{\ce{B}}}.
\tag{5}\label{equ-constant}
$$
[...] As seen in equation \ref{equ-constant} [...]
[...] insert into \eqref{equ-constant} [...]
These references can be inserted directly without $
signs, but they can also be used in conjunction with them.
Labels can be applied wherever tags can also be applied. They will also insert a link that brings you back to where the label was defined.
Since MathJax is rendered per page and not per post, the labels will affect other posts, too. If you get an error, try a different label.
Conclusion
Especially aligning equations can be very beneficial to the readability of a post.
There are quite a few environments that might be useful to achieve this. In most cases, however, you don't need such extensive maths and the align
environment is quite sufficient.