3

I'm using the chemmacros package, its reactions environment, and the article document class:

\documentclass[a4paper, 12pt]{article}
\usepackage{amsmath, amsthm, amssymb, mathtools}
\usepackage{upgreek, chemfig, chemmacros}

\chemsetup{modules = all}
\chemsetup[reactions]{ before-tag = \thesection. } % Adds section number to label
\makeatletter
\@addtoreset{reaction}{section}
\makeatother

\begin{document}

\section{Galvanic cell}
\begin{reactions}
  Zn &-> Zn\pch[2] + 2 e\mch \label{eq:galvZnR} \\
  Cu\pch[2] + 2 e\mch &-> Cu \label{eq:galvCuR}
\end{reactions}

As seen in~\ref{eq:galvZnR} and~\ref{eq:galvCuR}...

\end{document}

Now I want to change the reactions label numbering to include the section number. From the chemmacros manual I found the way using the reaction/before-tag.

But the reference to the label does not include the section number. How do I fix this? I tried \numberwithin{equation}{section} since reactions only is a wrapper to align (?) but it does not work.

My next guess would be entirely redefining the reactions environment as seeen in https://tex.stackexchange.com/a/267804 but that seems like a rather dirty way.

Thanks for the help!

1 Answer 1

3

I suggest you replace

\chemsetup[reactions]{ before-tag = \thesection. } % Adds section number to label
\makeatletter
\@addtoreset{reaction}{section}
\makeatother

with either

\numberwithin{reaction}{section}

or

\counterwithin{reaction}{section}

The former requires loading the amsmath package (which your document does); the latter works even if amsmath isn't loaded.


A full MWE (minimum working example):

enter image description here

\documentclass[a4paper, 12pt]{article}
\usepackage{amsthm, amssymb, mathtools, upgreek}
\usepackage{chemfig, chemmacros}
\chemsetup{modules = all}

\numberwithin{reaction}{section}

%% Optional:
\usepackage{cleveref}
\crefname{reaction}{reaction}{reactions}

\begin{document}
\setcounter{section}{1}
\begin{reactions}
  Zn &-> Zn\pch[2] + 2 e\mch \label{eq:galvZnR} \\
  Cu\pch[2] + 2 e\mch &-> Cu \label{eq:galvCuR}
\end{reactions}

As seen in~\ref{eq:galvZnR} and~\ref{eq:galvCuR}, \dots

As seen in \cref{eq:galvZnR,eq:galvCuR}, \dots
\end{document}
4
  • 2
    \counterwithin{reaction}{section} works even without amsmath.
    – egreg
    Commented Nov 6, 2019 at 17:17
  • @egreg - Thanks for this. I've updated the answer to incorporate your suggestion.
    – Mico
    Commented Nov 6, 2019 at 17:23
  • 1
    BTW: chemmacros loads mathtools (and thus amsmath) anyway :)
    – cgnieder
    Commented Nov 6, 2019 at 17:27
  • @clemens thanks for the info. the import is a leftover from the preamble i use in every tex file where i import basic info, but i might omit it Commented Nov 7, 2019 at 9:09

You must log in to answer this question.

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