# Customize subequation numbering

I need to customize equation numbering as follows:

Some text ...

a+b  (5)
3+2  (5')

... some more text....

c+d  (6)


The idea is that (5') is an example of (5). I thought of using subequations, but I am not sure how to customize the inner numbering, as it would generate 5a and 5b, or 5.i, 5.ii, etc.... If I use \tag{} instead, I am not sure how to get the previous equation number (5, in the example).

You can define an \eqorn command. If you use \eqorn[''] you get two primes; any math symbol can be specified in the optional argument.

\documentclass{article}
\usepackage{amsmath}

\newcommand{\eqorn}[1][']{\tag{\theequation\ensuremath{#1}}}

\begin{document}

Some text ...
\begin{gather}% or align or whatnot
a+b \label{main} \\
3+2 \eqorn \label{secondary}
\end{gather}
... some more text....
$$c+d$$
with references to \eqref{main} and \eqref{secondary}.

\end{document}


• This is a very simple solution with which - as you say - any formula can be specified in the optional argument. Can it be modified so that the first equation in the series could also be customized (for instance, if I want three consecutive equations to be numbered as $(5_{x-y}), (5_{x+y}), (5_{z^2})$)? – R W Feb 11 '19 at 0:25
• @RW I'd say that \eqorn[_{x-y}] should work. – egreg Feb 11 '19 at 0:45
• My question is not about typesetting a subscript - this part works perfectly OK, and this is a great advantage of your solution (compared to using the subequations environment). However, what I don't manage to do right is the "main" numbering. In my example, if add \eqorn just before the labels for the 2nd and 3rd equations, I get $(5),(5_{x+y}),(5_{z^2})$, but if I add \eqorn before all 3 labels I get $(4_{x-y}), (4_{x+y}), (4_{z^2})$ (the counter doesn't move), whereas what I want is $(5_{x-y}), (5_{x+y}), (5_{z^2})$. Many thanks in advance! – R W Feb 11 '19 at 1:08
• @RW Not sure what you mean: if I open subequations and then make a gather environment with two equations having \eqorn[_a] and \eqorn[_b] respectively, I get 1_a and 1_b. If I then close subequations, the next equation is numbered 2. – egreg Feb 11 '19 at 9:27
• Here is an example \documentclass{article} \usepackage{amsmath} \newcommand{\eqorn}[1][']{\tag{\theequation\ensuremath{#1}}} \begin{document} $$\label{before} c+d$$ Some text ... \begin{gather} a+b \eqorn[a] \label{main} \\ 3+2 \eqorn[b] \label{secondary} \end{gather} ... some more text.... $$\label{after} c+d$$ with references to \eqref{before}, \eqref{main} and \eqref{secondary}, \eqref{after} \end{document} However, you are right, this does not happen inside the subequation environment. – R W Feb 11 '19 at 12:15

Here's an approach using expl3's \prg_replicate:nn to produce n copies of ' to number the equations in subequations and etoolbox's \patchcmd to insert that in the subequations environment.

\documentclass{article}
\usepackage{amsmath}
\usepackage{expl3}

% Creating a command to print a number of apostrophes
\ExplSyntaxOn
\cs_new:Npn \apost #1
{ \__andy_apost:cn { c@ #1 } }
\cs_new:Npn \__andy_apost:Nn #1
{
\ensuremath {
\prg_replicate:nn { #1 - 1 } { {'} }
}
}
\cs_generate_variant:Nn \__andy_apost:Nn { c }
\ExplSyntaxOff

% Patching-in that command in \subequations
\usepackage{etoolbox}
\patchcmd\subequations
{\def\theequation{\theparentequation\alph{equation}}}
{\def\theequation{\theparentequation\apost{equation}}}
{}{\FAIL}

\begin{document}

\begin{subequations}
$$a+b$$
$$3+2$$
$$5$$
\end{subequations}

\end{document}

• Possibly \ensuremath{\prg_replicate:nn { #1 - 1 }{'}} gives a better output. – egreg Nov 12 '18 at 22:29

An alternative is the modified answer from here:

\documentclass{article}
\usepackage{amsmath}
\makeatletter
\newcommand*\ifcounter[1]{%
\ifcsname c@#1\endcsname%
\expandafter\@firstoftwo%
\else%
\expandafter\@secondoftwo%
\fi%
}%
\makeatother

\newcommand\EqFamTag[2][roman]{%
\ifcounter{#2}{%
\xdef\temp{\csname #2 Eq\endcsname \ifnum\value{#2Prev}>0 \space(\csname #1\endcsname{#2Prev})\fi}%
\global\expandafter\let\csname #2\arabic{#2}\endcsname\temp%
\tag{\temp}%
}{%
\global\expandafter\newcounter{#2}%
\global\expandafter\newcounter{#2Prev}
\xdef\temp{\theequation \ifnum\value{#2Prev}>0 \space(\csname #1\endcsname{#2Prev})\fi}%
\xdef\eqonfamily{\theequation}%
\global\expandafter\let\csname #2 Eq\endcsname\eqonfamily%
\global\expandafter\let\csname #2\arabic{#2}\endcsname\temp%
\tag{\temp}%
}%
}%

\newcommand\myFamRef[2][1]{\csname #2#1\endcsname}

\begin{document}
\section{test}
$$f(x)=3\cdot x$$

$$x=4\cdot y \EqFamTag{MyFamily}$$

As an example:

$$x=4\cdot 2=8\EqFamTag{MyFamily}$$

The equation: \myFamRef[2]{MyFamily} is the first example/member of the equation \myFamRef{MyFamily}

$$x=4\cdot y$$

Another example of :

$$x=4\cdot 2=8\EqFamTag{MyFamily}$$

The equation: \myFamRef[3]{MyFamily} is the second example/member of the equation \myFamRef{MyFamily}

Another family of equations is:

$$f(x)=3+y \EqFamTag{NewFam}$$

and a member of it is:

$$f(6)=9 \EqFamTag{NewFam}$$

\end{document}


Output:

It is very easy to have a first (main) subequation numbered as an ordinary equation, the subequations standard numbering starting only at the second subequation:

\documentclass{article}
\usepackage{amsmath}
\setcounter{equation}{4}

\begin{document}
Some text . . . . . .
\begin{subequations}
$$c + d$$