Chemical formulas and reactions in LaTeX (mhchem, ce)

The mhchem package writes chemical formulas and reactions with \ce{...}: \ce{H2O} lowers the 2 automatically, and \ce{2H2 + O2 -> 2H2O} prints a reaction with an arrow.

Preamble

The snippets on this page need these lines before \begin{document}:

\usepackage[version=4]{mhchem}

Molecular formulas

A number after an element symbol becomes a subscript; a number in front stays at full size as a coefficient. The letters are upright, and \ce works in text and in math mode. Load the package with version=4: without the option it writes a warning to the log.

Water is \ce{H2O}, sulfuric acid \ce{H2SO4}, slaked lime \ce{Ca(OH)2}
and blue vitriol \ce{CuSO4.5H2O}.
Result
The compiled example

Open in the editor

Charges and isotopes

A + or - directly after a formula becomes a raised charge. A charge with a number follows ^. For an isotope, put the mass number after ^ and the atomic number after _ in front of the symbol.

\ce{Na+}, \ce{OH-}, \ce{SO4^2-}, \ce{Fe^3+}, \ce{^{14}_{6}C}
Result
The compiled example

Open in the editor

Reaction equations

Put spaces around + and around the arrow. -> is a reaction arrow and <=> an equilibrium arrow. Text in square brackets after the arrow is printed above it. A ^ with spaces around it prints an upward arrow for a gas, a v a downward arrow for a precipitate.

\ce{2H2 + O2 -> 2H2O}

\ce{N2 + 3H2 <=> 2NH3}

\ce{CaCO3 ->[\Delta] CaO + CO2 ^}

\ce{AgNO3 + NaCl -> AgCl v + NaNO3}
Result
The compiled example

Open in the editor

Formulas inside equations

Inside a math environment \ce sets the chemistry upright, and the rest of the formula stays normal math. An e- is printed as an electron.

\[
  \ce{Zn^2+ + 2e- -> Zn}
  \qquad
  K = \frac{[\ce{NH3}]^2}{[\ce{N2}] \, [\ce{H2}]^3}
\]
Result
The compiled example

Open in the editor

Structural formulas

The chemfig package draws structures with \chemfig{...}. -, = and ~ are single, double and triple bonds. A number in square brackets sets the direction of a bond in steps of 45 degrees, so [2] points up and [6] down. Parentheses start a branch, and *6(...) draws a ring with six atoms.

Complete document:

\documentclass{article}
\usepackage{chemfig}
\begin{document}
\chemfig{H-C(-[2]H)(-[6]H)-H}
\qquad
\chemfig{*6(-=-=-=)}
\end{document}
Result
The compiled example

Open in the editor

Pitfalls

Compiled with pdfLaTeX from TeX Live 2023.

Related pages