# Macro's in mhchem?

I want to write hydroxyl radicals quite frequently, so I thought of making a small macro. But with the macro, the text is not processed as chemical compounds in the mhchem package. Consider the following MWE:

\documentclass{standalone}

\usepackage[version=3]{mhchem}
\usepackage{amsmath}

\begin{document}

This is with the macro \ce{\ohrad}, this is without the macro \ce{OH^{.}}

\end{document}


and the output

• This is perfectly normal, I'm afraid: \ce starts scanning token by token and what it finds is not a letter to be typeset upright, but a control sequence and it does nothing with it; but eventually TeX expands it (in math mode). – egreg Oct 7 '13 at 20:10
• Use \newcommand\ohrad{\cf{OH^{.}}}. Macros reveal \ce innards: math mode... but mhchem's formulae are nestable – clemens Oct 7 '13 at 20:10

And according to @cgnieder it should work to use \cf{}. And it does:

\documentclass{standalone}

\usepackage[version=3]{mhchem}
\usepackage{amsmath}

\begin{document}

This is with the macro \ce{\ohrad}, this is without the macro \ce{OH^{.}}

\end{document}


Output

This adds a prefix \^ which causes the following macro to be expanded (once) while \ce is scanning.

\documentclass{standalone}

\usepackage[version=3]{mhchem}
\makeatletter
\let\old@mhchem@ce@i\mhchem@ce@i
\def\mhchem@ce@i{%
\expandafter\mhc@expand
\else
\expandafter\old@mhchem@ce@i
\fi}
\def\mhc@expand\^{\expandafter\mhchem@ce@continue}
\makeatother
\usepackage{amsmath}