## Monday, February 11, 2013

### Using counters to correctly number out-of-order theorem environments

A little trick I had to figure out today while writing up the joint paper Ralph and I want to have done soon.

For stylistic reasons, I find myself frequently wanting to state a main theorem, then state and prove a bunch of lemmas, then come back and prove the main theorem. Also, frequently, I'll want to have automatically numbered claims within that proof:

Theorem 1.1: The moon is made of green cheese.

Theorem 1.2: The moon is not made of rock.

Proof: Clear.

Lemma 1.3: The moon is organic.

Proof: Use spectroscopy.

Proof of Theorem 1.1: We have established that the moon is organic.

Claim 1.1.1: The organic material of the moon is cheese....

The problem, of course, is that $$\LaTeX$$ keeps a counter of what Theorem I'm on, and if I just tell it to insert a claim where you see one in the example, it will number that claim 1.3.1 instead of 1.1.1. (I number all theorems, definitions, lemmas etc in one sequence using the counter 'thm'.)

The way to fix this is to use "counters", which is basically $$\LaTeX$$-speak for variables.

First: in the preamble to the document, put the following:

\newcounter{delayedthm}
\newcounter{restoredthm}


Next, put the following immediately after the \end{thm} of Theorem 1.1:

\setcounter{delayedthm}{\value{thm}}


and put a similar definition

\setcounter{restoredthm}{\value{thm}}


after the \end{lemma} of Lemma 1.3. Note: for reasons that aren't entirely clear to me, commands that manipulate counters, such as \value{} or \addtocounter{}{}, use the unslashed name of the counter.

Now, right before the \begin{proof} of the proof of Theorem 1.1, put

\setcounter{thm}{\value{delayedthm}}


and

\setcounter{thm}{\value{restoredthm}}


after \end{proof}. Voila! Renumbering on the fly!