# Nesting subequations within align

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 :(

-

# 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


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}
$$0 \neq 1$$
\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}
$$1 \neq 2$$
\end{document}


## Output

-
@MobiusPizza I've updated my answer with, hopefully, a “more elegant solution”. –  Qrrbrbirlbel Nov 2 '12 at 17:44
Truly ingenious, wouldn't have been able to work that out myself, and great foresight on the possible issue with chapter counter. Many thanks! –  Mobius Pizza Nov 2 '12 at 21:13
I have improved the answer a bit by allowing labeling to work with the parent level, i.e. parentequation –  Mobius Pizza Nov 5 '12 at 10:37
@MobiusPizza Very good addition, I didn't even think of labeling the parent equation. I've updated my answer again with a solution I would use (the optional arguments), but YMMW. –  Qrrbrbirlbel Nov 5 '12 at 20:23
Suggestion for your MWE: You can actually do everything inside one align environment if, for the “bare” equations that aren't subequations, you put \tag{\theparentequation} before the \\. This way you're guaranteed that they'll all line up, whether or not each one is actually meant to be a subequation. –  Luke Maurer Jun 30 '13 at 17:33

As you mentioned, use subequations outside the align environment and use some box manipulation to align the equations horizontally:

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\begin{document}

\begin{subequations}
\begin{align}
A &= B+1 \\
B+1 &= C
\end{align}
\end{subequations}
\vspace*{-\belowdisplayshortskip}
\noindent therefore
\vspace*{-\abovedisplayshortskip}
\begin{subequations}
\begin{align}
\phantom{B+1}\llap{$A$} &= C \\
B &= C-1
\end{align}
\end{subequations}

\end{document}


The maximum length of the LHS of the top set of sub-equations is longer/wider than that of the bottom. So, we insert a \phantom{<longest LHS>}\llap{$<LHS>$} into the second set of sub-equations to make up for this horizontal difference in length.

The additional vertical adjustments make the representation more compact with the break of the short therefore text.

-
Thanks, alas I was looking for simple commands like \addtocounter{parentequation}{1} instead of alignment hacks like this, but that doesn't seem to work. I am guessing that is impossible to do. I will leave the question open if there are more elegant solutions. IIRC mathtools also offers \shortintertext for more compact vertical spacing for \intertext –  Mobius Pizza Nov 2 '12 at 17:11
@MobiusPizza: I fully agree. amsmath's align scans its contents more than once making some seemingly-easy modification hard to get around. –  Werner Nov 2 '12 at 17:13