# Environment for chemical reaction with reference

I need a chemical reaction environment, numbered and able to be referenced/labeled. It must also that is consistent with equation in its looks. I have read many questions and my code is an adaptation of this answer.

However I would really like to be able to use the same default \label.

I don't need a List of Reactions as most other questions seems to seek.

\documentclass{article}
\usepackage{hyperref}
\usepackage{amsmath,}
\usepackage{mhchem}[version=4]
\usepackage{tocloft}
\newcommand{\reactionlistname}{List of reactions}
\newlistof{reactions}{rxs}{\reactionlistname}

\newcounter{system}
\renewcommand*{\thesystem}{\arabic{system}}
\newcommand*{\systemautorefname}{Reaction}
\makeatletter
\renewcommand*{\p@system}{%
\expandafter\p@@system
}
\newcommand*{\p@@system}[1]{%
#1%
}
\makeatother

\newenvironment{reaction}[1][]{%
\refstepcounter{system}%
\ifx\\#1\\%
\else
\label{#1}%
\fi
$$% }{% \tag{\thesystem}%$$%
}

\begin{document}

You can see the chemical decomposition at \autoref{rea:bla} and its mathematical representation in \autoref{eq:foo}.

$$\label{eq:foo} t^2 + 27t$$

\begin{reaction}[rea:bla]
\ce{SiC + 2O2 -> CO2 + SiO2},
\end{reaction}

\end{document}


Edit 1:

I've also tried chemmacros, based on here, but then I get Equation 1 instead of Reaction 1:

\documentclass{article}
\usepackage{amsmath,}
\usepackage{chemmacros}
\chemsetup{
modules = {reactions} ,
formula = mhchem
}
\usepackage{hyperref}

\begin{document}
You can see the chemical decomposition at \autoref{rea:bla} and its mathematical representation in \autoref{eq:foo}.

$$\label{eq:foo} t^2 + 27t$$

\begin{reaction}\label{rea:bla}
\ce{SiC + 2O2 -> CO2 + SiO2},
\end{reaction}
\end{document}


• Perhaps the packages loading order? Normally, hyperref should be loaded as the last package, with very few exceptions (most notably cleveref). – Bernard Feb 19 at 19:06
• @Bernard Good tip, I've checked in my main document, it's already like that. I will try again with the MWE. – G. Bay Feb 19 at 21:57
• How could hyperref know that it should write Reaction? There is no variable names! – Sigur Feb 19 at 22:11
• @Sigur I have no idea. I thought the "module" reaction would do all the work for me... :( – G. Bay Feb 19 at 22:12
• Possibly related: Customizing ref of reaction environment – leandriis Feb 19 at 22:18

With the code of a previous answer of mine and some additions

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

\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}

\chemsetup{
modules = {reactions},
formula = mhchem,
reactions/tag-open=(R,
reactions/tag-close=),
}

\begin{document}
You can see the chemical decomposition at \autoref{rea:bla} and its
mathematical representation in \autoref{eq:foo}.
$$\label{eq:foo} t^2 + 27t$$
and there is a reaction
\begin{reaction}\label{rea:bla}
\ce{SiC + 2O2 -> CO2 + SiO2},
\end{reaction}

\end{document}


You can define new Reaction command; I also would suggest to use rxn acronym instead of rea for better code readability (rxn stands exclusively for "reaction", whereas "rea" has numerous meanings). Adapted from my answer:

\documentclass{article}
\usepackage{amsmath,}
\usepackage{chemmacros}
\chemsetup{
modules = {reactions},
formula = mhchem,
reactions/tag-open    = {(},
reactions/tag-close   = {)},
}
\usepackage{hyperref}
\newcommand{\rxnref}[1]{Reaction~\ref{#1}}

\begin{document}
You can see the chemical decomposition at \rxnref{rxn:bla} and its mathematical representation in \autoref{eq:foo}.

$$\label{eq:foo} t^2 + 27t$$

\begin{reaction}\label{rxn:bla}
\ce{SiC + 2O2 -> CO2 + SiO2}
\end{reaction}

\end{document}

• All right, that is a very simple solution and I like it. However it will require me to use a different macro each time. I didn't fully understand why I can't use \autoref in this case, could you explain a bit more? – G. Bay Feb 20 at 9:43
• And also why its using curly braces instead of parenthesis, is it possible to change? – G. Bay Feb 20 at 9:59
• Unfortunately, I don't know how to make \autoref work nicely with reaction environment of chemmacros; hyperref doesn't offer anything chemistry-related out of the box that matter. As for changing the brackets, see the updated answer: this is done via passing arguments in \chemsetup (reactions/tag-open and reactions/tag-close). – andselisk Feb 20 at 11:52