# Command and counter for chemical equations

I want to define a command for chemical equations, using the mhchem package. The objective is to obtain this:

A + B = C     (R1)
C + D = E     (R2)


Following the mhchem manual this should be something like:

\newcommand\reaction[1]{$$\ce{#1}$$}
\newcounter{reaction}
\renewcommand{\thereaction}{R\,\arabic{reaction}}


which is similar to the solution given here Create New Type of Align Environment with Unique Counter

The command works(\reaction{A + B -> C}), but the counter does not. It looks like it keeps using the counter for the equation environment. What am I getting wrong?

• Maybe you should see in the mathtools documentation the way to customise equation labels, with the \newtagform and \usetagform commands. Sep 23 at 14:06
• Do you only have reactions in your document, or is it mixed in between other equations?
– Werner
Sep 24 at 0:26
• @Werner there are both chemical reactions and regular equations. yesterday

I'd state your problem counts of two parts. First, the counter, which is solved by adding to the preamble:

\newcounter{reactionCount}


Next, the command delimiting the reaction.

\newcommand{\reaction}[1]{
\refstepcounter{reactionCount} % Adds 1 to the counter
$\ce{#1} \tag{R\thereactionCount}$ % Using  for simplicity.
}


Here's a MWE:

\documentclass[preview, border=.2cm]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[version=4]{mhchem}

\newcounter{reactionCount} %Setting up the counter
\newcommand{\reaction}[1]{
\refstepcounter{reactionCount} % Adds 1 to the counter
$\ce{#1} \tag{R\thereactionCount}$ % Using  for simplicity.
}

\begin{document}
\reaction{A + B = C}
\reaction{C + D = E}
\end{document}


This is a quick solution, built upon what code you showed. There are smarter people in this forum who can code a better one, even with more features. If this is not what you were looking for, be sure to add more details to your question.

Extract from the mhchem Manual, from the passage following what you quoted in the question.

So far, so good. All reactions will be labelled exactly as all the equations. A few people asked for a different set of numbers for equations and reactions. One could use this code:

\makeatletter
\newcounter{reaction}
%%% >> for article <<
%\renewcommand\thereaction{C\,\arabic{reaction}}
%%% << for article <<
%%% >> for report and book >>
\renewcommand\thereaction{C\,\thechapter.\arabic{reaction}}
%%% << for report and book <<
\newcommand\reactiontag%
{\refstepcounter{reaction}\tag{\thereaction}}
\newcommand\reaction@[2][]%
{$$\ce{#2}% \ifx\@empty#1\@empty\else\label{#1}\fi% \reactiontag$$}
\newcommand\reaction@nonumber[1]%
{\begin{equation*}\ce{#1}\end{equation*}}
\newcommand\reaction%
{\@ifstar{\reaction@nonumber}{\reaction@}}
\makeatother


With that, all reactions will be labelled independently of the equations.

$$$$a+b$$$$
\reaction{CO2 + C}
\reaction*{CO2 + C}
\reaction[react:co]{CO2 + C}
$$$$a+b$$$$


You can replace equation with a different counter for reactions.

\documentclass{article}
\usepackage{amsmath}
\usepackage[version=4]{mhchem}

\makeatletter

\newcounter{reaction}
\renewcommand{\thereaction}{R\arabic{reaction}}
%\renewcommand{\thereaction}{R\thesection.\arabic{reaction}}

\newenvironment{reaction}
{%
\let\c@equation\c@reaction
\let\theequation\thereaction
$$% } {$$}

\begin{document}

$$a+b=c$$

\begin{reaction}
\ce{A + B -> C}
\end{reaction}

$$c+d=e$$

\begin{reaction}
\ce{C + D -> E}
\end{reaction}

\end{document}


The same where numbering is by section.

\documentclass{article}
\usepackage{amsmath}
\usepackage[version=4]{mhchem}

\makeatletter

\newcounter{reaction}
%\renewcommand{\thereaction}{R\arabic{reaction}}
\renewcommand{\thereaction}{R\thesection.\arabic{reaction}}

\newenvironment{reaction}
{%
\let\c@equation\c@reaction
\let\theequation\thereaction
$$% } {$$}

\begin{document}

\section{First}

$$a+b=c$$

\begin{reaction}
\ce{A + B -> C}
\end{reaction}

\section{Second}

$$c+d=e$$

\begin{reaction}
\ce{C + D -> E}
\end{reaction}

\end{document}