# Interlace Subequations Environments

I wish to use the equations environment to obtain the following output:

Some text...
a &= b  \\   (A.1a)
c &= d       (A.2a)
and some more text...
a' &= b' \\   (A.1b)
c' &= d'      (A.2b)


I want to be able to tag (A.1) as \label{eq1} and (A.2) as \label{eq2}; that is, I want \eqref{eq1} to produce '(A.1)' and \eqref{eq2} to produce '(A.2)'. I am using amsmath.

If these equations were in a different order -- namely, if the middle two were swapped -- then this would be easy. I am struggling, however, to do it with the equations in the order above...

I can make these equations individually reference-able, by using things like \addtocounter, \refstepcounter and \tag, but only in a way that \label{eq1a} produces '(A.1a)'; I cannot do the 'combined' (A.1).

• Are the equations in a single align or are they in separate equation environments? – egreg Sep 21 '18 at 16:02
• They are in separate align environments. I've updated the question, hopefully in a clear way! – Sam T Sep 21 '18 at 17:55

In this solution, the mixedsubequations environment can have as optional argument the labels for the various equations; let me call it main. If no optional argument is specified, the environment is subordinate.

In the main environment, the subequations counter is reset to 1, in order to produce “a”, in the subordinate environments it is stepped.

Note that this works only if the main environment and the subordinate ones all have the same number of equations (not necessarily in a single align environment each). Note also that the number of labels in the optional argument to the main environment is used to determine the number of equations.

\documentclass{book}
\usepackage{amsmath}
\usepackage{xparse}

\newcounter{mixedsubequations}
\renewcommand{\themixedsubequations}{\alph{mixedsubequations}}

\ExplSyntaxOn

\int_new:N \g_samt_mixedsubeq_int

\NewDocumentEnvironment{mixedsubequations}{o}
{
\IfNoValueTF { #1 }
{
\stepcounter{mixedsubequations}
}
{
\int_gset:Nn \g_samt_mixedsubeq_int { \clist_count:n { #1 } }
\clist_map_inline:nn { #1 }
{
\refstepcounter{equation}\label{##1}
}
\setcounter{mixedsubequations}{1}
}
\domixedsubequations
}
{\ignorespacesafterend}

\NewDocumentCommand{\domixedsubequations}{}
{
\cs_set:Npx \theequation
{
\exp_not:o { \theequation }
\exp_not:n { \alph{mixedsubequations} }
}
\ignorespaces
}
\ExplSyntaxOff

\begin{document}

\appendix
\chapter{Title}

Some text
\begin{mixedsubequations}[eq1,eq2]
\begin{align}
a  &= b  \label{eq1a}\\
c  &= d  \label{eq2a}
\end{align}
\end{mixedsubequations}
Some more text
\begin{mixedsubequations}
\begin{align}
a' &= b' \label{eq1b}\\
c' &= d' \label{eq2b}
\end{align}
\end{mixedsubequations}

\eqref{eq1} \eqref{eq1a} \eqref{eq1b}

\eqref{eq2} \eqref{eq2a} \eqref{eq2b}

\end{document}


• Wow, thank you very much! I shall implement that tomorrow; assuming there are no mishaps, I'll accept the answer (already +1-d it!) – Sam T Sep 21 '18 at 22:01
• You are a latex machine! Works all fine. (I realised that 'tomorrow' was the weekend, and I didn't fancy working on the weekend. =P) My only comment, really, is that when I call \eqref{eq1}, it says that the label is missing; however, the label is printed as desired, so it's only very minor. Thanks so much! :) – Sam T Sep 24 '18 at 10:24
• @SamT Doesn't happen to me. – egreg Sep 24 '18 at 10:42
• That is strange. See [i.stack.imgur.com/3EHd7.png]. As I said, though, not really an issue. Thanks again for the code :) – Sam T Sep 24 '18 at 13:41
• You can fool TeXStudio in believing the labels have been defined by adding \catcode\^^A=14 (which makes the combination ^^A into an alternate comment character) in the document preamble and inputting \begin{mixedsubequations}[eq1,eq2]^^A\label{eq1}\label{eq2}` – egreg Sep 24 '18 at 13:54