3

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]{\begin{equation}\ce{#1}\end{equation}}
\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?

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

3 Answers 3

1

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}

Numbered Reactions

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.

2

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}}
\@addtoreset{reaction}{chapter}
%%% << for report and book <<
\newcommand\reactiontag%
  {\refstepcounter{reaction}\tag{\thereaction}}
\newcommand\reaction@[2][]%
  {\begin{equation}\ce{#2}%
  \ifx\@empty#1\@empty\else\label{#1}\fi%
  \reactiontag\end{equation}}
\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.

\begin{equation}a+b\end{equation}
\reaction{CO2 + C}
\reaction*{CO2 + C}
\reaction[react:co]{CO2 + C}
\begin{equation}a+b\end{equation}

enter image description here

1

You can replace equation with a different counter for reactions.

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

\makeatletter

\newcounter{reaction}
%\counterwithin*{reaction}{section}% adjust to suit
\renewcommand{\thereaction}{R\arabic{reaction}}
%\renewcommand{\thereaction}{R\thesection.\arabic{reaction}}

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

\begin{document}

\begin{equation}
a+b=c
\end{equation}

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

\begin{equation}
c+d=e
\end{equation}

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

\end{document}

enter image description here

The same where numbering is by section.

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

\makeatletter

\newcounter{reaction}
\counterwithin*{reaction}{section}% adjust to suit
%\renewcommand{\thereaction}{R\arabic{reaction}}
\renewcommand{\thereaction}{R\thesection.\arabic{reaction}}

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

\begin{document}

\section{First}

\begin{equation}
a+b=c
\end{equation}

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

\section{Second}

\begin{equation}
c+d=e
\end{equation}

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

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