[Tex/LaTex] Nested subequations –> Following equations get wrong numbers

equationsnumbering

In a document, I use the subequations environment inside another subequations environment – to e.g. have equations numbered as (1a), (1b), (1ca), (1cb), (1d) etc. At an overall level, all the equations belong together, hence they all have number (1), and furthermore, at a nested level, the two (1c) equations belong together, hence (1ca) and (1cb). This all works fine, but the equations that then follow – which should be (2), (3) etc. – get different numbers. In the example below, the last equation outside all of the subequations should be number (2), but when I compile it it gets number (5).

I suppose there is some conflict causing the wrong numbering, but could anyone tell me how I obtain the correct numbering?

The code is as follows:

\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\begin{document}
\section{Section}
\begin{subequations}
These equations are numbered as subequations:
\begin{align}
F &= m a \\
p & = mv
\end{align}
These equations are also numbered as subequations, and they are numbered correctly:
% % %
\begin{subequations}
\begin{align}
F &= m a \\
p & = mv
\end{align}
\end{subequations}
\begin{subequations}
\begin{align}
F &= m a \\
p & = mv
\end{align}
\end{subequations}
\end{subequations}
But the next equation is then not numbered correctly; It should have number (2):
\begin{align}
F &= ma
\end{align}

\end{document}

Best Answer

You should define a subsubequations environment: the problem is that at every \begin{subequations} the "original" equation counter is stepped and its value is remembered to be restored at \end{subequations}. Since in your document there are three \begin{subequations}, the number of the next equation ends to be 5.

\documentclass[10pt,a4paper]{article}

\usepackage{amsmath}

\makeatletter
\newcounter{parentsubequation}% Counter for ``parent equation''.
\newenvironment{subsubequations}{%
  \refstepcounter{equation}%
  \protected@edef\theparentsubequation{\theequation}%
  \setcounter{parentsubequation}{\value{equation}}%
  \setcounter{equation}{0}%
  \def\theequation{\theparentsubequation\alph{equation}}%
  \ignorespaces
}{%
  \setcounter{equation}{\value{parentsubequation}}%
  \ignorespacesafterend
}
\makeatother

\begin{document}
\section{Section}
\begin{subequations}
These equations are numbered as subequations:
\begin{align}
F &= m a \\
p & = mv
\end{align}
These equations are also numbered as subequations, and they are numbered correctly:
% % %
\begin{subsubequations}
\begin{align}
F &= m a \\
p & = mv
\end{align}
\end{subsubequations}
\begin{subsubequations}
\begin{align}
F &= m a \\
p & = mv
\end{align}
\end{subsubequations}
\end{subequations}
But the next equation is then not numbered correctly; It should have number (2):
\begin{align}
F &= ma
\end{align}

\end{document}