5

I am using the reaction environment from the chemmacros package to typeset a few, simple chemical reactions.

I would like to customize the way I am referencing to my reaction. Ideally, the whole reference name (and hence hyperlink) would be "Reaction {#1}" (yes, with the curly brackets, to be consistent with the equation tag).

I cannot use the cleveref package, as it has weird interactions with the rest of my document, I would rather stick with the \ref and \autoref commands. Here is my MWE (please note that I am actually using a memoir class document, if it changes anything):

\documentclass{article}
\usepackage{chemmacros}
\usepackage{hyperref}
\chemsetup{modules = all}
\begin{document}
Reaction \autoref{rxn:water} is good.
\begin{reaction}
H3O+ + HO- = 2 H2O "\label{rxn:water}"
\end{reaction}
\\
Reaction \ref{rxn:water} is just water...
\end{document}

Thanks in advance!

4

You can modify how hyperref chooses the string to write in the aux file by testing whether \theHequation begins with R, which is how chemmacros modifies it.

\documentclass{article}
\usepackage{amsmath}
\usepackage{chemmacros}
\usepackage{hyperref}

\chemsetup{modules=all,greek=textgreek}

\makeatletter % patchcmd from etoolbox, loaded by chemmacros
\patchcmd{\hyper@makecurrent}
 {\edef\Hy@param{#1}}
 {\xHy@param@def{#1}}
 {}{}
\def\xHy@param@def#1{%
  \edef\@tempa{\csname theH#1\endcsname}%
  \edef\@tempa{\expandafter\@car\@tempa\@nil}%
  \edef\Hy@param{\if R\@tempa reaction\else#1\fi}%
}
\makeatother
\newcommand\reactionautorefname{Reaction}

\begin{document}

\autoref{rxn:water} is good.
\begin{reaction}
H3O+ + HO- = 2 H2O "\label{rxn:water}"
\end{reaction}
Reaction \ref{rxn:water} is just water...

\end{document}

enter image description here

3

You can adapt \ref{...} to be used with chemical reactions only:

\newcommand{\rxnref}[1]{Reaction~\{\#\ref{#1}\}}

However, I cannot think of a good solution for \autoref{...}. Normally you would want to adjust the prefix with something like \def\reactionautorefname{Reaction}, but to my knowledge there is no \reactionautorefname{...} to begin with. chemmacros's reaction environment is a wrapper for amsmath's align environment, hence you would need to use \def\equationautorefname{Reaction}, but in this case both reactions and math equations receive "Reaction" prefix, which make little sense. Therefore the best solution is probably to use rxnref{...} only:

\documentclass{article}
\usepackage{chemmacros}
    \chemsetup{modules = all}
\usepackage{hyperref}
\newcommand{\rxnref}[1]{Reaction~\{\#\ref{#1}\}}

\begin{document}

\begin{reaction}
    H3O+ + HO- = 2 H2O "\label{rxn:water}"
\end{reaction}

\rxnref{rxn:water} is just water...
\end{document}

enter image description here

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.