# Advanced hierarchical counting for theorems

I want to number successive theorems with hierarchical counting (top level counter n, sub level counter m) in a way that I can chose from the following options for each theorem:

• Theorem n. (increases n-counter, resets m-counter)
• Theorem n.m. (increases n-counter, resets m-counter)
• Theorem n.m. (increases m-counter)

To allow sth. like

Theorem 1.

Theorem 2.1

Theorem 2.2.

Theorem 3.

Theorem 3.1.

In fact, a quite liberal numbering should be possible, with multiple levels and custom counting-style (arabic, alph, ...).

But all define new theorem environments, so you have to use different environments for the different numbering options. The problem with this is that I already have some theorem environments and I want to use this counting scheme for all of them in the same way (at the moment, they just use the same counter).

So is there any way to achieve this without creating new versions of each theorem environment for each numbering option?

If not, a conclusion would be that, if you want to compose multiple independent style parameters for theorems (e.g. the numbering way, the header/body font, ...), you would have to create an own environment for each combination. This would produce much redundance and would be hard to maintain. Is it like that?

### Ideas:

• Is it possible to have a counter to be passed at \renewtheorem{th}[counter]{Theorem} which counts like m.n and can be increased manually in m and n?

• Can you create environments with options, so when opening a new theorem environment you pass the numbering option?

Here is an environment, within which each theorem is numbered with an extra digit appended to the previous theorem number. If you want a Theorem 2.1 without a previous Theorem 2, just add \stepcounter{theorem} before the start of the environment. \documentclass{article}

\newtheorem{theorem}{Theorem}

\newcount\savedtheorem
\newenvironment{subtheorems}{%
\savedtheorem=\value{theorem}%
\edef\prevthetheorem{\thetheorem}%
\setcounter{theorem}{0}%
\renewcommand\thetheorem{\prevthetheorem.\arabic{theorem}}%
}
{%
\setcounter{theorem}{\savedtheorem}%
}

\begin{document}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum
gravida mauris.

\begin{theorem}
Some theorem.
\end{theorem}

\begin{subtheorems}

\begin{theorem}
Some subtheorem.
\end{theorem}

\begin{theorem}
Some subtheorem.
\end{theorem}

\begin{subtheorems}

\begin{theorem}
Some sub-subtheorem.
\end{theorem}

\begin{theorem}
Some sub-subtheorem.
\end{theorem}

\end{subtheorems}
\end{subtheorems}

\begin{theorem}
Some theorem.
\end{theorem}
\end{document}


Below is a minimal implementation that creates a theorem environment from scratch. The theorem environment takes one optional parameter, which actually represents a counter (default is theorem, but I've also defined subtheorem). Apart from that, the construction of the environment is the same whether you're using a "regular theorem" or a "subtheorem": \documentclass{article}

\usepackage{lipsum}

\newcounter{theorem}
\newcounter{subtheorem}[theorem]
\renewcommand{\thesubtheorem}{\thetheorem.\arabic{subtheorem}}
\newenvironment{theorem}[theorem]
{\refstepcounter{#1}%
\noindent\textbf{Theorem~\csname the#1\endcsname.}\itshape%
\ignorespacesafterend}

\begin{document}

\lipsum

\begin{theorem}
Some theorem.
\end{theorem}

\begin{theorem}[subtheorem]
Some subtheorem.
\end{theorem}

\begin{theorem}[subtheorem]
Some subtheorem.
\end{theorem}

\begin{theorem}
Some theorem.
\end{theorem}

\begin{theorem}[subtheorem]
Some subtheorem.
\end{theorem}
\end{document}


Of course, you could add more levels if you want, or modify the theorem environment to suit your needs.

• Why not just \newtheorem{theorem}{Theorem} and \newtheorem{subtheorem}{Theorem}[theorem], doens't that do exactly the same thing? I think this gives it a bette structure than giving an optional argument. – daleif Jun 4 '14 at 18:28
• @daleif: Perhaps, but the OP was after using a single environment rather than defining multiple ones for each level of nesting. – Werner Jun 4 '14 at 18:30
• He/she will learn his/her lesson soon enough. – daleif Jun 4 '14 at 18:31

Only the environments theorem, subtheorems and subtheorems* are needed, and I believe it behaves exactly as requested.

Starred variant subtheorems* -- subtheorems of the previous theorem

Unstarred variant subtheorems -- subtheorems with a fresh new parent number.

\documentclass{article}

\pagestyle{empty}

\newtheorem{theorem}{Theorem}
\newtheorem{theorem@ii}{Theorem}[theorem]

\makeatletter
\newenvironment{subtheorems}
{%
\let\theorem\theorem@ii
\stepcounter{theorem}
}{}
\makeatother

\makeatletter
\newenvironment{subtheorems*}
{%
\let\theorem\theorem@ii
}{}
\makeatother

\begin{document}

\begin{theorem}ONE\end{theorem}

\begin{theorem}TWO\end{theorem}

\begin{subtheorems}
\begin{theorem}THREE---ONE\end{theorem}
\begin{theorem}THREE---TWO\end{theorem}
\end{subtheorems}

\begin{theorem}FOUR\end{theorem}

\begin{subtheorems*}
\begin{theorem}FOUR---ONE\end{theorem}
\begin{theorem}FOUR---TWO\end{theorem}
\end{subtheorems*}

\end{document} 