The mhchem package
Typesets chemical formulas and reaction equations from plain input with \ce, for example \ce{2H2 + O2 -> 2H2O}.
Loading the package
\usepackage[version=4]{mhchem}In math mode, H_2O gives italic letters, and every index and charge needs _{} or ^{}. Inside \ce{...} you type the formula the way you would write it in a text file: digits after an element become subscripts, + and - after a formula become charges, and -> becomes a reaction arrow. The letters stay upright.
\ce works in text and in math mode. Structural formulas with drawn bonds are a job for chemfig.
Main commands
\ce{H2O} | Formula; the digit becomes a subscript. |
\ce{SO4^2-} | Ion with charge 2−. |
\ce{2H2 + O2 -> 2H2O} | Reaction equation with stoichiometric numbers and an arrow. |
\ce{A <=> B} | Equilibrium arrow. |
\ce{A ->[H2O][\Delta] B} | Text above and below the arrow. |
\ce{^{14}_{6}C} | Isotope with mass number and atomic number. |
\ce{AgCl v}, \ce{CO2 ^} | Arrow down for a precipitate, arrow up for a gas; a space has to come before v or ^. |
Complete example
\documentclass{article}
\usepackage[version=4]{mhchem}
\begin{document}
Water is \ce{H2O}, the sulfate ion is \ce{SO4^2-},
and carbon-14 is written \ce{^{14}_{6}C}.
\begin{center}
\ce{2H2 + O2 -> 2H2O}
\ce{CO2 + H2O <=> H2CO3}
\ce{CaCO3 ->[\Delta] CaO + CO2 ^}
\ce{Ag+ + Cl- -> AgCl v}
\end{center}
\end{document}Compiled with pdfLaTeX from TeX Live 2023.
Pitfalls
- Load the package with
version=4. Without the option it prints a warning, because the output of the same input can differ between versions. - Spaces matter.
\ce{A + B}is a sum,\ce{A+B}reads the plus sign as a charge of A. mhchemhas no command for units. Write quantities withsiunitx.
Related pages
- How-to Chemical formulas and reactions in LaTeX
- Package
chemfig - Package
siunitx - How-to Exponents and subscripts in LaTeX
- How-to Superscript and subscript in text in LaTeX