5

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}

\newcommand\ohrad{OH^{.}}

\begin{document}

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

\end{document}

and the output

enter image description here

Holene
  • 6,920
  • 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 07 '13 at 20:10
  • 3
    Use \newcommand\ohrad{\cf{OH^{.}}}. Macros reveal \ce innards: math mode... but mhchem's formulae are nestable – cgnieder Oct 07 '13 at 20:10

2 Answers2

5

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

\documentclass{standalone}

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

\newcommand\ohrad{\cf{OH^{.}}}

\begin{document}

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

\end{document}

Output

enter image description here

Holene
  • 6,920
3

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{%
\ifx\mhchem@ce@lookahead\^%
   \expandafter\mhc@expand
\else
   \expandafter\old@mhchem@ce@i
\fi}
\def\mhc@expand\^{\expandafter\mhchem@ce@continue}
\makeatother
\usepackage{amsmath}

\newcommand\ohrad{OH^{.}}

\begin{document}


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

\end{document}
David Carlisle
  • 757,742