I get an "undefined control sequence" error when trying to use the mhchem and hyperref packages together, when \ce is within a \section{} label. A minimum example that demonstrates the problem:


The error message using pdflatex in MikTeX 2.9 on Windows:

! Undefined control sequence.
\mhchem@ce ...ate {s}\chardef \mhchem@ce@substate
                                                  =0\relax \mhchem@ce@result...
l.5 \section{\ce{H2O}}

I'm very new to mhchem, and latex in general -- is this a known problem? Is there a workaround? (As background, I first encountered this problem trying to use \ce within a markdown file and then make a pdf with pandoc. The template.latex file for pandoc adds \usepackage{hyperref} when using pdflatex.)

  • I think it's the fact that hyperref tries to add a bookmark with content \ce{H2O} and this will (probably fail) – user31729 Nov 21 '14 at 21:01
  • Does \section{\texorpdfstring{\ce{H2O}}{H2O}} help? – user31729 Nov 21 '14 at 21:04
  • @ChristianHupfer That fixes it, thank you! Thanks for explaining what's going on, too. – millikan Nov 21 '14 at 21:10
  • See Heiko Oberdiek's full and much better answer ;-) – user31729 Nov 21 '14 at 21:11

\ce breaks in the code that tries to convert it to a bookmark string. With \texorpdfstring an alternative can be provided for the bookmarks:

\section{\texorpdfstring{\ce{H2O}}{H\texttwoinferior O}}

In this case you are lucky, that there is a subscript 2 in Unicode (enabled by option unicode or pdfencoding=auto).

Also \ce can be disabled for bookmarks to print its argument at least:

\pdfstringdefDisableCommands{\let\ce\@firstofone}% after hyperref is loaded

Package bookmarks improves the algorithm for the bookmarks, that makes an additional LaTeX run obsolete in most cases. Also it adds more features.

Math example from the comments

\section{Water \texorpdfstring{\ce{H2O}}{H\texttwoinferior O}}
\section{Implication of \texorpdfstring{$E=mc^2$}{E = mc\twosuperior}}
\section{Solutions of \texorpdfstring
  {$\ddot s = -\omega^2 s$}%
  {\"s = \textminus\omega\twosuperior s}%

Result with math

| improve this answer | |
  • Off-topic comment: Is there any hope that PDF and the readers/viewers will understand accept math typesetting in bookmarks? – user31729 Nov 21 '14 at 21:16
  • @ChristianHupfer And who wants to read a PDF, where the math also pollutes the section titles? Having complicated math expressions in the title is not quite inviting to read the section. – Heiko Oberdiek Nov 21 '14 at 21:22
  • Thanks for the great answer! I like the unicode subscript 2. – millikan Nov 21 '14 at 21:23
  • @HeikoOberdiek: I don't want huge math expressions in section titles (or generic bookmarks), but something like inline intermixture, i.e. Implications of E=mc^2 or Solutions of \ddot{s} = -\omega^2 s. – user31729 Nov 21 '14 at 22:04
  • @ChristianHupfer These math examples can be expressed in Unicode, see updated answer. – Heiko Oberdiek Nov 22 '14 at 3:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.