# Lemma numbering by first instance of referencing

My document is long with proofs broken into several lemmas and many of them are in the appendix. However, it seems odd when I am using Lemma 25 (which should ideally be Lemma 1) to prove Lemma 2.

It does not immediately make clear that I am not making cyclic errors in proofs. Ideally, in any proof I'd like to reference only to Lemmas with numbers lower than them so that I do not make this error as my proofs get more and more complex.

This means that I'd like to have my Lemmas numbered by the first time they are referenced (or stated, one of the two that happens first). Any way that I can achieve this?

-
Doesn't feel too easy to follow to me... perhaps your "appendix" lemmas should be numbered differently, say "lemma A10"? –  vonbrand May 9 '13 at 2:12
The idea is to have a linear structure in the proofs with consistent number so that all lemmas/theorems only reference to lemmas proved before that, i.e. smaller in number. I'd rather not have a more confusing structure than initiating different numbering in the appendix. –  ste_kwr May 9 '13 at 2:26
Don't think in terms of "proved before" or such, think of your poor readers! Organize the stuff so that required results are proved before use, or somehow collect/mark them as "in an appendix, proof can be looked up there if you don't believe me". It might be useful (at least for you, perhaps for your editors/readers) to offer some kind of roadmap to make sure Thm 45 is not proven using Lem 312, which in turn is dependent on Cor 47, itself an easy consecuence of Thm 45... –  vonbrand May 9 '13 at 2:35
Thanks, but I think we are in the same page, and I am referencing the lemmas in the appendix before I use them. (For this theorem, we need Lemmas 6,8,12 proved in the appendix). But I don't want to have Lemmas 6,8,12 in the main body of the proof and still want to have them numbered 6,8,12 and not 22,24,28. –  ste_kwr May 9 '13 at 2:41
Have you thought of giving the statements of the Lemmas in the body of the document, and deferring the proofs to an appendix? That way you get the numbering that you like, and the reader has easy access to the statements rather than having to find them in an appendix. (this may be the same suggestion that vonbrand made) What sort of document is it? –  Scott H. May 9 '13 at 2:59

I don't use lemmas, but if they use a numbering scheme like equations, then you should be able to adapt this. I show here how to defer the presentation of equations, while preserving the numbering when they were "conceived."

I create a \defereqn command to "conceive" an equation (give it a number and remember it) without printing it. I also have the command \recalleqn which will recall the deferred equations (one by one) in the order they were conceived. If one wanted to recall them out of order, that should be doable, but then \recalleqn would require an argument and have to know how to process it.

\documentclass[12pt]{article}
\newcounter{deferred}\setcounter{deferred}{0}
\newcounter{shown}\setcounter{shown}{0}
\newcounter{saveequation}
\newcommand\defereqn[1]{%
\expandafter\edef\csname defeqnum\roman{deferred}\endcsname{%
\arabic{equation}}%
\expandafter\gdef\csname defeq\roman{deferred}\endcsname{#1}%
}
\newcommand\recalleqn{%
\setcounter{saveequation}{\value{equation}}%
\setcounter{equation}{\csname defeqnum\roman{shown}\endcsname}%
\csname defeq\roman{shown}\endcsname%
\setcounter{equation}{\value{saveequation}}%
}
\begin{document}

In equation~\ref{eq:first}, we prove what we need.
\defereqn{%
$$\label{eq:first} y = x$$
}%
Then we use it in equation~\ref{eq:second}

$$\label{eq:second} y = x^2$$

We will also use equation~\ref{eq:third}
\defereqn{%
$$\label{eq:third} y = x^3$$
}%
to prove

$$\label{eq:fourth} y = x^4$$

\clearpage

Our first deferred equation was

\recalleqn

Our next deferred equation was

\recalleqn

\end{document}


-
Thanks, this seems promising and seems to do exactly what I want. Let me try this out and get back there. –  ste_kwr May 12 '13 at 22:45
@user30407 I hope it works for you. FYI, it would also be straightforward, I think, to define the body of the equation at time of recall, rather than at time of first reference. –  Steven B. Segletes May 13 '13 at 9:54
This thing works excellently. I'm rather new to latex and your example taught me many things, I looked up all the commands to see what you were up to, etc. I've posted a modification specific to lemmas that allows one to pass arguments in case one needs to switch the order further. –  ste_kwr May 15 '13 at 2:25

This is an example that I have generated based on the answer by Steven B. Segletes, all credit goes to him.

You can pass the label of the lemma as the argument in the following while issuing \deferlemma and then use the same label to recall using \recalllemma

\documentclass[12pt]{article}

\usepackage{amsmath}

\newcounter{lema}\setcounter{lema}{0}
\newtheorem{lemma}[lema]{Lemma}

\newcounter{savelema}
\newcommand\deferlemma[2]{
\expandafter\edef\csname deflemnum#1\endcsname{\arabic{lema}}
\expandafter\gdef\csname deflem#1\endcsname{#2}
}

\newcommand\recalllemma[1]{
\setcounter{savelema}{\value{lema}}
\setcounter{lema}{\csname deflemnum#1\endcsname}
\csname deflem#1\endcsname
\setcounter{lema}{\value{savelema}}
}

\begin{document}

We will use Lemma~\ref{lem:first} to show the following result:
\deferlemma{first}{
\begin{lemma}
\label{lem:first}
$y = x$
\end{lemma}
}

\begin{lemma}
\label{lem:second}
$y = x^2$
\end{lemma}

We will also use lemma~\ref{lem:third} to prove
\deferlemma{third}{
\begin{lemma}
\label{lem:third}
$y = x^3$
\end{lemma}
}

\begin{lemma}
\label{lem:fourth}
$y = x^4$
\end{lemma}

\clearpage

Our first deferred lemma was

\recalllemma{first}

Our next deferred lemma was

\recalllemma{third}

\end{document}

-