# Number theorems with subsections: “Theorem 1.2.3.4”

I would like my numbering of theorems to be like "Theorem 1.2.3.4" where 1 is the chapter, 2 is the section, 3 is the subsection, and 4 refers to the fourth theorem in this subsection.

A solution is using \numberwithin{mytheorem}{subsection}: Theorem numbering as chapter.section.subsection.theorem number

However, if I place a theorem in a section, so not in a subsection, I get "Theorem 1.2.0.4". Is it possible to get "Theorem 1.2.4" in this case?

• Not without some serious reprogramming (or various handheld adjustments) , you should reconsider this numbering scheme, it might confuse your readers more than it would help them. – daleif Jun 5 '16 at 14:14
• @daleif, Is it really that strange a question? So I cant use \newtheorem{thm}{mytheorem}[section] and put some mytheorem's in sections and some in chapters? (because then I get "Theorem 0.1") – Carucel Jun 5 '16 at 20:10
• @Carucel: You could change the resetting counter from time to time manually. – user31729 Jun 6 '16 at 14:04

As daleif mentionted: The numbering scheme can confuse readers, but here's a solution.

The command \clevercnt checks whether (sub)section counters are greater than zero and uses the full format then, otherwise another, shorter format is used.

\documentclass{book}

\usepackage{amsmath}
\usepackage{amsthm}

\newtheorem{thm}{Theorem}[subsection]
%\numberwithin{thm}{subsection}

\newcommand{\clevercnt}[1]{%
\ifnum\value{subsection} > 0
\thesubsection.\arabic{thm}%
\else
\ifnum\value{section} > 0
\thesection.\arabic{thm}%
\else
\thechapter.\arabic{thm}%
\fi
\fi
}

\renewcommand{\thethm}{\clevercnt{thm}}

\begin{document}

\chapter{Foo}
\begin{thm}

\end{thm}

\chapter{Second}
\section{Foo}
\begin{thm}
Hello
\end{thm}

\chapter{Second}
\section{Foo}
\begin{thm}
Hello
\end{thm}

\section{other}
\subsection{Foobar}
\begin{thm}
Hello
\end{thm}

\end{document}

• On second thought this will not work in any case, however! – user31729 Jun 5 '16 at 18:06
• Thank you! Why do you say it doesn't work? – Carucel Jun 5 '16 at 20:13
• I did say it will not work in any case -- e.g before a chapter starts ( though unlikely this would be used that way) – user31729 Jun 5 '16 at 20:16