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

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

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}

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.