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`

.

## Answers

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
\@addtoreset{theorem}{section}
\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.

Therefore, adding

```
\makeatletter
\@addtoreset{theorem}{section}
\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`

.