[Tex/LaTex] Nesting subequations within align

alignamsmathsubequations

subequations seems to only work outside environments such as align and equation. What if I want to preserve the alignment of the equal signs using align while also would like subequations numbering within, such as:

\begin{align}
   \begin{subequations}
    A &= B+1 \\
    B+1 &= C
   \end{subequations}
\intertext{therefore}
   \begin{subequations}
    A &= C \\
    B &= C-1
   \end{subequations}
\end{align}

Which generates multitude of errors 🙁

Best Answer

The subequations environment & the parentequation counter

The original subequations environment redefines \theparentequation in a way that it is independent from the counter parentequation:

\protected@edef\theparentequation{\theequation}%

But we can redefine the subequations environment so that only the value of equation is saved and \theparentequation uses the counter parentequation again:

\def\theparentequation{\arabic{parentequation}}%

But that way we would loose the setting of \theequation that might involve \thechapter in classes like book. For this, we could change it again manually to

\def\theparentequation{\thechapter.\arabic{parentequation}}%

etoolbox and \patchcmd

Another approach uses etoolbox and its \patchcmd macro.

First we let \theparentequation be the same as \theequation:

\let\theparentequation\theequation

After that we look for every ocurrence of equation and change it to parentequation:

\patchcmd{\theparentequation}{equation}{parentequation}{}{}

At this point we don't need the \definition of \theparentequation inside the subequations environment anymore:

\usepackage{etoolbox}

% let \theparentequation use the same definition as equation
\let\theparentequation\theequation
% change every occurence of "equation" to "parentequation"
\patchcmd{\theparentequation}{equation}{parentequation}{}{}

\renewenvironment{subequations}{%
  \refstepcounter{equation}%
%  \def\theparentequation{\arabic{parentequation}}% we patched it already :)
  \setcounter{parentequation}{\value{equation}}%
  \setcounter{equation}{0}%
  \def\theequation{\theparentequation\alph{equation}}%
  \ignorespaces
}{%
  \setcounter{equation}{\value{parentequation}}%
  \ignorespacesafterend
}

\nextParentEquation

Let me also define the command \nextParentEquation that increments parentequation and resets equation so that we don't have to do this manually.

\newcommand*{\nextParentEquation}{%
  \stepcounter{parentequation}\setcounter{equation}{0}%
}

Workaround for parentequation labels (by OP Mobius Pizza)

While \label directly after \begin{subequations} works for referencing the first parent equation, to enable it to work after \nextParentEquation it is necessary to define a alias command \parentlabel for example, to evade sanitation performed by amsmath:

\let\parentlabel\label

Addition by Qrrbrbirlbel

I didn't think of labeling the parentequations, but with OP's addition (see above), I would even go a step further and redefine the subequations environment and the \nextParentEquation so that they take on optional argment, the label's name.

Code

This MWE uses report with one \chapter to demonstrate that \thechapter. doesn't get lost while redefining the subequations environment, this also works with article, of course.

\documentclass{report}
\usepackage{amsmath}
\usepackage{etoolbox}

% let \theparentequation use the same definition as equation
\let\theparentequation\theequation
% change every occurence of "equation" to "parentequation"
\patchcmd{\theparentequation}{equation}{parentequation}{}{}

\renewenvironment{subequations}[1][]{%              optional argument: label-name for (first) parent equation
  \refstepcounter{equation}%
%  \def\theparentequation{\arabic{parentequation}}% we patched it already :)
  \setcounter{parentequation}{\value{equation}}%    parentequation = equation
  \setcounter{equation}{0}%                         (sub)equation  = 0
  \def\theequation{\theparentequation\alph{equation}}% 
  \let\parentlabel\label%                           Evade sanitation performed by amsmath
  \ifx\\#1\\\relax\else\label{#1}\fi%               #1 given: \label{#1}, otherwise: nothing
  \ignorespaces
}{%
  \setcounter{equation}{\value{parentequation}}%    equation = subequation
  \ignorespacesafterend
}

\newcommand*{\nextParentEquation}[1][]{%            optional argument: label-name for (first) parent equation
  \refstepcounter{parentequation}%                  parentequation++
  \setcounter{equation}{0}%                         equation = 0
  \ifx\\#1\\\relax\else\parentlabel{#1}\fi%         #1 given: \label{#1}, otherwise: nothing
}

\begin{document}
\chapter{Test}
\begin{equation}
  0 \neq 1
\end{equation}
\begin{subequations}[eq:2]% or: \label{eq:2}
\begin{align}%              or: \parentlabel{eq:2}
    A & = B+1 \label{eq:2a} \\
  B+1 & = C   \label{eq:2b}
\intertext{therefore}\nextParentEquation[eq:3]% or: \nextParentEquation\parentlabel{eq:3}
    A & = C   \label{eq:3a} \\
    B & = C-1 \label{eq:3b}
\end{align}
\end{subequations}
\eqref{eq:2}: \eqref{eq:2a}, \eqref{eq:2b} \\ \eqref{eq:3}: \eqref{eq:3a}, \eqref{eq:3b}
\begin{equation}
  1 \neq 2
\end{equation}
\end{document}

Output

Output