Theorem numbering in an unlabeled section at the beginning of a chapter

In the memoir class, I would like to have my theorems labeled according to the sections, e.g., Theorem 1.2.3.

However, sometimes I have theorems at the beginning of a chapter before the first section of the chapter is declared. It seems that LaTeX incorrectly handles the numbering of the theorems in this case, and it does not seem to reset the theorem counter at the declaration of a new chapter. For example

\documentclass{memoir}
\newtheorem{theorem}{Theorem}[section]
\begin{document}
\chapter{One}
\section{A}
\begin{theorem} OneA \end{theorem}
\chapter{Two}
\begin{theorem} Two \end{theorem}
\section{B}
\begin{theorem} TwoB \end{theorem}
\end{document}


Gets me:

Chapter 1 One

1.1 A

Theorem 1.1.1 OneA

Chapter 2 Two

Theorem 2.0.2 Two

2.1 B

Theorem 2.1.1 TwoB

which gets the numbering of theorem Two incorrectly as 2.0.2 (whereas it should be 2.0.1), since the counter is still running from the last section in the previous chapter.

How do I fix this?

• I think, it should be 2.2 then, not 2.0.2 as you wrote? – user31729 Mar 1 '15 at 16:25

This is fixed in the next release of LaTeX (the above was generated with the pre-release code), but meanwhile you can use the code in the reply to the latex bug report at

http://www.latex-project.org/cgi-bin/ltxbugs2html?pr=amslatex/4393

\makeatletter
\def\@stpelt#1{\global\csname c@#1\endcsname \m@ne\stepcounter{#1}}
\makeatother


The memoir class already provides the mechanism for this:

\documentclass{memoir}

\newtheorem{theorem}{Theorem}[section]
\counterwithin*{theorem}{chapter}

\begin{document}
\chapter{One}
\section{A}
\begin{theorem} OneA \end{theorem}
\chapter{Two}
\begin{theorem} Two \end{theorem}
\section{B}
\begin{theorem} TwoB \end{theorem}
\end{document}


The command \counterwithin*{theorem}{chapter} tells LaTeX to reset the theorem number when chapter is stepped, without modifying its representation (because of *).

If you want to remove the 0, here's the trick:

\documentclass{memoir}
\newtheorem{theorem}{Theorem}[section]
\counterwithin*{theorem}{chapter}
\renewcommand{\thetheorem}{%
\ifnum\value{section}>0
\thesection.%
\else
\thechapter.%
\fi
\arabic{theorem}%
}

\begin{document}
\chapter{One}
\section{A}
\begin{theorem} OneA \end{theorem}
\chapter{Two}
\begin{theorem} Two \end{theorem}
\section{B}
\begin{theorem} TwoB \end{theorem}
\end{document}


You can load the etoolbox package and reset the theorem counter at the beginning of each chapter.

\usepackage{etoolbox}
\pretocmd{\chapter}{\setcounter{theorem}{0}}{}{}


MWE

\documentclass{memoir}
\newtheorem{theorem}{Theorem}[section]

\usepackage{etoolbox}
\pretocmd{\chapter}{\setcounter{theorem}{0}}{}{}

\begin{document}
\chapter{One}
\section{A}
\begin{theorem} OneA \end{theorem}
\chapter{Two}
\begin{theorem} Two \end{theorem}
\section{B}
\begin{theorem} TwoB \end{theorem}
\end{document}


• You could, but if you want other things than theorems such as lemmas, proofs you'd have to do each one. The one line change to \@stpelt given in the bug report fixes this for all counters at all sectioning levels. – David Carlisle Mar 1 '15 at 16:44
• @DavidCarlisle I know, it's just to give one more possibility. :-) – karlkoeller Mar 1 '15 at 16:46
• @DavidCarlisle All theorem like environments should be numbered together. – egreg Mar 1 '15 at 21:26
• @egreg yes and chapters should start with a section, but you never know what users want:-) – David Carlisle Mar 1 '15 at 22:16

You can reset numbers at chapter boundaries too by using the internal \@addtoreset command as

\makeatletter\@addtoreset{theorem}{chapter}\makeatother


after you have declared your theorems.

\documentclass{memoir}
\newtheorem{theorem}{Theorem}[section]