I have some chemical formulae throughout my whole text that are changed from time to time. So I defined a macro \compound
with the approporiate content for usage in chemmacros'
\ch
macro.
Compiling the document (see MWE) shows that the direct entry works as expected (is typeset as a formula), while the second version is typeset as normal text.
\documentclass{minimal}
\usepackage{chemmacros}
\newcommand{\compound}{H2O}
\begin{document}
Works: \ch{H2O}
Does not work: \ch{\compound}
\end{document}
Do I miss (or misunderstand) something?
Any help is very appreciated. Thanks in advance.
Best Answer
\ch
is not provided bychemmacros
but by thechemformula
package.As you've noticed it doesn't expand its argument. As a consequence when it sees
\compound
(unaware of its definition) it does nothing with it and just inputs it. Only afterwards\compound
expands to its definition but then it is too late.That means you either need to do that manually by adding the appropriate number of
\expandafter
s or you can define a\chX
(say) which expands its argument once before parsing it:If you choose the
\chX
way together withchemmacros
you either should also loadchemformula
explicitly (\usepackage{chemmacros}\usepackage{chemformula}
) or tellchemformula
to do it for you (\chemsetup{formula = chemformula}
) before defining the variant.