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}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+}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}]}$\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
- H_2O in math mode: the letters come out in italics, while chemical formulas are set upright.
- No spaces around the plus sign: in \ce{H2+O2} mhchem may read the plus as a charge. Separate the species of a reaction with spaces.
- \rightarrow instead of -> inside \ce: the package has its own arrows (->, <-, <=>, <->), and only those stretch when reaction conditions are written above them.
See also
Don't want to memorize commands?
Draw a formula or paste a screenshot, and CamteX turns it into ready LaTeX.