# Interrupting and resuming subequations

As the title states I would like to interrupt and resume a block of equations that is inside a subequations regime. My problem arises when the interrupting stuff shall contain equations on its own:

\documentclass{article}
\usepackage{amsmath}

\begin{document}
\begin{subequations}\label{eq:master}%
\begin{align}%
a & = b \label{eq:master:ab}\\
c & = d \label{eq:master:cd}
\end{align}%
\end{subequations}%
%
Some stuff in between
$$% A = B\label{eq:helper}$$
%
Resume here the subequations, i.e., continue with 1c:
\begin{subequations}%
\begin{align}%
e & = f \label{eq:master:ef}\\
g & = h \label{eq:master:gh}
\end{align}%
\end{subequations}%
Master~\eqref{eq:master}, consisting of~\eqref{eq:master:ab}, \eqref{eq:master:cd}, \eqref{eq:master:ef}, \eqref{eq:master:gh},
and helper~\eqref{eq:helper}.
\end{document}

I know how to manually fiddle with the counters, but that is not The LaTeX Way. I have seen something like that for enumerations utilizing the enumitem package. How would an elegant solution look like, that let me write

\begin{subequations}[resume]%
\begin{align}%
e & = f \label{eq:master:ef}\\
g & = h \label{eq:master:gh}
\end{align}%
\end{subequations}%
-
Do you want the "stuff in between" without a label? If not, what label do you want for it? –  Steven B. Segletes Mar 5 at 14:08
@StevenB.Segletes As in the MWE: it should be labeled (2). –  mafp Mar 5 at 14:32

Here is another method.

I explain: the subequations now admit an optional argument. The expected values are resume, intermezzo, or nothing. As soon as neither resume nor intermezzo is encountered, a new potential thread is initialized: continuation goes with resume, putting stuff in-between can be anything and if it is again a subequations block it has to be flagged intermezzo.

The code takes into account that hyperref patches subequations.

One caveat: subequations block playing in the rôle of intermezzi can not be globally labeled: only the actual sub-equations may be.

\documentclass{article}
\usepackage{amsmath}

% original amsmath definition
% \subequations:
% \long macro:->\refstepcounter {equation}\protected@edef \theparentequation {\theequation }\setcounter {parentequation}{\value {equation}}\setcounter {equation}{0}\def \theequation {\theparentequation \alph {equation}}\ignorespaces

% saved by hyperref in
% > \HyOrg@subequations=\long macro:

% hyperref-patched \subequations: (\endsubequations not modified)
% > \subequations=macro:
% ->\stepcounter {equation}\protected@edef \theHparentequation {\@ifundefined {th
% uations \def \theHequation {\theHparentequation \alph {equation}}\ignorespaces
% .

% extending the environment
%   1. with optional parameter: expected values resume or intermezzo or none.
%   2. while keeping the hyperref customization.

\makeatletter
\def\user@resume{resume}
\def\user@intermezzo{intermezzo}
%
\newcounter{previousequation}
\newcounter{lastsubequation}
\newcounter{savedparentequation}
\setcounter{savedparentequation}{1}
%
\renewenvironment{subequations}[1][]{%
\def\user@decides{#1}%
\setcounter{previousequation}{\value{equation}}%
\ifx\user@decides\user@resume
\setcounter{equation}{\value{savedparentequation}}%
\else
\ifx\user@decides\user@intermezzo
\refstepcounter{equation}%
\else
\setcounter{lastsubequation}{0}%
\refstepcounter{equation}%
\fi\fi
\protected@edef\theHparentequation{%
\@ifundefined {theHequation}\theequation \theHequation}%
\protected@edef\theparentequation{\theequation}%
\setcounter{parentequation}{\value{equation}}%
\ifx\user@decides\user@resume
\setcounter{equation}{\value{lastsubequation}}%
\else
\setcounter{equation}{0}%
\fi
\def\theequation  {\theparentequation  \alph{equation}}%
\def\theHequation {\theHparentequation \alph{equation}}%
\ignorespaces
}{%
%  \arabic{equation};\arabic{savedparentequation};\arabic{lastsubequation}
\ifx\user@decides\user@resume
\setcounter{lastsubequation}{\value{equation}}%
\setcounter{equation}{\value{previousequation}}%
\else
\ifx\user@decides\user@intermezzo
\setcounter{equation}{\value{parentequation}}%
\else
\setcounter{lastsubequation}{\value{equation}}%
\setcounter{savedparentequation}{\value{parentequation}}%
\setcounter{equation}{\value{parentequation}}%
\fi\fi
%  \arabic{equation};\arabic{savedparentequation};\arabic{lastsubequation}
\ignorespacesafterend
}
\makeatother

\begin{document}\thispagestyle{empty}

\begin{subequations}\label{eq:master}
\begin{align}
a & = b \label{eq:master:ab}\\
c & = d \label{eq:master:cd}
\end{align}
\end{subequations}
%
Some intermezzo stuff in between
$$A = B\label{eq:helper}$$
%
\begin{subequations}[intermezzo]
\begin{align}
C &= D\label{eq:inter1}\\
E &= F\label{eq:inter2}
\end{align}
\end{subequations}
Resume here the subequations, i.e., continue with 1c:
\begin{subequations}[resume]
%  \label{eq:Master} % NO! impossible here
\begin{align}
e & = f \label{eq:master:ef}\\
g & = h \label{eq:master:gh}\\
g & = h \label{eq:master:gh2}
\end{align}
\end{subequations}
Master~\eqref{eq:master}, consisting of~\eqref{eq:master:ab},
\eqref{eq:master:cd} and \eqref{eq:master:ef}, \eqref{eq:master:gh},
\eqref{eq:master:gh2}
and helpers \eqref{eq:helper}, \eqref{eq:inter1} and \eqref{eq:inter2}. But one
can not label the intermezzo block, only its subequations.

Some more continued stuff:
\begin{subequations}[resume]
\begin{align}
i & = j \label{eq:master:ij}\\
k & = l \label{eq:master:kl}\\
i & = j \label{eq:master:ij2}
\end{align}
\end{subequations}
and we re-initialize again (resuming will continue from here):
\begin{subequations}
\begin{align}
A &= B\label{eq:final1}\\
B &= A\label{eq:final2}
\end{align}
\end{subequations}
Two intermezzi (my head starts spinning!):
\begin{subequations}[intermezzo]
\label{eq:Intermediate}
\begin{align}
A &= B\label{eq:final3}\\
B &= A\label{eq:final4}
\end{align}
\end{subequations}
\begin{align}
A &= B\label{eq:final5}\\
B &= A\label{eq:final6}
\end{align}
And we resume again (some aspirin please!)
\begin{subequations}[resume]
\begin{align}
A &= B\label{eq:final7}\\
B &= A\label{eq:final8}
\end{align}
\end{subequations}

So we had a new block of subequations \eqref{eq:final1}, \eqref{eq:final2},
\eqref{eq:final7}, and \eqref{eq:final8}; and in-between there were
\eqref{eq:final3}, \eqref{eq:final4}, \eqref{eq:final5}, and \eqref{eq:final6}.

\end{document}

-

My solution provides a second environment subequations* that picks up the values from the last subequations or subequations* environment.
The \end{subequations} part is redefined to save the values of the counters (in this case 1 and 2 = b) in auxiliary counters. These are used in the subequations* environment.

Note: It is the possible to label the subequations* environments which means that there will be more than one “Master” equation. This is gets interesting when you use a \pageref.

The reason for etoolbox\patchcmd macor can be read in another answer of mine. Short version: The \theparentequation macro is defined independently from the parentequation counter.

## Code

\documentclass{article}
\usepackage{amsmath}
\usepackage{etoolbox}

\makeatletter
\newcounter{qrr@oldeq}
\newcounter{qrr@oldsubeq}
\newcounter{qrr@realeq}
\renewenvironment{subequations}{%
\refstepcounter{equation}%
\protected@edef\theparentequation{\theequation}%
\setcounter{parentequation}{\value{equation}}%
\setcounter{equation}{0}%
\def\theequation{\theparentequation\alph{equation}}%
\ignorespaces
}{%
\setcounter{qrr@oldeq}{\value{parentequation}}%
\setcounter{qrr@oldsubeq}{\value{equation}}%
\setcounter{equation}{\value{parentequation}}%
\ignorespacesafterend
}
\newenvironment{subequations*}{%
\setcounter{qrr@realeq}{\value{equation}}%
\let\theparentequation\theequation%
\patchcmd{\theparentequation}{equation}{parentequation}{}{}%
\setcounter{parentequation}{\numexpr\value{qrr@oldeq}-1}%
\setcounter{equation}{\value{qrr@oldsubeq}}%
\def\theequation{\theparentequation\alph{equation}}%
\refstepcounter{parentequation}%
\ignorespaces
}{%
\setcounter{qrr@oldeq}{\value{parentequation}}%
\setcounter{qrr@oldsubeq}{\value{equation}}%
\setcounter{equation}{\value{qrr@realeq}}%
\ignorespacesafterend
}
\makeatother
\begin{document}
\begin{subequations}\label{eq:master}%
\begin{align}%
a & = b \label{eq:master:ab}\\
c & = d \label{eq:master:cd}
\end{align}%
\end{subequations}%
%
Some stuff in between
$$% A = B\label{eq:helper}$$
%
Resume here the subequations, i.e., continue with 1c:
\begin{subequations*}\label{eq:Master}
\begin{align}%
e & = f \label{eq:master:ef}\\
g & = h \label{eq:master:gh}
\end{align}%
\end{subequations*}%
Master~\eqref{eq:master}, consisting of~\eqref{eq:master:ab}, \eqref{eq:master:cd} and \eqref{eq:master:ef}, \eqref{eq:master:gh} from Master \eqref{eq:Master},
and helper~\eqref{eq:helper} and final~\eqref{eq:final}.

Some stuff at the end:
$$% A = B\label{eq:final}$$
\end{document}

## Output

-
Good solution, but I find it very confusing to the reader. –  egreg Mar 5 at 14:35
I agree on both counts that solution is excellent but that presentation is confusing to reader. It would be less so if the stuff in between were unlabeled, but then it could not be referenced. –  Steven B. Segletes Mar 5 at 18:00
@egreg But consider the possibility that the subequations carry a special main tag, and it won't be confusing. –  mafp Mar 6 at 16:15