8

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?

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

4 Answers 4

7

enter image description here

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
5

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 *).

enter image description here

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}

enter image description here

3

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} 

enter image description here

4
  • 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. Commented Mar 1, 2015 at 16:44
  • @DavidCarlisle I know, it's just to give one more possibility. :-) Commented Mar 1, 2015 at 16:46
  • @DavidCarlisle All theorem like environments should be numbered together.
    – egreg
    Commented Mar 1, 2015 at 21:26
  • @egreg yes and chapters should start with a section, but you never know what users want:-) Commented Mar 1, 2015 at 22:16
2

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.

Sample output

\documentclass{memoir}
\newtheorem{theorem}{Theorem}[section]
\makeatletter\@addtoreset{theorem}{chapter}\makeatother
\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 must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .