Typesetting chemistry in MathJax
Source: Quantum Computing SE meta: Formatting chemical formulae and reactions
The LaTeX package mhchem
is not activated here, by default. However,
you can load it yourself, by using the command \require{\mhchem}
(you need to use it only once on a page).
Example: \require{\mhchem}\ce{H2O}
generates
$\require{\mhchem}\ce{H2O}$.
Courtesy: @DavidCervone on Physics SE
Chemistry Stack Exchange has a community wiki by @ManishEarth titled How can I format math/chemistry expressions here? which documents the MathJax commands for chemistry. Since they are free to alter/delete their copy without consulting us, let's have our own, using their template. Constructive edits below this line most welcome.
Getting started
On chemistry.SE, we use MathJax to format mathematical as well as chemical equations and similar expressions in questions, answers, and comments. MathJax allows us to typeset expressions using $\LaTeX$ notation.
To use MathJax, enclose your math within single ($...$
) or double ($$...$$
) dollar signs. Single dollar signs make the math inline, for example, Let $x$ be a variable
gives:
Let $x$ be a variable.
On the other hand, double dollar signs make the math a block element. It gets its own line, and is slightly larger. For example, The equation of motion is as follows: $$v=u+at$$ It is a SUVAT equation
gives:
The equation of motion is as follows: $$v=u+at$$ It is a SUVAT equation
Note that, in math mode, MathJax ignores the spaces you type, e.g. $a b$
yields $a b$. MathJax formats expressions the way it is common in mathematics texts. However, the printing rules for signs and symbols used in the natural sciences and technology may require additional spaces (in particular, between the numerical value and the unit symbol). In math mode, use \
(backslash space) or ~
(tilde) if you want the equivalent of space in normal text. Where separation of numbers into groups of three digits is used, the groups shall be separated by a thin space \,
(backslash comma); e.g. $299\,792\,458$
yields $299\,792\,458$.
Basic chem
We use the mhchem package for chemistry. It lets us easily format chemical formulas and reactions without typing too much.
There really is only one command you need to know here: \ce{...}
. \ce{...}
takes its parameters and automatically formats it. For example,
$\ce{HCl}$ dissociates in water as follows:
$$\ce{H2O + HCl <=> H3O+ + Cl-}$$
Renders as
$\ce{HCl}$ dissociates in water as follows:
$$\ce{H2O + HCl <=> H3O+ + Cl-}$$
Note that spaces are very important for mhchem to separate super/subscripts from normal text. \ce{H3O+}
will display $\ce{H3O+}$, but \ce{H2O +}
will display $\ce{H2O +}$. When typesetting ions with more than a single charge, the argument has to be raised using the caret (^; also known as the circumflex accent) character, e.g. $\ce{Cu^2+}$
renders as $\ce{Cu^2+}$, while $\ce{Cu2+}$
would incorrectly render to $\ce{Cu2+}$. (Also see basic math below.)
Various types of reaction arrows are supported, including ->
, <=>
, <==>>
, etc.
It also supports various types of bonds, via the \bond{..}
command (to be called inside \ce{...}
). You need not call \bond
for normal bonds.
Eg: \ce{H\bond{->}A-B=C#D\bond{~}E\bond{~-}F\bond{...}G\bond{<-}E}
displays:
$$\ce{H\bond{->}A-B=C#D\bond{~}E\bond{~-}F\bond{...}G\bond{<-}E}$$
Full documentation of mhchem here
Basic math
Superscripts and subscripts
You can denote superscripts via the ^
character, and subscripts via _
. For example, x^2
renders as $x^2$, x_1
renders as $x_1$, and x_1^3
renders as $x_1^3$.
If you want to include more than one character in the super/sub script, enclose it in curly braces ({...}
).
For example, x^10
renders as $x^10$, but x^{10}
renders as $x^{10}$
Fractions and square roots
Fractions can be easily displayed using \frac{..}{..}
. For example, $$\frac{a+b^c}{de+f}$$
renders as
$$\frac{a+b^c}{de+f}$$
However, where it is necessary to include fractions in the body text, they shall, where possible, be reduced to a single level by using a solidus (/) or, where applicable, the negative index. For example, instead of $\frac{1}{\sqrt 2}$ write $1/\sqrt 2$ or $2^{-1/2}$; instead of $C_{\mathrm m,p} = 33.58\ \mathrm{\frac{J}{K \cdot mol}}$ write $C_{\mathrm m,p} = 33.58\ \mathrm{J/(K \cdot mol)}$ or $C_{\mathrm m,p} = 33.58\ \mathrm{J\cdot K^{-1} \cdot mol^{-1}}$.
Protip: You can exclude the braces for single-character numerators/denominators (if the first character is a letter, you need to use a space after \frac
, though). For example \frac12
renders as $\frac12$, and \frac ab
renders as $\frac ab$.
Square roots can be added in a similar manner, via \sqrt{....}
. For example, \sqrt{x+y}
renders as $\sqrt{x+y}$.
Greek letters
Greek letters can be added using a backslash ('\'), followed by the name of the letter. Captialise the first letter of the name for greek capital letters.
Eg \alpha \beta \gamma \Omega
renders as $\alpha \beta \gamma \Omega$.
Make sure that you put spaces after these if you are typing normal alphabet characters. Eg e^{\pii}
gives an error, you need to use e^{\pi i}
.
Note that there are special commands \varepsilon , \varsigma , \varrho , and \varpi
to distinguish between the lunate Greek letters.
MathJax extensions
MathJax has available a variety of extensions enabling other features: strikeout lines, enclosures, text/background coloration, interactive equations, etc. Information about these extensions and how to enable them can be found at this meta post.
Further reading
If you would like to know more, you can continue reading about which symbols are written in italic (sloping) type and which are printed in roman (upright) type.
Note: As of April 30 2017 the CDN hosting at cdn.mathjax.org was shut down, as a result directories such as https://github.com/mathjax/MathJax-third-party-extensions/tree/master/mhchem (like the one appearing in the original of the text below) will 404. According to Issue #39 "[IMPORTANT] MathJax CDN end-of-life & suggested changes to third-party extensions #39" all 3rd party extensions will be hosted in separate repositories (one per extensions). See link to Issue #39 for a partial list of the new locations.
MathJax/mhchem Extension 3.3 was moved to: https://github.com/mhchem/MathJax-mhchem. The full Mathjax/mhchem is now located at: https://mhchem.github.io/MathJax-mhchem/.