How to Write Chemistry Formulas in LaTeX (mhchem)
To typeset chemical formulas in LaTeX, load mhchem and write \ce{H2O} or \ce{2H2 + O2 -> 2H2O}. It handles subscripts, charges, and reaction arrows.
To typeset chemical formulas and equations in LaTeX, load the mhchem package and write them inside \ce{} — for example \ce{H2O}, \ce{SO4^2-}, or \ce{2H2 + O2 -> 2H2O}. mhchem parses the formula automatically and sets subscripts, charges, coefficients, and reaction arrows correctly, far more cleanly than building them by hand in math mode. It works with the default pdfLaTeX engine.
1. Setup and basic formulas
\usepackage{mhchem} % needs amsmath (usually already loaded)
...
Water is \ce{H2O} and sulfuric acid is \ce{H2SO4}.
Numbers become subscripts automatically — you write H2O, mhchem renders the 2 as a subscript.
2. Reactions and arrows
\ce{2H2 + O2 -> 2H2O} % forward reaction
\ce{N2 + 3H2 <=> 2NH3} % equilibrium
\ce{A ->[\text{catalyst}] B} % conditions above arrow
| Notation | Meaning |
|---|---|
| -> | Yields (forward) |
| <=> | Equilibrium |
| <- | Reverse |
| ->[text] | Label above the arrow |
| ->[a][b] | Above and below |
3. Charges, states, and isotopes
\ce{Na+} % sodium ion
\ce{SO4^2-} % sulfate, subscript 4 + superscript 2-
\ce{CO2(g)} % gas state
\ce{H2O(l)} % liquid state
\ce{^{14}_{6}C} % carbon-14 isotope
Charges go after the species with ^; mhchem keeps them separate from subscript counts.
4. Inside equations
\ce{} works in math mode, so you can put reactions in numbered equations and align:
\begin{equation}
\ce{CaCO3 ->[\Delta] CaO + CO2}
\end{equation}
See the math equations guide for the surrounding environments.
5. Where it fits
mhchem is essential for chemistry lab reports, theses, and papers. For physics units and quantities alongside formulas, pair it with siunitx — covered in the lab report guide.
→ Typeset chemical equations with instant preview in LetX.
Written by Shihab Shahriar Antor — AI Engineer & Founder of Shahriar Labs, maker of LetX.
Frequently Asked Questions
How do I write a chemical equation with arrows in LaTeX?
Load mhchem and use \ce{} with arrow notation: \ce{2H2 + O2 -> 2H2O} for a forward reaction, <=> for equilibrium, and ->[catalyst] to label an arrow with conditions above it. mhchem parses the formula automatically, so you write numbers and symbols naturally and it sets the subscripts, coefficients, and spacing correctly. This is far cleaner than building equations with manual subscripts in math mode.
How do I write ions and charges, like sulfate or a sodium ion?
Inside \ce{}, write charges after the species with a caret: \ce{Na+} for a sodium ion, \ce{SO4^2-} for sulfate, and \ce{Ca^2+} for calcium. mhchem positions the charge as a superscript and keeps the subscript count separate, so \ce{SO4^2-} correctly shows a subscript 4 and a superscript 2−. You don't need to enter math mode; mhchem handles the formatting within the \ce command.
Does mhchem work with pdfLaTeX, or do I need a special engine?
mhchem works with all engines, including the default pdfLaTeX — just add \usepackage{mhchem} to the preamble. It depends on the amsmath package, which you almost certainly already load, and integrates with math mode so you can use \ce inside equations and align environments. There's no special compiler requirement, which makes it easy to add to an existing chemistry paper or lab report.