我想经常写羟基自由基,所以我想到做一个小宏。但是使用宏,文本在mhchem
包中不会被处理为化合物。考虑以下 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}
和输出
答案1
并且根据 @cgnieder 的说法,它应该可以使用\cf{}
。并且确实如此:
\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}
输出
答案2
这会添加一个前缀,导致以下宏在扫描\^
时被扩展(一次) 。\ce
\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}