How-to

How to Write Chemical Formulas in LaTeX

The fix

Load \usepackage{mhchem} and wrap chemistry in \ce{...}. mhchem handles subscripts, charges, and reaction arrows automatically: \ce{H2SO4}, \ce{SO4^2-}, and \ce{2H2 + O2 -> 2H2O}. It works in both text and math mode.

Not sure it worked on your document? Paste it into LetX and compile it, free and in the browser.

Example

\usepackage{mhchem}
% ...
Water is \ce{H2O}.
\[ \ce{2H2 + O2 -> 2H2O} \]
An ion: \ce{SO4^2-}

Frequently asked questions

How do I write a reaction arrow?

Inside \ce{...} use -> for a forward arrow, <=> for equilibrium, and <-> for resonance. mhchem spaces and sizes them correctly.

How do I write charges and states?

Write charges as \ce{Na+} or \ce{SO4^2-}, and states with (s), (l), (g), (aq): \ce{NaCl(s)}.

Related LaTeX fixes

Browse all LaTeX problems & fixes, or start with the LaTeX guide.

Learn the topic properly

Try the fix in LetX

Open the editor, paste your code, and compile in seconds to see the error clear. Free, in your browser.

Open LetX Free