All guides

Chemical formulas in LaTeX

Updated:

The mhchem package turns plain reaction notation into a properly typeset formula: digits after an element symbol become subscripts and charges become superscripts. No manual underscores needed.

Preamble

\usepackage[version=4]{mhchem}

The version=4 option selects the current syntax of the package; without it mhchem prints a warning on every compilation. Overleaf has the package installed, so there is nothing to download.

Copying this from handwritten notes? Snap a photo of the page and CamteX turns the formulas into LaTeX. Try it free

Reaction equation

\ce{2H2 + O2 -> 2H2O}
2 H2+O2⟶2 H2O2\,\mathrm{H_2} + \mathrm{O_2} \longrightarrow 2\,\mathrm{H_2O}

A coefficient in front of a formula (2H2) stays on the line, while a digit after an element symbol becomes a subscript. The reaction arrow is typed as two characters: ->.

Ions and equilibrium

\ce{CH3COOH <=> CH3COO- + H+}
CH3COOH⇌CH3COO−+H+\mathrm{CH_3COOH} \rightleftharpoons \mathrm{CH_3COO^-} + \mathrm{H^+}

A + or - right after a formula becomes a superscript charge. Write multiple charges with the digit: \ce{SO4^2-}, \ce{Fe^3+}. The <=> pair of arrows marks a reversible reaction.

Conditions, gas and precipitate

\ce{CaCO3 ->[T] CaO + CO2 ^}
\ce{Ba^2+ + SO4^2- -> BaSO4 v}

Text in square brackets after an arrow goes above it, and a second pair of brackets goes below. A ^ separated by a space draws the arrow for an escaping gas, and the letter v the arrow for a precipitate.

States of matter

\ce{AgNO3(aq) + NaCl(aq) -> AgCl(s) + NaNO3(aq)}

The state goes in parentheses right after the formula: (s), (l), (g), (aq). mhchem keeps it upright and does not turn the letters inside the parentheses into subscripts.

A chemical formula inside math

$K_a = \frac{[\ce{CH3COO-}][\ce{H+}]}{[\ce{CH3COOH}]}$
Ka=[CH3COO−][H+][CH3COOH]K_a = \frac{[\mathrm{CH_3COO^-}][\mathrm{H^+}]}{[\mathrm{CH_3COOH}]}

\ce works in math mode too, so a chemical formula can sit inside an equilibrium constant, a fraction or a subscript without switching modes.

Common mistakes

See also

Don't want to memorize commands?

Draw a formula or paste a screenshot, and CamteX turns it into ready LaTeX.

Try it free