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!

0

3 Answers 3

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

0

With an up to date chemmacros (v6.1 2022/02/13) this works out of the box:

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

\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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .