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.
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)}.
Try the fix in LetX
Open the editor, paste your code, and compile in 1 second to see the error clear — free, in your browser.
Open LetX Free