Tuesday, April 29, 2008

Correctly reset LaTeX's theorem counters

Now that's a technical title to start with! Let's keep this short: I've gotten the following question a few times already. When numbering theorems (either standard LaTeX or by package amsthm) within sections (or deeper levels), the counter is not correctly reset when starting a new chapter (or in fact any level above your numbering level).



\chapter{First chap}
\section{First sec}
\begin{lemma}that's the one\end{lemma}

\chapter{Second chap}
\begin{lemma}This number is wrong.\end{lemma}
\section{Next sec}
\begin{lemma}Now it's OK again.\end{lemma}

Will produce:
Chapter 1 First chap
1.1 First sec
Lemma 1.1.1 That's the one
Chapter 2 Second chap
Lemma 2.0.2 This number is wrong.

2.1 Next sec
Lemma 2.1.1 Now it's OK again.

Notice how the theorem counter did not get reset when entering the second chapter. It is only reset when entering a new section. Of course, I know that the zero is ugly, but apparantly some people really do want to include theorems in a chapter even before the first section has started.

In your document preamble (after the \newtheorem), put the following:


Note that this is only necessary for document classes report and book. When you are numbering within, e.g., subsection, also add a \@addtoreset{lemma}{section}. You'll get the idea.