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

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.

-
That's interesting. Does this persist if you load amsthm or ntheorem? (I imagine this might be the sort of slight bug that theorem packages might fix... –  Seamus Aug 3 '11 at 17:05
@Seamus: No, they do not change that, because technically you could use any counter in the theorem definition, not just one that comes from a section. So the theorem packages have no easy way of figuring out what you want. –  Caramdir Aug 3 '11 at 17:39

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

-
In other words, the answer to my first question is "by design". What about a workaround like, whenever a "master" counter is incremented, every "slave" counter is incremented and only then reset? –  lockstep Aug 3 '11 at 17:43
@lockstep: That would require to change the counter reset mechanism of LaTeX: \def\@stpelt#1{\stepcounter{#1}\global\csname c@#1\endcsname \z@} (see texdoc source2e, page 104). Be aware that this changes that for all counters and hence might have unintended side-effects. –  Caramdir Aug 3 '11 at 17:47
About your "easiest workaround": True, but it would have to be done for every theorem type, so it's not a perfectly automatic solution. –  lockstep Aug 3 '11 at 17:49
Thanks for pointing me to \@stpelt -- I understand that changing its definition shouldn't be done lightly, but this is an answer to my second question. –  lockstep Aug 3 '11 at 18:06
@lockstep: Alternatively you could add a wrapper around \newtheorem that checks what the counter name is and adds the appropriate \@addtoresets. –  Caramdir Aug 3 '11 at 18:13

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.

-