# Numbering groups of subequations alternatingly throughout a longer section of a document

I want to have two different groups of subequations that go through a longer section of my document alternatingly with text in between them. One group is labeled with 1.1, 1.2, ..., the other with 2.1, 2.2, ... and so forth.

I achieved this by treating them all as independent equations with equation* and using \tag to number the equations manually.

However, this is a very tedious and error-prone process and there must be a better way to achieve the same goal.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[]{geometry}
\usepackage{amsmath}

\begin{document}

\begin{equation*}
\textrm{I am a subequation of group one} \tag{1.1}
\end{equation*}

Some random text.

\begin{equation*}
\textrm{I am a subequation of group two} \tag{2.1}
\end{equation*}

Some random text.

\begin{equation*}
\textrm{I am a subequation of group one} \tag{1.2}
\end{equation*}

Some random text.

\begin{equation*}
\textrm{I am a subequation of group two} \tag{2.2}
\end{equation*}

Some random text.

\begin{equation*}
\textrm{I am a subequation of group one} \tag{1.3}
\end{equation*}

Some random text.

\begin{equation*}
\textrm{I am a subequation of group two} \tag{2.3}
\end{equation*}

\end{document}


• What does the prefix X in your numbering X.Y represent? I assume Y represents just the next equation number (which is incremented automatically. – Werner Mar 21 at 0:49
• I've edited the question and tried to make it more clear. Basically, I want the equations to be labeled automatically, but there should be two groups of equations that are differentiated, so one group's subequations are labeled with the prefix 1, the other group's subequations with the prefix 2. So when the counter for one group goes up, the counter for the other group does not. – jona173 Mar 21 at 0:54
• I just provided a working example and tried to make the focus more clear. – jona173 Mar 21 at 1:07

The following setup automates what you're after in a similar way the subequations does. You wrap which equation set you want to have a unique representation using the equationseries environment, and also specify an accompanying series as the mandatory argument. If the series hasn't been defined previously, it's define automatically. If it already exists, the historic values are extracted so that the series can continue as before. It can also be interspersed with other equation which should default to the usual equation formatting.

\documentclass{article}

\usepackage{amsmath}

\makeatletter
\NewDocumentEnvironment{equationseries}{ m }{%
% Check whether the equation series has been used before
\ifcsname c@eqnseries#1\endcsname
% Series has been used before
\expandafter\protected@edef\expandafter\eqnprefix\expandafter{\csname theeqnseries#1\endcsname}% Extract series prefix
\setcounter{parentequation}{\value{equation}}% Store current equation counter
\setcounter{equation}{\value{eqnserieseqn#1}}% Set equation counter
\else
% First time using this series
\refstepcounter{equation}% Step equation counter
\setcounter{parentequation}{\value{equation}}% Store current equation counter
\protected@edef\eqnprefix{\theequation}% Define series prefix
\newcounter{eqnseries#1}\setcounter{eqnseries#1}{\value{equation}}% Define series counter
\newcounter{eqnserieseqn#1}% Define series equation counter
\setcounter{equation}{0}% Reset equation counter
\fi
%
\def\theequation{\eqnprefix.\arabic{equation}}% Define series equation number representation
\ignorespaces
}{%
\setcounter{eqnserieseqn#1}{\value{equation}}% Store equation series counter
\setcounter{equation}{\value{parentequation}}% Restore equation counter
\ignorespacesafterend
}
\makeatother

\begin{document}

\section{Equations}

A regular equation:
$$f(x) = ax^2 + bx + c$$

An \verb|align|ed set of equations:
\begin{align}
f(x) &= ax^2 + bx + c \\
ax^2 + bx + c &= g(x)
\end{align}

\section{Subequations}

\begin{subequations}
A regular equation:
$$f(x) = ax^2 + bx + c$$

An \verb|align|ed set of equations:
\begin{align}
f(x) &= ax^2 + bx + c \\
ax^2 + bx + c &= g(x)
\end{align}
\end{subequations}

\clearpage

\section{Equation series}

A regular equation set as part of \verb|first|:
\begin{equationseries}{first}
$$f(x) = ax^2 + bx + c$$

Some text between the equations and then
$$g(x) = ax^2 + bx + c$$
\end{equationseries}

Then we start a new equation series called \verb|second|:
\begin{equationseries}{second}
$$f(x) = ax^2 + bx + c$$

Some more text between the equations and then
$$g(x) = ax^2 + bx + c$$
\end{equationseries}

A regular equation inserted between:
$$f(x) = ax^2 + bx + c$$

Back to the \verb|first| equation series:
\begin{equationseries}{first}
$$f(x) = ax^2 + bx + c$$
\end{equationseries}

Back to the \verb|second| equation series:
\begin{equationseries}{second}
\begin{align}
f(x) &= ax^2 + bx + c \\
ax^2 + bx + c &= g(x)
\end{align}
\end{equationseries}

Then, a normal set of equations:
\begin{align}
f(x) &= ax^2 + bx + c \\
ax^2 + bx + c &= g(x)
\end{align}

\end{document}

• Thank you so much! This is more than I could ever ask for! Unfortunately, I am getting multiple errors. "Undefined control sequence" in line 7, "Illegal parameter number in definition of \reserved@a." in line 19, "Illegal parameter number in definition of \cl@@ckpt." in line 20 and "LaTeX Error: Environment equationseries undefined." followed by "LaTeX Error: \begin{document} ended by \end{equationseries}." every time a new equationseries has begun and ended. Is there a certain package I'm missing or is it maybe due to the fact that I am using overleaf? – jona173 Mar 21 at 2:37
• I just figured out I was simply missing xparse. This code is seriously amazing and will be used a lot from now on. Could the same code be used not to change the prefix of an equation series, but the suffix? For example, one series of subequations is numbered as 2.a, 2.b, 2.c, ... and another as 2.1, 2.2, 2.3, ... and so forth? – jona173 Mar 21 at 10:30
• @jona173: If you need to include xparse, then you might not have an up-to-date LaTeX, as the xparse definitions were incorporated into the kernel in Oct 2020. Inside the equationseries environment, you can add \renewcommand{\theequation}{\eqnprefix.\alph{equation}} to have a 2.a, 2.b, ... numbering. The default is 2.1, 2.2, ... – Werner Mar 21 at 17:22

A variant of Werner's solution where you can decide the format of the subequation numbers at first usage of a series.

\documentclass{article}
\usepackage{amsmath}

%\usepackage{xparse} % not needed with LaTeX 2020-10-01 or later

\newcounter{savedequation}

\ExplSyntaxOn

\prop_new:N \g_jona_eqseries_prop

\NewDocumentEnvironment{eqseries}{mO{.\arabic{##1}}}
{
\prop_if_in:NnF \g_jona_eqseries_prop { #1 }
{
% step the equation number if new series
\refstepcounter{equation}
% store zero as starting point if new series
\prop_gput:Nnn \g_jona_eqseries_prop { #1 } { 0 }
% store the parent number if new series
\prop_gput:Nnx \g_jona_eqseries_prop { #1-parent } { \theequation }
% define the representation if new series
\cs_gset:cn { __jona_eqseries_repr_#1_:n } { #2 }
}
\setcounter{savedequation}{\value{equation}}
% set the parent number as found in the property list
\setcounter{parentequation}{\prop_item:Nn \g_jona_eqseries_prop { #1-parent }}
% set the starting number in the series as the last one used
\setcounter{equation}{ \prop_item:Nn \g_jona_eqseries_prop { #1 } }
% set the representation
\cs_set:Npx \theequation
{
\exp_not:N \theparentequation \exp_not:c {__jona_eqseries_repr_#1_:n} { equation }
}
\ignorespaces
}
{
% save the last subequation number
\prop_gput:Nnx \g_jona_eqseries_prop { #1 } { \int_eval:n { \value{equation} } }
% reset to default
\setcounter{equation}{\value{savedequation}}
\ignorespacesafterend
}

\ExplSyntaxOff

\begin{document}

\section{Equations}

A regular equation:
$$f(x) = ax^2 + bx + c$$

An \verb|align|ed set of equations:
\begin{align}
f(x) &= ax^2 + bx + c \\
g(x) &= \exp x + \log x - \sin x
\end{align}

\section{Subequations}

\begin{subequations}
A regular equation:
$$f(x) = ax^2 + bx + c$$

An \verb|align|ed set of equations:
\begin{align}
f(x) &= ax^2 + bx + c \\
g(x) &= \exp x + \log x - \sin x
\end{align}
\end{subequations}

\clearpage

\section{Equation series}

A regular equation set as part of \verb|first|:
\begin{eqseries}{first}
$$f(x) = ax^2 + bx + c$$

Some text between the equations and then
$$g(x) = \exp x + \log x - \sin x$$
\end{eqseries}

Then we start a new equation series called \verb|second|:
\begin{eqseries}{second}[--\alph{#1}]
$$f(x) = ax^2 + bx + c$$

Some more text between the equations and then
$$g(x) = \exp x + \log x - \sin x$$
\end{eqseries}

A regular equation inserted between:
$$f(x) = ax^2 + bx + c$$

Back to the \verb|first| equation series:
\begin{eqseries}{first}
$$f(x) = ax^2 + bx + c$$
\end{eqseries}

Back to the \verb|second| equation series:
\begin{eqseries}{second}
\begin{align}
f(x) &= ax^2 + bx + c \\
g(x) &= \exp x + \log x - \sin x
\end{align}
\end{eqseries}

Then, a normal set of equations:
\begin{align}
f(x) &= ax^2 + bx + c \\
g(x) &= \exp x + \log x - \sin x
\end{align}

\end{document}