[Tex/LaTex] Inserting/Numbering/Labelling/Referencing several times the same equation written in various form

cross-referencingequationsnumbering

I'm looking for an automatic way in LaTeX to number a same equation that would be inserted several times (under different forms for instance). The output would look something like:

some text
   x-y=0    (1)
some text
   x=y      (1a)
some text. Now with an align environment
   x2-y2=0  (2)
   y-x=0    (1b)
and with an align environment again
  x2=y2     (2a)
  x2+x=y2+y (3)
Equations (1a) and (1c) are just different forms of Eq. (1), the same for Eq. (2a) and Eq. (2)

The perfect solution would have:

  • an automatic increment of letters
  • usability with both equation and align environment
  • simple labeling and referencing of the multiple equation forms
  • compatibility with autoref

But of course that could sound like an utopia, so any workarounds are welcome.

I've tried with the package deleq and the tag command. I don't want to use the deleq environments because they seem to rely on something like eqnarray. That's how far I got:

\documentclass[11pt]{article}
\usepackage{deleq}  
\usepackage{hyperref}
\usepackage{amsmath}
\begin{document}
Some text
\begin{equation} y-x=0 \label{Demo1} \end{equation}
Some text (the following is not compatible with equation)
$$ x=y \label{Demo1bis} \rndeqno{Demo1} $$ %Automatic ok but not equation and bad reference!
Some text, now with an align environment:
\begin{align}
  x_2-y_2&=0 \label{Demo2} \\
  y&=x \label{Demo1ter} \tag*{\ref{Demo1}b} %\rdeqno{Demo1} 
\end{align}
Some text, with align
\begin{align}
  x_2&=y_2 \label{Demo2bis} \tag*{\ref{Demo2}b} \\Not automatic !!!  %\rndeqno{Demo2}\\
  x_2+x&=y_2+y \label{Demo3} 
\end{align}
\autoref{Demo1bis} and \autoref{Demo1ter} are different forms of \autoref{Demo1}, the same for \autoref{Demo2bis} and \autoref{Demo2}. 

\end{document}

Best Answer

\documentclass[a4paper]{article}
\usepackage{amsmath}

\makeatletter
\newcommand{\defvariant}[1]{\label{#1}%
  \ifmeasuring@\else
    \expandafter\xdef\csname variant@#1\endcsname{\theequation}%
    \expandafter\gdef\csname variant@#1@number\endcsname{0}
  \fi}

\newcommand{\stepvariant}[2][]{%
  \ifmeasuring@\else
    \expandafter\xdef\csname variant@#2@number\endcsname{%
      \number\numexpr\csname variant@#2@number\endcsname+1\relax}%
    \tag{\ref{#2}\@alph{\number\csname variant@#2@number\endcsname}}%
    \if\relax\detokenize{#1}\relax\else\label{#1}\fi
  \fi}

\makeatother

\begin{document}
some text
\begin{equation}
   x-y=0 \defvariant{equal}
\end{equation}
some text
\begin{equation}
   x=y   \stepvariant[ea]{equal}
\end{equation}
some text. Now with an align environment
\begin{align}
   x^2-y^2&=0 \defvariant{squares} \\
   y-x&=0     \stepvariant[eb]{equal}
\end{align}
and with an align environment again
\begin{align}
  x^2&=y^2 \stepvariant[sa]{squares} \\
  x^2+x&=y^2+y 
\end{align}
Equations \eqref{ea}~and~\eqref{eb} are just different forms of 
Equation~\eqref{equal}, the same for Equation~\eqref{sa} and 
Equation~\eqref{squares}.
\end{document}

The command \defvariant defines an equation that can possibly have variant forms; its argument is also used to label the equation.

The command \stepvariant takes as mandatory argument the label of the "parent" equation and as optional argument a label for later reference.

A key point is \ifmeasuring@ that avoids stepping twice the pseudocounter for "children" equations in environments such as align and gather that typeset twice their contents, the first time for measuring the equation sizes (when \ifmeasuring@ is set to true).