Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
I thought about trying to answer this, but I couldn't decide on the best way to even set up what the user commands should be, let alone the actual code to make it work. I'm sure it's not too tricky, but I don't really have time... –  Seamus Sep 29 '11 at 9:04
    
very related, if not even a duplicate: Interrupting and resuming subequations –  Tobias Kienzler Jul 9 '13 at 12:05
add comment

4 Answers

up vote 12 down vote accepted
+25
\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).

share|improve this answer
add comment

If you have LuaLaTeX, we can provide a slight improvement on egreg's solution (in the sense that you'd only need one macro). Most (though not all) of the macro code could then be moved to lua. I've implemented the single macro as \tageqn[label]{number_category}; the name is easily changable.

\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage{luacode}
\usepackage{xifthen}
\usepackage{hyperref}

\begin{luacode}
tagList = {}    --maps number category to current value

function tagEqn(num)
    letter = incrementLetter(num)

    --default label (e.g. eqn:1b); can be configured by user
    label = "eqn:".. num .. letter

    outputEqn(num .. letter, label)
end

function labelEqn(num, label)
    outputEqn(num .. incrementLetter(num), label)
end 

--increments the current num's letter, returns the new letter
function incrementLetter(num)
    if tagList[num] == nil then
        tagList[num] = 0
    else
        tagList[num] = tagList[num] + 1
    end

    return string.char(string.byte("a")  + tagList[num])
end

function outputEqn(tag,label)
    tex.print( " \\tag{" .. tag ..  "}" .. "\\label{"..label.."}") 
end
\end{luacode}

\makeatletter
\newcommand{\tageqn}[2][]{\ifmeasuring@ \else \ifthenelse{\isempty{#1}}{\directlua{tagEqn(#2)} }{\directlua{labelEqn(#2,"#1")} } \fi}
\makeatother

\begin{document}

\section*{Basic, no specified labels}
Testing basic equations (including the equation environment) with no specified labels:\[999-99= \text{ modern math}\tageqn{17}\]
\begin{equation}
232332 = 912929192 \tageqn{17}
\end{equation}
\[323223=232332\tageqn{11}\]

Trying out default reference `eqn:17a' gives \ref{eqn:17a}, `eqn:17b' gives \ref{eqn:17b}, and `eqn:11a' gives \ref{eqn:11a}.

\section*{Basic, specified labels}

Testing basic equations with labels `dog', `cat', and `Time Lord'.\[999-99\tageqn[dog]{17}\]
\begin{equation}
232332 = 912929192 \tageqn[cat]{17}
\end{equation}
\[323223=232332\tageqn[Time Lord]{11}\]

Trying out references gives \ref{dog}, \ref{cat}, and \ref{Time Lord}.

\section*{Testing align (default labels)}

Finally, we test aligns.  First we do so with default labels

\begin{align}
3 &= 4 + 7 \tageqn{17}
\\&= 8 + 9 \tageqn{17}
\\&> x \tageqn{11}
\end{align}

Trying out default reference `eqn:17e' gives \ref{eqn:17e}, `eqn:17f' gives \ref{eqn:17f}, and `eqn:11c' gives \ref{eqn:11c}.

\section*{Testing align (custom labels)}

Now with labels `rar', `ar', and `gaga'.

\begin{align}
3 &= 4 + 7 \tageqn[rar]{17}
\\&= 8 + 9 \tageqn[ar]{17}
\\&> x \tageqn[gaga]{11}
\end{align}

The references give us: \ref{rar}, \ref{ar}, and \ref{gaga}, respectively!

\section*{Autoref}

Finally, we try out autoref. `eqn17e' gives \autoref{eqn:17e}, `Time Lord' gives \autoref{Time Lord}, and `gaga' gives \autoref{gaga}!

\end{document}
share|improve this answer
add comment

this is not a full solution but just some hints on how to do it.

  1. You will have to define your own counter for "sub-equations":

    \newcounter{subequation}
    
  2. Define how you want to display such counter:

    \renewcommand{\thesubequation}{\alph{subequation}}
    
  3. You can then create a reference and label for the equation with:

    \refstepcounter{subequation} \label{someLabel}
    

Now, when you use \ref{somLabel}, you will have the number for that equation. There are some issues though, as it will not show the number of the first equation, but just a., b., etc. and it won't 'reset' on its own when stepping the main equation counter.

You can have it automatically reset by using:

\newcounter{subequation}[equation]

but this won't work with your example as you want to insert the new sub-counter for eq1 after already incrementing to eq2.

Anyway, these are just hints for what to look at. Maybe someone with better knowledge of counters could help there.

share|improve this answer
add comment

Have you looked at the subequations environment provided by amsmath.sty? It seems to do exactly what you want. (See section 3.11.3 in amsldoc.pdf, which is at ftp://ftp.ams.org/ams/doc/amsmath/amsldoc.pdf. For example, if you type

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\thispagestyle{empty}
Here's some text.
\begin{subequations}
  Here's even more text.
  \begin{equation}
    \label{eq:first}
    x - y = 0
  \end{equation}
  Then again, we also have
  \begin{equation}
    \label{eq:second}
    x = y
  \end{equation}
  Of course, equation \eqref{eq:second} is just another version of
  equation \eqref{eq:first}.
\end{subequations}

Here's more text. Here's more text. Here's more text.
\begin{subequations}
  Here's an align environment:
  \begin{align}
    x^{2} + y^{2} &= z^{2} \label{pythag}\\
    \sin^{2}x + \cos^{2}x &= 1\label{trig}
  \end{align}
  Equation \eqref{pythag} isn't really the same as equation
  \eqref{trig}, but they sort of look similar.
\end{subequations}
\end{document}

then you'll get

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.