2

Related: Alignment of equals sign in multiple align environments

I have 4 equations that I manipulate. Each equation should be in their own subequation environment (that is, manipulation steps should not be receive a new equation number, but a), b), ... instead.

I'd like to be those 4 manipulations blocks aligned with each other. The related answer above achieves this by planting everything inside one big align environment. However, then I get consecutive numbers as a label (1, 2, 3, 4... and not 1.a 1.b ... 2.a 2.b .... )

Example (not aligned with each other):

\begin{subequations}
\begin{align}
R_1&=(a+b)^2 - 2ab\\
&=a^2+2ab + b^2 - 2ab\\
&=a^2+b^2
\end{align}
\end{subequations}

\begin{subequations}
\begin{align}
R_2&=(a+b)(a-b)\\
&=a^2 + ab - ab + b^2\\
&=a^2 + b^2
\end{align}
\end{subequations}

I tried to nest multiple subequations within an align environment, but that printed equation (verbatim in italics) instead of my actual formula. I also tried to throw everything into one subequations environment and manipulate the equation numbering with addtocounter but that only affected equations thereafter.

2 Answers 2

7

Here's a command \stepsubequation that should do what you want. I added also hyperref to show it also works in this case, but is not necessary for the job.

The \stepsubequation can receive as optional argument a label for referring to the following group of subequations, similar to adding \label{foo} after \begin{subequations}.

\documentclass{article}
\usepackage{amsmath}
\usepackage{hyperref}

\makeatletter
\newcommand{\stepsubequation}[1][]{%
  \ifmeasuring@
  \else
    \refstepcounter{parentequation}%
    \protected@xdef\theparentequation{\arabic{parentequation}}%
    \ifdefined\theHparentequation
      \protected@xdef\theHparentequation{\arabic{parentequation}}%
    \fi
    \setcounter{equation}{0}%
    \if\relax\detokenize{#1}\relax\else
      \edef\@currentlabel{\theparentequation}%
      \ltx@label{#1}%
    \fi
  \fi
}
\makeatother

\begin{document}

\begin{subequations}
\begin{align}
R_1&=(a+b)^2 - 2ab \label{a}\\
&=a^2+2ab + b^2 - 2ab \label{b}\\
&=a^2+b^2 \\
\stepsubequation[foo]
R_2&=(a+b)(a-b) \label{c}\\
&=a^2 + ab - ab + b^2 \\
&=a^2 + b^2
\end{align}
\end{subequations}

\begin{equation}
\text{this should have number 3}
\end{equation}

\eqref{a} -- \eqref{b} -- \eqref{c} -- \eqref{foo}

\end{document}

enter image description here

If you do operations such as \counterwithin or \numberwithin to equation, you can do as follows.

\documentclass{book}
\usepackage{amsmath}
\usepackage{xpatch}
\usepackage{hyperref}

\counterwithin{equation}{chapter}

\makeatletter
\AtBeginDocument{%
  \NewCommandCopy{\standardtheequation}{\theequation}%
  \xpatchcmd{\standardtheequation}{equation}{parentequation}{}{}%
}
\newcommand{\stepsubequation}[1][]{%
  \ifmeasuring@\else
    \refstepcounter{parentequation}%
    \protected@xdef\theparentequation{\standardtheequation}%
    \ifdefined\theHparentequation
      \protected@xdef\theHparentequation{\standardtheequation}%
    \fi
    \setcounter{equation}{0}%
    \if\relax\detokenize{#1}\relax\else
      \edef\@currentlabel{\theparentequation}%
      \ltx@label{#1}%
    \fi
  \fi
}
\makeatother

\begin{document}

\chapter{Experiment}

\begin{subequations}
\begin{align}
R_1&=(a+b)^2 - 2ab \label{a}\\
&=a^2+2ab + b^2 - 2ab \label{b}\\
&=a^2+b^2 \\
\stepsubequation[foo]
R_2&=(a+b)(a-b) \label{c}\\
&=a^2 + ab - ab + b^2 \\
&=a^2 + b^2
\end{align}
\end{subequations}

\begin{equation}
\text{this should have number 3}
\end{equation}

\eqref{a} -- \eqref{b} -- \eqref{c} -- \eqref{foo}

\end{document}

enter image description here

3
  • Thanks a lot! I just noticed that this doesn't work as expected when the equations are numbered with the current chapter number. So now it reads (3.1a) (3.2b) (3.3c) (5a) (5b) (5c) and the following equation has (3.5). Should I update this into the original question? Commented Nov 20, 2023 at 10:52
  • @infinitezero I added the workaround.
    – egreg
    Commented Nov 20, 2023 at 11:36
  • Thank you for the update. Unfortunately in my case it still doesn't work (stupid university layout). In any case, this is a good starting point for me and I will research further on how to get it done. Thanks again! Commented Nov 20, 2023 at 11:55
4

Alternatively to @egreg's answer, you could make size of both equations the same

\documentclass{report}
\usepackage{mathtools}
\usepackage{kantlipsum}

\counterwithin{equation}{chapter}


\begin{document}
\chapter{Chapter title}
\kant[1][1]

\begin{subequations}
\begin{align}
    R_1&=(a+b)^2 - 2ab\\
    &=a^2+2ab + b^2 - 2ab\\
    &=a^2+b^2
    \end{align}
\end{subequations}
\begin{subequations}
\begin{align}
    R_2&=
    \mathrlap{(a+b)(a-b)}           % Overwrites empty space
    \phantom{a^2+2ab + b^2 - 2ab}   % allocates empty space
    \\
    &=a^2 + ab - ab + b^2\\
    &=a^2 + b^2
\end{align}
\end{subequations}

\kant[1][2]
\end{document}

enter image description here

1
  • Thanks, that seems like a practical workaround. Commented Nov 20, 2023 at 17:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .