134
$\begingroup$

This FAQ explains and gives examples how to typeset chemical equations, quantities and units, and mathematical expressions on Chemistry Stack Exchange (rather than posting pictures, which you can't edit or search). Refer to the table of contents below to find topics you need to format questions and answers.

The technology used for typesetting is called MathJax. MathJax's syntax is very similar to LaTeX, so if you have written mathematical expressios with LaTeX before, almost everything you are used to will work. Chemical equations can be typeset with the mhchem package for MathJax, which largely behaves identically to the mhchem LaTeX package. Units and scientific notation are also formatted using mhchem.

Table of contents

  1. Getting started

  2. Basic MathJax

  3. Basic mhchem

  4. Learning by example

  5. Further reading

$\endgroup$
0

10 Answers 10

116
$\begingroup$

(1) Getting started

There are three different types of markup (formatting commands) that we can differentiate between.


Markdown

Markdown is a simple markup language that is widely used on the Internet, and is built into all Stack Exchange websites. It allows you to (for example) write bold text, italicised text, and create hyperlinks.

When writing a question or answer, there are a number of buttons in the editing box that can help you with this. For more information, please see the editing help.

A limited subset of raw HTML tags are allowed on Stack Exchange, but these should generally not be required to write an answer.


MathJax

MathJax is a library which allows your web browser to display expressions written in $\LaTeX$ syntax.

Expressions that are enclosed within single ($...$) or double ($$...$$) dollar signs are interpreted using MathJax. Where possible, all mathematical expressions, except those in titles, should be typed using MathJax.

  • $...$ - A pair of single dollar signs specify an inline equation. This means you can use it seamlessly inside a sentence.

    Type this:

    Let $V$ denote the volume of a gas.
    

    to get this:

    Let $V$ denote the volume of a gas.

  • $$...$$ - A pair of double dollar signs specify a display equation. It gets its own line, is generally slightly larger, and is centred on the page.

    Type this:

    The ideal gas law is written as:
    $$ pV = nRT $$
    

    to get this:

    The ideal gas law is written as: $$ pV = nRT $$


mhchem

The mhchem package for MathJax adds extra functionality for chemical equations, as well as quantities with units, in MathJax. It provides two main commands:

  1. \ce{...} is used for typesetting chemical formulae and equations;

  2. \pu{...} is used for typesetting numbers with units / dimensions.

These two commands must appear within a MathJax expression: that means it must itself be enclosed either with $...$ or $$...$$. Whatever is placed within the braces will be automatically passed to the mhchem package for rendering.

Note that the use of plain MathJax, i.e. $H_2O$, leads to italicised chemical symbols like $H_2O$ which are not correct. Therefore, chemical equations should always be typeset using mhchem, and not simply in plain MathJax.

Type this:

The chemical formula of water is $\ce{H2O}$.

The freezing point of water is $\pu{273.15 K}$.

to get this:

The chemical formula of water is $\ce{H2O}$.

The freezing point of water is $\pu{273.15 K}$.


(Back to index)

Next section: (2.1) Basic MathJax: Superscripts and subscripts

$\endgroup$
0
15
$\begingroup$

(4) Learning by example

The MathJax and mhchem syntax in the left-hand side must be enclosed in single dollar signs $...$ for an inline (smaller) equation, or double dollar signs $$...$$ for a larger, centred, display-style equation.

Type... To get...
\ce{H2O} $$\ce{H2O}$$
\ce{Ca^2+(aq) + 2 OH-(aq) <=> Ca(OH)2(s)} $$\ce{Ca^2+(aq) + 2 OH-(aq) <=> Ca(OH)2(s)}$$
\ce{A ->[catalyst] B} $$\ce{A ->[catalyst] B}$$
\pu{3.4E-8} $$\pu{3.4E-8}$$
\pu{8.314 J K-1 mol-1} $$\pu{8.314 J K-1 mol-1}$$
K_\mathrm{eq} $$K_\mathrm{eq}$$
\mathrm{p}K_\mathrm{a} $$\mathrm{p}K_\mathrm{a}$$
\Delta_\mathrm{r} G^\circ $$\Delta_\mathrm{r} G^\circ$$
\Delta_\mathrm{r} G^⦵ $$\Delta_\mathrm{r} G^⦵$$
E = E^\circ - \frac{RT}{zF}\ln{Q} $$E = E^\circ - \frac{RT}{zF}\ln{Q}$$
\left(\frac{\frac{2}{3}}{\frac{6}{7}}\right) $$\left(\frac{\frac{2}{3}}{\frac{6}{7}}\right)$$
\ce{K\overset{+7}{Mn}O4} $$\ce{K\overset{+7}{Mn}O4}$$
\left(\frac{\partial U}{\partial T}\right)_V = C_{V} $$\left(\frac{\partial U}{\partial T}\right)_V = C_{V}$$
\int_{V_{i}}^{V_{f}}p \, \mathrm{d}V $$\int_{V_{i}}^{V_{f}}p \, \mathrm{d}V$$
\sum_{i=1}^{\infty}\frac{1}{a^{i}} $$\sum_{i=1}^{\infty}\frac{1}{a^{i}}$$

(Back to index)

Previous section: (3.3) Basic mhchem: Quantities with units

Next section: (5) Further reading

$\endgroup$
0
6
$\begingroup$

(3.1) Basic mhchem: Chemical formulae

Chemical formulae can be typeset with the \ce{...} command. Note that the \ce{...} command must itself be placed in a MathJax expression, i.e. it must be surrounded by single or double dollar signs.

For the most part, mhchem behaves as intuitively as it possibly can. Therefore, you do not have to specify most subscripts, and charges at the end of a molecule are automatically typeset in superscript form.

Type this:

$\ce{H2SO4}$ dissociates into $\ce{H+}$ and $\ce{HSO4-}$.

to get this:

$\ce{H2SO4}$ dissociates into $\ce{H+}$ and $\ce{HSO4-}$.

However, if you have a multiply charged species (i.e. a charge more than 1), then you need to introduce an explicit superscript with ^.

Type this:

$\ce{CuSO4}$ dissociates into $\ce{Cu^2+}$ and $\ce{SO4^2-}$.

Not $\ce{Cu2+}$ and $\ce{SO42-}$!

to get this:

$\ce{CuSO4}$ dissociates into $\ce{Cu^2+}$ and $\ce{SO4^2-}$.

Not $\ce{Cu2+}$ and $\ce{SO42-}$!


(Back to index)

Previous section: (2.4) Basic MathJax: Other commands

Next section: (3.2) Basic mhchem: Chemical reactions

$\endgroup$
3
$\begingroup$

(2.1) Basic MathJax: Superscripts and subscripts

You can denote superscripts using the ^ character, and subscripts using _.

Type this:

The heat capacity at constant volume is $C_V$.

Einstein's mass–energy relation is $E = mc^2$.

to get this:

The heat capacity at constant volume is $C_V$.

Einstein's mass–energy relation is $E = mc^2$.

If you want to include more than one character in the super- or subscript, you need to enclose it in curly braces {...}.

Type this:

The half-life of a substance is denoted by $t_{1/2}$ (not $t_1/2$).

to get this:

The half-life of a substance is denoted by $t_{1/2}$ (not $t_1/2$).


(Back to index)

Previous section: (1) Getting started

Next section: (2.2) Fractions and square roots

$\endgroup$
3
$\begingroup$

(2.2) Basic MathJax: Fractions and square roots

Fractions

Fractions can be typeset using \frac{<numerator>}{<denominator>}. It is preferable to typeset fractions in display style (i.e. with two dollar signs $$...$$).

Type this:

For an ideal gas, we have
$$ \frac{pV}{nRT} = 1. $$

to get this:

For an ideal gas, we have $$ \frac{pV}{nRT} = 1. $$

If fractions must be typed inline (using single dollar signs), it is better to write them using the '/' division sign (properly termed a solidus), or a negative power.

Type this:

For a real gas, we can define the compressibility factor $Z = pV/nRT$,
which is in general not equal to $1$.

to get this:

For a real gas, we can define the compressibility factor $Z = pV/nRT$, which is in general not equal to $1$.


Square roots

Square roots can be added in a similar manner using \sqrt{....}.

Type this:

The root-mean-square velocity of a gas is $\sqrt{3RT/M}$.

to get this:

The root-mean-square velocity of a gas is $\sqrt{3RT/M}$.


(Back to index)

Previous section: (2.1) Basic MathJax: Superscripts and subscripts

Next section: (2.3) Basic MathJax: Greek letters and other symbols

$\endgroup$
3
$\begingroup$

(3.2) Basic mhchem: Chemical reactions

You can combine multiple chemical formulae into one large chemical equation within a single \ce{...} command.

This is best explained with an example. The + symbol acts as a plus sign as long as it is separated from other chemical formulae by one or more spaces, and there are a number of builtin arrows such as ->. Stoichiometric coefficients, like the 2 below, can simply be placed before the appropriate chemical.

Type this:

Sulfuric acid dissociation: $$\ce{H2SO4 -> 2 H+ + SO4^2-}$$

to get this:

Sulfuric acid dissociation: $$\ce{H2SO4 -> 2 H+ + SO4^2-}$$

A few of the common arrows are listed here:

Type... To get... Meaning
-> $\ce{->}$ net forward reaction; elementary step
<- $\ce{<-}$ net backward reaction
<-> $\ce{<->}$ resonance structures (not for equilibria)
<=> $\ce{<=>}$ equilibrium
<=>> $\ce{<=>>}$ equilibrium favouring forward reaction
<<=> $\ce{<<=>}$ equilibrium favouring reverse reaction

(Back to index)

Previous section: (3.1) Basic mhchem: Chemical formulae

Next section: (3.3) Basic mhchem: Quantities with units

$\endgroup$
3
$\begingroup$

(2.4) Basic MathJax: Other commands

Many mathematical operators, such as the trigonometric operators and logarithms, can (and should) be typeset with MathJax commands that are prefixed with backslashes.

Type this:

A famous formula for entropy is $S = k \ln W$.

to get this:

A famous formula for entropy is $S = k \ln{W}$.

Note that the simple $ln W$ leads to incorrect output $ln W$.

A list of common commands is provided here:

Type... To get... Meaning
\sin $\sin$ sine
\cos $\cos$ cosine
\tan $\tan$ tangent
\arcsin $\arcsin$ inverse sine (or use \sin^{-1})
\arccos $\arccos$ inverse cosine (or use \cos^{-1})
\arctan $\arctan$ inverse tangent (or use \tan^{-1})
\exp $\exp$ exponential
\log $\log$ logarithm (use \log_{b} for base b)
\ln $\ln$ natural logarithm

(Back to index)

Previous section: (2.3) Basic MathJax: Greek letters and other symbols

Next section: (3.1) Basic mhchem: Chemical formulae

$\endgroup$
3
$\begingroup$

(2.3) Basic MathJax: Greek letters and other symbols

Note that all of the following must be typed within a MathJax expression (either in inline or display mode). If these commands are followed by alphabetical characters, they must be separated from the command by a space.

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.

Type this:

The Larmor frequency is $\nu = \gamma B_0 / 2 \pi$.

A photon of wavelength $\lambda$ can excite an electron:

$$\Delta E = \frac{hc}{\lambda}$$

to get this:

The Larmor frequency is $\nu = \gamma B_0 / 2 \pi$.

A photon of wavelength $\lambda$ can excite an electron:

$$\Delta E = \frac{hc}{\lambda}$$

The lunate Greek letters can be typed using (for example) \varrho $\varrho$ instead of \rho $\rho$, but these are not often needed. See the links at the bottom of this answer for more information.


Other symbols

A non-exhaustive selection of commonly-used symbols is presented below.

Type... To get... Meaning
= $=$ equal to
\pm $\pm$ plus or minus
\times $\times$ multiplication symbol
\cdot $\cdot$ small centre dot (for multiplication)
\neq $\neq$ not equal to
< $<$ less than
> $>$ greater than
\leq $\leq$ less than or equal to
\geq $\geq$ greater than or equal to
\ll $\ll$ much less than
\gg $\gg$ much greater than
\geq $\geq$ greater than or equal to
\approx $\approx$ approximately equal to
\sim $\sim$ similar to
\propto $\propto$ proportional to
\infty $\infty$ infinity symbol
\partial $\partial$ partial derivative symbol
\hbar $\hbar$ reduced Planck constant

A more complete list of symbols may be found at the following websites:


I can't find a symbol I want!

You can try drawing it at https://detexify.kirelabs.org/classify.html. Note, however, that MathJax does not include, or support the use of, arbitrary LaTeX packages. Therefore, if Detexify indicates that some obscure package is needed for a particular symbol (it will say \usepackage{...}), there is a good chance that it will not work on MathJax.


(Back to index)

Previous section: (2.2) Basic MathJax: Fractions and square roots

Next section: (2.4) Basic MathJax: Other commands

$\endgroup$
2
$\begingroup$

(3.3) Basic mhchem: Quantities with units

Quantities with units can be typeset using the \pu{...} command. Just like \ce{...}, this must itself be part of a MathJax expression, i.e. enclosed within dollar signs.

Note that typing these with plain MathJax will typically lead to incorrect italicisation and spacing: for example, $298 K$ is rendered as $298 K$ (wrong) whereas \pu{298 K} is rendered as $\pu{298 K}$ (correct).

Type this:

Boltzmann's constant has a value of $\pu{1.381e-23 J K-1}$.

to get this:

Boltzmann's constant has a value of $\pu{1.381e-23 J K-1}$.

On top of the automatically correct spacing and italicisation which the \pu{...} command provides, there are also several convenience shortcuts that are present in this example, which are only available with the \pu{...} command:

  • The automatic expansion of xxxeyy to xxx \cdot 10^{yy} (you can replace e with E to get a multiplication symbol $\times$).

  • No need for an explicit superscript in the units.

There are more examples of \pu{...} usage at How to write physical units.


(Back to index)

Previous section: (3.2) Basic mhchem: Chemical reactions

Next section: (4) Learning by example

$\endgroup$
2
$\begingroup$

(5) Further reading


Italic vs upright symbols

There are a number of rules which specify when symbols should be written in upright font and when they should be italicised. We have already seen some of these:

  • Chemical formulae: $\ce{H2O}$ is correct, $H_2O$ is wrong
  • Units: $\pu{298 K}$ is correct, $298\,K$ is wrong

In plain MathJax, you can typeset upright text using the \mathrm{...} command. This is often needed for physical symbols, such as Avogadro's number, $N_{\mathrm{A}}$ $N_{\mathrm{A}}$.

To learn about when this is necessary, please see: Which symbols are written in roman (upright) font and which are italicized?.


More advanced formatting

There is a collection of tips available at Hidden points of editing you probably didn't know.

On top of that, there are a number of MathJax extensions (some of which are rather obscure) which provide additional specialised functionality: What additional formatting features are available to MathJax (possibly via \require{})?


TeX and LaTeX

The syntax of MathJax is very similar to the "math mode" of the $\TeX$ and $\LaTeX$ typesetting language of Knuth and Lamport.

Note, however, that the broader formatting capabilities (e.g. chapter/section/other environments) of $\TeX$ are not available within MathJax.


mhchem

Note that, just like how MathJax is not LaTeX, the mhchem MathJax package is not the same thing as the mhchem LaTeX package (though it is developed by the same person).

The full documentation for MathJax mhchem is located at https://mhchem.github.io/MathJax-mhchem.


(Back to index)

Previous section: (4) Learning by example

$\endgroup$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .