Home > sectioning > Resetting theorem counters: \section and (missing) \subsection

# Resetting theorem counters: \section and (missing) \subsection

August 3Hits:2

Suppose I define a new theorem "Theorem" and declare that its counter should be reset whenever the subsection counter is incremented or reset:

\newtheorem{theorem}{Theorem}[subsection] 


At some point in my (article) document I start a new \section (say, section 2) without starting a \subsection. I expect the first theorem in this section to be numbered with "2.0.1" (which may be considered bad style). However, the theorem is numbered "2.0.2".

Is this "by design" (technically, I didn't start a new \subsection) or a bug? Moreover, what would be a proper workaround? (Such a workaround should not involve manually resetting the theorem counter, and it should work for whatever theorem type I may define in addition to "Theorem".)

\documentclass{article}  \newtheorem{theorem}{Theorem}[subsection]  \begin{document}  \section{bla}  \subsection{blubb}  \begin{theorem} Some text. \end{theorem}  \subsection{foo}  \begin{theorem} Some text. \end{theorem}  \section{bar}  \begin{theorem} Some text. \end{theorem}  \end{document} 


EDIT: In response to Seamus' comment: The unexpected counter value also occurs with amsthm and ntheorem.

In LaTeX, “subcounters” are only reset when the counter is incremented by a \stepcounter. They are not reset when the counter is changed in any other way (e.g. via \setcounter or TeX commands).

The \section increments the section counter and thus sets the subsection counter to 0. But this does not reset the theorem counter. Only the next \subsection (which increments the subsection counter) resets the theorem counter.

I guess the easiest (or at least safest; see the comments) workaround is to simply add the theorem counter to the reset list for the section counter:

\makeatletter
\makeatother



Perhaps more of a motivation to Caramdir's answer, the LaTeX core - latex.ltx - defines a new theorem in your usage via \newtheorem{<env_name>}{<caption>}[<within>]. This defines a new counter called <env_name> and executes \@addtoreset{<env_name>}{<within>} (if the counter <within> exists, otherwise LaTeX produces an error). The latter resets the counter <env_name> whenever <within> is incremented.

\makeatletter
\makeatother



resets the theorem counter whenever you use \section{...} as well.

A less esoteric version of Caramdir's solution is through the chngcntr package:

\usepackage{chngcntr}

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



The *-version of \counterwithin avoids the package being smart and redefine \thetheorem as it would do without the *

If you want to avoid doing the additional command for each theorem-like environments you define, just abstract the construction:

\usepackage{chngcntr}

\newcommand{\newsubsectheorem}[2]{%
\newtheorem{#1}{#2}[subsection]%
\counterwithin{#1}{section}}

\newsubsectheorem{theorem}{Theorem}
\newsubsectheorem{proposition}{Proposition}
\newsubsectheorem{lemma}{Lemma}



If you later change your mind and want to return to a "section-theorem" numbering, just modify accordingly \newsubsectheorem.

## Related Articles

• ### Resetting theorem counters: \section and (missing) \subsectionAugust 3

Suppose I define a new theorem "Theorem" and declare that its counter should be reset whenever the subsection counter is incremented or reset: \newtheorem{theorem}{Theorem}[subsection] At some point in my (article) document I start a new \sectio

• ### How can I number theorems by section in LyX?November 10

The theorem count doesn't update when I move to another section. I.e. I'm currently in section 6, and LyX has already in theorem 30, when theorem 1 is in section 1. How can I make LyX start counting the theorems and lemmas from "1" in each new s

• ### How to identify the counter of Equation, Theorem, and SectionJanuary 16

I asked before how to identify the counter of Equation and Theorem. A simple answer suggested there is \newtheorem{thm}[equation]{Theorem} A simple variation allows me to identify the Theorem and Section counter: \newtheorem{thm}[section]{Theorem} Is

• ### Error in numbering theorems in section zero of a chapter in reportApril 6

I'm writing a multichapter report and I'm having a problem with the theorems being numbered correctly of section zero of a new chapter. For instance, chapter two ends with Corollary 2.3.21 and Chapter three begins with Theorem 3.0.22. Also, Chapter t

• ### How to reset all counters at once?June 4

I have several environments in my .tex file. I'd like to know if there is a way to reset all counters at once? To clarify what I mean, suppose we have environments theorem, proposition, etc., and instead of using individual commands \setcounter{theor

• ### How can I set customized separators for theorem counters?February 7

How can I set customized separators for theorem counters? For example in The chapter, section and number are separated with dot. How can I change these dots to other symbols? For instance how can I change the first dot with dash and the second dot wi

• ### Problem with numbering theorem by sectionApril 7

I am having a problem with the numbering of the theorems (by section) in a paper. My problem is that I want to number my theorems and propositions such that they appear as something like: Theorem 3.1, Proposition 3.1 etc. However, using the current l

• ### Is it possible to embed section titles within subsections?June 19

I would like to to do the following \section{Animals} \subsection{Mammals} \subsection{Birds} and have it result in 1 Animals 1.2 Animals>Mammals 1.3 Animals>Birds I have tried to do this with the titlesec package, but it does not seem to support em

• ### Header should display section if no subsection availableFebruary 27

I'm writing a document which uses scrartcl alongside with twoside and want to create a header with scrpage2. As expected the package prints the section on even pages and the subsection on odd pages. So far so good. But when there is no subsection ava

• ### How do I insert section and first subsection in the header using scrartcl?December 1

I have a scartcl document and I would like to add a header, with the section aligned to the left, and the first subsection on the page aligned to the right. The header should not be in italic. How should I go about doing so? UPDATE Based on the answe

• ### reset theorem counter after every subsection and print just #subsectionOctober 31

I have declared \newtheorem{thm}{Theorem} \numberwithin{thm}{subsection}. This numbers every theorem in this format [chapter].[section].[subsection].[numberofthm]. Is it possible to have the counter just printing [numberofthm] instead? --------------

• ### How to reset chapter and section counter with \partMay 4

To get the output Toc or pdf bookmark below: Part I Chapter I Chapter II Part II Chapter I Chapter II I need to reset the chapter counter. I google and get one method. \makeatletter \@addtoreset{chapter}{part} \makeatother Yes, It can output like thi

• ### Theorem environment numbered according to subsectionDecember 20

I was wondering if there was a way to modify the theorem environment so that it interacted with sections/subsections as follows: \section{} \subsection{} \begin{ex} Whatever \end{ex} \begin{ex}Whatever \end{ex} \subsection{} \begin{ex} Whatever \end{

I am using latex for a big report, using the article class. So far, so good. The problems is the table of contents does not point to the right page for sections. It works great for subsections (as far as I can tell, there are too many to try them all

• ### Centering sections without centering subsectionsSeptember 26

This question is about how to center all the sections (including chapters) without centering subsections. I have browsed all the related questions asked here but I found no one to which my question is duplicate. I have tried a proposed solution that

• ### numbering theorem in sections of an article July 21

I am writing an article in WinEdt (Windows 7), but it's creating problem in numbering the theorems in subsections. My article consists of different sections. In section 3 it starts with a Lemma and by the \begin{lemma} command it takes the number "Le

• ### Issue with resetting theorem counterMarch 20

I am using LyX to typeset a document. I wanted to use another theorem-like construct not available in amsthm. So I included it using LaTeX code. \newtheorem{discussion}{Discussion}[section] This works fine except the next theorem that appears using t

• ### fancyhdr: Section number, Section, colon and subsection in headerApril 12

I am a bit confused with the fancyhdr options. I currently have this style for my headers: MWE: \documentclass[12pt,oneside]{book}%use oneside to ensure no blanks\ \usepackage[text={5.45in,8.5in}, left=1.5in, right=1.25in, top=1.25in, bottom=1.25in,