6.8: Displaying Chemical Formulas
- Page ID
- 557635
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\dsum}{\displaystyle\sum\limits} \)
\( \newcommand{\dint}{\displaystyle\int\limits} \)
\( \newcommand{\dlim}{\displaystyle\lim\limits} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\(\newcommand{\longvect}{\overrightarrow}\)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\ket}[1]{\left| #1 \right>}\)
\(\newcommand{\bra}[1]{\left< #1 \right|}\)
\(\newcommand{\braket}[2]{\left< #1 \vphantom{#2} \right| \left. #2 \vphantom{#1} \right>}\)
\(\newcommand{\braopket}[3]{\left< #1 \vphantom{#2}\vphantom{#3} \right| #2 \vphantom{#1}\vphantom{#3} \left| #3 \vphantom{#1}\vphantom{#2} \right>}\)
\(\newcommand{\qmvec}[1]{\mathbf{\vec{#1}}}\)
\(\newcommand{\op}[1]{\hat{\mathbf{#1}}}\)
\(\newcommand{\expect}[1]{\langle #1 \rangle}\)
\(\newcommand{\dfn}[1]{\emph{\textbf{#1}}}\)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)
This page explains how to display chemical formulas and reaction equations in WeBWorK problems using the mhchem extension for MathJax. Students see properly typeset formulas such as \( \ce{H2O} \) and balanced equations without any image files or external libraries.
Use this page when: you need to display chemical formulas, ions, or reaction equations in a PGML problem. The \ce{} command is part of the mhchem package, which is loaded automatically by MathJax in WeBWorK. For rendering graphical molecular structures from SMILES strings, see Rendering Chemical Structures with RDKit instead.
Background on displaying chemical formulas
MathJax includes the mhchem extension, which provides the \ce{} command for typesetting chemical formulas and equations. This is the same syntax used in LaTeX chemistry documents and on LibreTexts pages. Because MathJax is already loaded in WeBWorK, no additional macros or JavaScript are required.
Use \ce{} whenever you need chemical notation in a problem. It handles subscripts, superscripts, charges, reaction arrows, and stoichiometric coefficients automatically, producing cleaner output than manually formatting with standard math mode.
Displaying chemical formulas
Inline formulas
Wrap \ce{} inside \( ... \) to display a formula inline with the surrounding text.
The molecular formula for glucose is \( \ce{C6H12O6} \).
This renders as: The molecular formula for glucose is \( \ce{C6H12O6} \).
Display-mode equations
Wrap \ce{} inside \[ ... \] (or use PGML double-backtick ``) to display an equation on its own centered line.
[``\ce{C6H12O6 + 6 O2 -> 6 CO2 + 6 H2O}``]
This renders as:
\[ \ce{C6H12O6 + 6 O2 -> 6 CO2 + 6 H2O} \nonumber \]
Common mhchem syntax
| Feature | Syntax | Renders as |
|---|---|---|
| Molecular formula | \ce{H2SO4} |
\( \ce{H2SO4} \) |
| Ionic charge | \ce{SO4^2-} |
\( \ce{SO4^2-} \) |
| Reaction arrow | \ce{A -> B} |
\( \ce{A -> B} \) |
| Equilibrium arrow | \ce{A <=> B} |
\( \ce{A <=> B} \) |
| State symbols | \ce{NaCl(aq)} |
\( \ce{NaCl(aq)} \) |
| Stoichiometric coefficients | \ce{2 H2 + O2 -> 2 H2O} |
\( \ce{2 H2 + O2 -> 2 H2O} \) |
Using \ce{} inside RadioButtons choices
When chemical formulas appear as answer choices in a RadioButtons widget, wrap each formula in inline math delimiters \( \ce{...} \). The radio button will render the formula using MathJax.
$rb = RadioButtons(
[ "\( \ce{C6H12O6} \)", "\( \ce{O2} \)", "\( \ce{CO2} \)", "\( \ce{H2O} \)" ],
"\( \ce{C6H12O6} \)",
);
Complete working example
Cellular respiration with mhchem -- A full problem that displays a balanced chemical equation using \ce{} and asks students to identify the oxidized reactant with RadioButtons.
## TITLE('Identify the oxidized reactant in cellular respiration')
## DESCRIPTION
## A balanced chemical equation is rendered using mhchem. Students
## identify which reactant is being oxidized.
## ENDDESCRIPTION
## KEYWORDS('mhchem','chemical formula','cellular respiration','RadioButtons')
## DBsubject('Chemistry')
## DBchapter('Chemical Reactions')
## DBsection('Oxidation-Reduction')
## Date('2026-02-26')
## Author('Author name')
## Institution('Institution name')
DOCUMENT();
loadMacros(
"PGstandard.pl",
"PGML.pl",
"parserRadioButtons.pl",
"PGcourse.pl",
);
$rb = RadioButtons(
[ "\( \ce{C6H12O6} \)", "\( \ce{O2} \)", "\( \ce{CO2} \)", "\( \ce{H2O} \)" ],
"\( \ce{C6H12O6} \)",
labels => "ABC",
displayLabels => 1,
separator => '<div style="margin-bottom: 0.7em;"></div>',
uncheckable => 1,
randomize => 1,
);
BEGIN_PGML
In the following chemical reaction for cellular respiration, which reactant is being oxidized?
[``\ce{C6H12O6 + 6 O2 -> 6 CO2 + 6 H2O}``]
[_]{$rb}
END_PGML
ENDDOCUMENT();
Apply it today
- Use
\ce{}for any chemical formula or equation instead of manually formatting subscripts and superscripts in math mode. - For inline formulas, use
\( \ce{...} \). For display equations, use PGML double-backtick syntax:[``\ce{...}``]. - For graphical molecular structures (ring systems, stereochemistry), use RDKit.js instead (see Rendering Chemical Structures with RDKit).


