# How to determine in advance whether a chapter has any sections, a section has any subsections, or a subsection has any subsubsections?

Continuing Test if a chapter has sections : to number theorem-like environments in a nice way, it is advisable to know at the start of a logical portion of your document whether this logical portion contains deeper logical portions. In other terms, you wish to know at the start of a chapter (or a section or a subsection) X whether a section (respectively, a subsection or a subsubsection) appears before X is over. This would let you choose the numbering level of the theorems properly.

Let's consider, for example, theorems in the section s of a chapter c. There are two cases.

• If c.s logically contains no subsections, you can number the theorems within c.s as c.s.1, c.s.2, …, since these numbers will not be confused with any numbers of any sectioning element.

• If c.s logically contains at least one subsection, you are advised to number the theorems before the first subsection as c.s.0.1, c.s.0.2, … and number the theorems within the subsections as c.s.1.⁅whatever⁆, c.s.2.⁅whatever⁆, …. By writing c.s.ss.⁅whatever⁆, we mean that ss is the subsection number and you apply the same rule recursively: you check whether c.s.ss has subsubsections or not and enumerate the theorems inside c.s.ss accordingly as

• c.s.ss.sss.1, c.s.ss.sss.2, … (where sss is the subsubsection number)
• or as c.s.ss.1, c.s.ss.2, ….

There is one really bad manual way to accomplish this (I took the preamble I need a solution for):

\documentclass[envcountsame,envcountsect]{svmono}%%% from http://www.springer.com/gp/authors-editors/book-authors-editors/resources-guidelines/book-manuscript-guidelines/manuscript-preparation/5636
\PassOptionsToPackage{mainaux,starttoc,index,glossary}{rerunfilecheck}
\usepackage[compress]{cleveref}
\crefname{theorem}{Thm.}{Thms.}%%% Abbreviate in the middle of a sentence.
\Crefname{theorem}{Theorem}{Theorems}%%% No abbreviation should start any sentence.
\crefrangeformat{theorem}{Thms.\@ #3#1#4 -- #5#2#6}%%% Allow for line breaks everywhere.
\Crefrangeformat{theorem}{Theorems #3#1#4 -- #5#2#6}%%% Allow for line breaks everywhere; no abbreviation should start any sentence.
\begin{document}
\numberwithin{theorem}{chapter}
\chapter{Introduction}
\begin{theorem}\label{thm:A}This theorem should have number 1.1 .\end{theorem}
\begin{theorem}\label{thm:B}This theorem should have number 1.2 .\end{theorem}
\part{First Part}
No theorems here.
\chapter{Second Chapter}
\begin{theorem}\label{thm:C}This theorem should have number 2.1 .\end{theorem}
\begin{theorem}\label{thm:D}This theorem should have number 2.2 .\end{theorem}
\numberwithin{theorem}{section}
\part{Second Part}
No theorems here.
\chapter{Third Chapter}
\begin{theorem}\label{thm:E}This theorem should have number 3.0.1 .\end{theorem}
\begin{theorem}\label{thm:F}This theorem should have number 3.0.2 .\end{theorem}
\section{Some Section}
\begin{theorem}\label{thm:G}This theorem should have number 3.1.1 .\end{theorem}
\begin{theorem}\label{thm:H}This theorem should have number 3.1.2 .\end{theorem}
\part{Third Part}
No theorems here.
\chapter{Fourth Chapter}
\begin{theorem}\label{thm:I}This theorem should have number 4.0.1 .\end{theorem}
\begin{theorem}\label{thm:J}This theorem should have number 4.0.2 .\end{theorem}
\numberwithin{theorem}{subsection}
\section{Some Section}
\begin{theorem}\label{thm:K}This theorem should have number 4.1.0.1 .\end{theorem}
\begin{theorem}\label{thm:L}This theorem should have number 4.1.0.2 .\end{theorem}
\subsection{Some Subsection}
\begin{theorem}\label{thm:M}This theorem should have number 4.1.1.1 .\end{theorem}
\begin{theorem}\label{thm:N}This theorem should have number 4.1.1.2 .\end{theorem}
\numberwithin{theorem}{section}
\part{Fourth Part}
No theorems here.
\chapter{Fifth Chapter}
\begin{theorem}\label{thm:O}This theorem should have number 5.0.1 .\end{theorem}
\begin{theorem}\label{thm:P}This theorem should have number 5.0.2 .\end{theorem}
\numberwithin{theorem}{subsection}
\section{Some Section}
\begin{theorem}\label{thm:Q}This theorem should have number 5.1.0.1 .\end{theorem}
\begin{theorem}\label{thm:R}This theorem should have number 5.1.0.2 .\end{theorem}
\numberwithin{theorem}{subsubsection}
\subsection{Some Subsection}
\begin{theorem}\label{thm:S}This theorem should have number 5.1.1.0.1 .\end{theorem}
\begin{theorem}\label{thm:T}This theorem should have number 5.1.1.0.2 .\end{theorem}
\subsubsection{Some Subsubsection}
\begin{theorem}\label{thm:U}This theorem should have number 5.1.1.1.1 .\end{theorem}
\begin{theorem}\label{thm:V}This theorem should have number 5.1.1.1.2 .\end{theorem}
Now, let's reference all of them: \cref{thm:A,thm:B,thm:C,thm:D,thm:E,thm:F,thm:G,thm:H,thm:I,thm:J,thm:K,thm:L,thm:M,thm:N,thm:O,thm:P,thm:Q,thm:R,thm:S,thm:T,thm:U,thm:V}.
However, I expect Thms.\@ 1.1 -- 5.1.1.1.2'' here.
\end{document}


You get what you want:

Before part I:

In part I:

In part II:

In part III:

In part IV:

However, this method is error-prone: whenever you add, remove, or change any sectioning command (\subsubsection, \subsection, \section, \chapter, perhaps also \part), you need to check and perhaps adjust your \numberwithins.

In a better world, you won't even add \numberwithins but have all the complexity moved to the definitions of your sectioning commands (and, perhaps, theorem-like environments) in the preamble. Probably, the sectioning commands would write information about themselves to some auxiliary file and read from it again on the next run of {pdf|xe|lua}latex.
How to do this? I.e., how to adapt your sectioning commands (and, perhaps, theorem-like environments) such that they automatically check for the presence of future sectioning elements that lie deeper than the current sectioning element and issue the corresponding \numberwithins automatically? (To reduce the complexity of this task, please first don't think about the appendix and all the backmatter, which is usually present in books.) If it makes the task easier, you could even consider creating sectioning environments such as, e.g., \begin{myPart}{title}[short title]…\end{myPart}, …, \begin{mySubsubsection}{title}[short title]…\end{mySubSubsection} instead of traditional \part{title}[short title], …, \subsubsection{Title}[short title].

And, by the way, what has to be done so that cleveref compresses the list of theorems into a range? The option compress is present but has no visible effect.

EDIT: John's solution http://tex.stackexchange.com/a/564190 (as of Wed Dec 9 04:19:55 UTC 2020) affects the position of the margin stuff as follows. Consider the original file, say, mwe1.tex, containing

\documentclass[envcountsame,envcountsect]{svmono}%%% taken from https://www.springer.com/gp/authors-editors/book-authors-editors/your-publication-journey/manuscript-preparation
\usepackage{marginfix}%%% we do need it in a non-mwe
\usepackage{todonotes}
\usepackage{lipsum}
\begin{document}
\chapter{First chapter}
\lipsum[1]
\marginpar{Margin text}
\chapter{Second chapter}
Main text.\todo{Todo note}%
\end{document}


and its adaptation mwe2.tex, containing

\documentclass[envcountsame,envcountsect]{svmono}%%% taken from https://www.springer.com/gp/authors-editors/book-authors-editors/your-publication-journey/manuscript-preparation
\usepackage{marginfix}%%% we do need it in a non-mwe
\usepackage{todonotes}
\usepackage{lipsum}
%%% Making the enumeration style of the theorem-like environments autmomatically follow the numbering of ((sub-)sub-)sections and chapters in a logical way; cf. http://tex.stackexchange.com/a/564190 :
\newcommand{\newchapter}[2]{\expandafter\gdef\csname chapter#1\endcsname{#2}}% see aux file
\newcommand{\newsection}[2]{\expandafter\gdef\csname section#1\endcsname{#2}}
\newcommand{\newsubsection}[2]{\expandafter\gdef\csname subsection#1\endcsname{#2}}
\makeatletter
\newenvironment{Chapter}{\stepcounter{chapter}%
\@ifundefined{chapter\thechapter}{\numberwithin{theorem}{chapter}}%
{\numberwithin{theorem}{\csname chapter\thechapter\endcsname}}%
\chapter}{\par\ifnum\c@section>0%
\immediate\write\@auxout{\string\newchapter{\thechapter}{section}}%
\fi}
\newenvironment{Section}{\stepcounter{section}%
\@ifundefined{section\thesection}{\numberwithin{theorem}{section}}%
{\numberwithin{theorem}{\csname section\thesection\endcsname}}%
\section}{\par\ifnum\c@subsection>0%
\immediate\write\@auxout{\string\newsection{\thesection}{subsection}}%
\fi}
\newenvironment{Subsection}{\stepcounter{subsection}%
\@ifundefined{subsection\thesubsection}{\numberwithin{theorem}{subsection}}%
{\numberwithin{theorem}{\csname subsection\thesubsection\endcsname}}%
\subsection}{\par\ifnum\c@subsubsection>0%
\immediate\write\@auxout{\string\newsubsection{\thesubsection}{subsubsection}}%
\fi}
\makeatother
\begin{document}
\begin{Chapter}{First chapter}
\lipsum[1]
\marginpar{Margin text}
\end{Chapter}%
\begin{Chapter}{Second chapter}
Main text.\todo{Todo note}%
\end{Chapter}%
\end{document}


Running pdflatex on the two files in a loop till stabilization results in the following differences according to diffpdf (on the left we have the interesting output portion for mwe1.tex, on the right the interesting output portion for mwe2.tex):

As you see, applying the solution made the todo note go down. Whereas it might seem a small nuisance in the minimal working example, in a large book the change creates problems with the todo notes in the margin: they appear logically too low, even when they are not anywhere near the beginning or the end of a chapter or a section. Anyway, from a typesetter's viewpoint, a solution to a sectioning problem has to be (mostly) independent of the todo-notes placement anyhow.

• The aux file is LaTeX's form of time travel. The previous version is read at \begin{document}. The only question is what do you want to store and how? You might want to check out the refcount package. Commented Sep 25, 2020 at 4:05
• @JohnKormylo You want to store, intuitively speaking, the logical structure of the document. (Maybe, it suffices to read what is already stored in the toc file.) Then, for each call of a structuring macro on level i (i=0 is part, i=1 is chapter, i=2 is section, etc.) you wish to read from the aux file or toc file whether the next such command would be on level i or i-1 (in this case, you say, roughly, \numberwithin{theorem}{current level}) or on level i+1 (in this case, you say, roughly, \numberwithin{theorem}{one deeper level}). I'm afraid I don't really know how to (la)tex this properly.
– user224332
Commented Sep 25, 2020 at 11:00
• Are you really sure that your readers will understand this complicated system? Commented Dec 9, 2020 at 9:03
• @egreg It's not complicated. You simply wish to make sure that the numbers of the theorem-like environments do not duplicate themselves or the numbers of anything else, that the number of a theorem-like environment tells you the deepest enclosing numbered logical ((sub-)sub-)section or chapter, and that the numbers are economical, not including any unnecessary zeroes.
– user224332
Commented Dec 9, 2020 at 16:21

this shows how to use the aux file. To get rid of all the \postchapter, \postsection and \postsubsection entries, add them to every command that can start a new one, including \AtEndDocument.

Getting rid of \startchapter etc commands is trickier since \thechapter is not valid until somewhere deep in the code, and due to the way \chapter etc. passes parameters you can't just add it to the end.

\documentclass[envcountsame,envcountsect]{svmono}%%% from

http://www.springer.com/gp/authors-editors/book-authors-editors/resources-guidelines/book-manuscript-guidelines/manuscript-preparation/5636
\PassOptionsToPackage{mainaux,starttoc,index,glossary}{rerunfilecheck}
\usepackage[compress]{cleveref}
\crefname{theorem}{Thm.}{Thms.}%%% Abbreviate in the middle of a sentence.
\Crefname{theorem}{Theorem}{Theorems}%%% No abbreviation should start any sentence.
\crefrangeformat{theorem}{Thms.\@ #3#1#4 -- #5#2#6}%%% Allow for line breaks everywhere.
\Crefrangeformat{theorem}{Theorems #3#1#4 -- #5#2#6}%%% Allow for line breaks everywhere; no abbreviation should start any sentence.

\makeatletter
\newcommand{\postchapter}{\ifnum\c@section>0
\immediate\write\@auxout{\string\newchapter{\thechapter}{section}}
\fi}
\newcommand{\newchapter}[2]{\expandafter\gdef\csname chapter#1\endcsname{#2}}% at \begin{document}
\newcommand\startchapter{\@ifundefined{chapter\thechapter}{\numberwithin{theorem}{chapter}}%
{\numberwithin{theorem}{\csname chapter\thechapter\endcsname}}\ignorespaces}

\newcommand{\postsection}{\ifnum\c@subsection>0
\immediate\write\@auxout{\string\newsection{\thesection}{subsection}}
\fi}
\newcommand{\newsection}[2]{\expandafter\gdef\csname section#1\endcsname{#2}}% at \begin{document}
\newcommand\startsection{\@ifundefined{section\thesection}{}%
{\numberwithin{theorem}{\csname section\thesection\endcsname}}\ignorespaces}

\newcommand{\postsubsection}{\ifnum\c@subsubsection>0
\immediate\write\@auxout{\string\newsubsection{\thesubsection}{subsubsection}}
\fi}
\newcommand{\newsubsection}[2]{\expandafter\gdef\csname subsection#1\endcsname{#2}}% at \begin{document}
\newcommand\startsubsection{\@ifundefined{subsection\thesubsection}{}%
{\numberwithin{theorem}{\csname subsection\thesubsection\endcsname}}\ignorespaces}
\makeatother

\begin{document}
\csname subsection5.1.1\csname
\chapter{Introduction}
\startchapter
\begin{theorem}\label{thm:A}This theorem should have number 1.1 .\end{theorem}
\begin{theorem}\label{thm:B}This theorem should have number 1.2 .\end{theorem}
\postchapter
\part{First Part}
No theorems here.
\chapter{Second Chapter}
\startchapter
\begin{theorem}\label{thm:C}This theorem should have number 2.1 .\end{theorem}
\begin{theorem}\label{thm:D}This theorem should have number 2.2 .\end{theorem}
\postchapter
\part{Second Part}
No theorems here.
\chapter{Third Chapter}
\startchapter
\begin{theorem}\label{thm:E}This theorem should have number 3.0.1 .\end{theorem}
\begin{theorem}\label{thm:F}This theorem should have number 3.0.2 .\end{theorem}
\section{Some Section}
\startsection
\begin{theorem}\label{thm:G}This theorem should have number 3.1.1 .\end{theorem}
\begin{theorem}\label{thm:H}This theorem should have number 3.1.2 .\end{theorem}
\postsection
\postchapter
\part{Third Part}
No theorems here.
\chapter{Fourth Chapter}
\startchapter
\begin{theorem}\label{thm:I}This theorem should have number 4.0.1 .\end{theorem}
\begin{theorem}\label{thm:J}This theorem should have number 4.0.2 .\end{theorem}
\section{Some Section}
\startsection
\begin{theorem}\label{thm:K}This theorem should have number 4.1.0.1 .\end{theorem}
\begin{theorem}\label{thm:L}This theorem should have number 4.1.0.2 .\end{theorem}
\subsection{Some Subsection}
\startsubsection
\begin{theorem}\label{thm:M}This theorem should have number 4.1.1.1 .\end{theorem}
\begin{theorem}\label{thm:N}This theorem should have number 4.1.1.2 .\end{theorem}
\postsubsection
\postsection
\postchapter
\part{Fourth Part}
No theorems here.
\chapter{Fifth Chapter}
\startchapter
\begin{theorem}\label{thm:O}This theorem should have number 5.0.1 .\end{theorem}
\begin{theorem}\label{thm:P}This theorem should have number 5.0.2 .\end{theorem}
\section{Some Section}
\startsection
\begin{theorem}\label{thm:Q}This theorem should have number 5.1.0.1 .\end{theorem}
\begin{theorem}\label{thm:R}This theorem should have number 5.1.0.2 .\end{theorem}
\subsection{Some Subsection}
\startsubsection
\begin{theorem}\label{thm:S}This theorem should have number 5.1.1.0.1 .\end{theorem}
\begin{theorem}\label{thm:T}This theorem should have number 5.1.1.0.2 .\end{theorem}
\subsubsection{Some Subsubsection}
\begin{theorem}\label{thm:U}This theorem should have number 5.1.1.1.1 .\end{theorem}
\begin{theorem}\label{thm:V}This theorem should have number 5.1.1.1.2 .\end{theorem}
\postsubsection
\postsection
\postchapter
Now, let's reference all of them: \cref{thm:A,thm:B,thm:C,thm:D,thm:E,thm:F,thm:G,thm:H,thm:I,thm:J,thm:K,thm:L,thm:M,thm:N,thm:O,thm:P,thm:Q,thm:R,thm:S,thm:T,thm:U,thm:V}.
However, I expect Thms.\@ 1.1 -- 5.1.1.1.2'' here.
\end{document}


This version is implemented using environments.

\documentclass[envcountsame,envcountsect]{svmono}%%% from http://www.springer.com/gp/authors-editors/book-authors-editors/resources-guidelines/book-manuscript-guidelines/manuscript-preparation/5636
\PassOptionsToPackage{mainaux,starttoc,index,glossary}{rerunfilecheck}
\usepackage[compress]{cleveref}
\crefname{theorem}{Thm.}{Thms.}%%% Abbreviate in the middle of a sentence.
\Crefname{theorem}{Theorem}{Theorems}%%% No abbreviation should start any sentence.
\crefrangeformat{theorem}{Thms.\@ #3#1#4 -- #5#2#6}%%% Allow for line breaks everywhere.
\Crefrangeformat{theorem}{Theorems #3#1#4 -- #5#2#6}%%% Allow for line breaks everywhere; no abbreviation should start any sentence.

\newcommand{\newchapter}[2]{\expandafter\gdef\csname chapter#1\endcsname{#2}}% see aux file
\newcommand{\newsection}[2]{\expandafter\gdef\csname section#1\endcsname{#2}}
\newcommand{\newsubsection}[2]{\expandafter\gdef\csname subsection#1\endcsname{#2}}

\makeatletter
\newenvironment{Chapter}{\stepcounter{chapter}%
\@ifundefined{chapter\thechapter}{\numberwithin{theorem}{chapter}}%
{\numberwithin{theorem}{\csname chapter\thechapter\endcsname}}%
\chapter}{\par\ifnum\c@section>0
\immediate\write\@auxout{\string\newchapter{\thechapter}{section}}
\fi}

\newenvironment{Section}{\stepcounter{section}%
\@ifundefined{section\thesection}{\numberwithin{theorem}{section}}%
{\numberwithin{theorem}{\csname section\thesection\endcsname}}%
\section}{\par\ifnum\c@subsection>0
\immediate\write\@auxout{\string\newsection{\thesection}{subsection}}
\fi}

\newenvironment{Subsection}{\stepcounter{subsection}%
\@ifundefined{subsection\thesubsection}{\numberwithin{theorem}{subsection}}%
{\numberwithin{theorem}{\csname subsection\thesubsection\endcsname}}%
\subsection}{\par\ifnum\c@subsubsection>0
\immediate\write\@auxout{\string\newsubsection{\thesubsection}{subsubsection}}
\fi}
\makeatother

\begin{document}
\begin{Chapter}[short title]{Introduction}
\begin{theorem}\label{thm:A}This theorem should have number 1.1 .\end{theorem}
\begin{theorem}\label{thm:B}This theorem should have number 1.2 .\end{theorem}
\end{Chapter}

\part{First Part}
No theorems here.
\begin{Chapter}{Second Chapter}
\begin{theorem}\label{thm:C}This theorem should have number 2.1 .\end{theorem}
\begin{theorem}\label{thm:D}This theorem should have number 2.2 .\end{theorem}
\end{Chapter}
\part{Second Part}
No theorems here.
\begin{Chapter}{Third Chapter}
\begin{theorem}\label{thm:E}This theorem should have number 3.0.1 .\end{theorem}
\begin{theorem}\label{thm:F}This theorem should have number 3.0.2 .\end{theorem}
\begin{Section}{Some Section}
\begin{theorem}\label{thm:G}This theorem should have number 3.1.1 .\end{theorem}
\begin{theorem}\label{thm:H}This theorem should have number 3.1.2 .\end{theorem}
\end{Section}
\end{Chapter}

\part{Third Part}
No theorems here.
\begin{Chapter}{Fourth Chapter}
\begin{theorem}\label{thm:I}This theorem should have number 4.0.1 .\end{theorem}
\begin{theorem}\label{thm:J}This theorem should have number 4.0.2 .\end{theorem}
\begin{Section}{Some Section}
\begin{theorem}\label{thm:K}This theorem should have number 4.1.0.1 .\end{theorem}
\begin{theorem}\label{thm:L}This theorem should have number 4.1.0.2 .\end{theorem}
\begin{Subsection}{Some Subsection}
\begin{theorem}\label{thm:M}This theorem should have number 4.1.1.1 .\end{theorem}
\begin{theorem}\label{thm:N}This theorem should have number 4.1.1.2 .\end{theorem}
\end{Subsection}
\end{Section}
\end{Chapter}

\part{Fourth Part}
No theorems here.
\begin{Chapter}{Fifth Chapter}
\begin{theorem}\label{thm:O}This theorem should have number 5.0.1 .\end{theorem}
\begin{theorem}\label{thm:P}This theorem should have number 5.0.2 .\end{theorem}
\begin{Section}{Some Section}
\begin{theorem}\label{thm:Q}This theorem should have number 5.1.0.1 .\end{theorem}
\begin{theorem}\label{thm:R}This theorem should have number 5.1.0.2 .\end{theorem}
\begin{Subsection}{Some Subsection}
\begin{theorem}\label{thm:S}This theorem should have number 5.1.1.0.1 .\end{theorem}
\begin{theorem}\label{thm:T}This theorem should have number 5.1.1.0.2 .\end{theorem}
\subsubsection{Some Subsubsection}
\begin{theorem}\label{thm:U}This theorem should have number 5.1.1.1.1 .\end{theorem}
\begin{theorem}\label{thm:V}This theorem should have number 5.1.1.1.2 .\end{theorem}
\end{Subsection}
\end{Section}
\end{Chapter}
Now, let's reference all of them: \cref{thm:A,thm:B,thm:C,thm:D,thm:E,thm:F,thm:G,thm:H,thm:I,thm:J,thm:K,thm:L,thm:M,thm:N,thm:O,thm:P,thm:Q,thm:R,thm:S,thm:T,thm:U,thm:V}.
However, I expect Thms.\@ 1.1 -- 5.1.1.1.2'' here.
\end{document}

• Not a problem, just don't reuse old names. Commented Sep 26, 2020 at 0:31
• While testing your environment solution in the context of a larger book, I ran over a problem. I edited my question to make the problem clear. Could I kindly ask you to take a look and repair/adapt your solution?
– user224332
Commented Dec 8, 2020 at 18:53
• Oops, I left out the default setting for section and subsection. Now fixed. Commented Dec 8, 2020 at 22:49
• Thank you. Is there a formatting problem in your answer? As for the environment version, I see a portion of LaTeX code, followed by \makeatother in plain text, followed by LaTeX code again. Anyway, I extracted the code before \makeatother, discovered yet another issue and edited my question accordingly. Might I kindly ask you to take a look and repair/adapt your solution?
– user224332
Commented Dec 9, 2020 at 4:45