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
\usepackage{mathtools}%%% loads amsmath internally
\PassOptionsToPackage{mainaux,starttoc,index,glossary}{rerunfilecheck}
\usepackage[unicode,hidelinks]{hyperref}
\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 \numberwithin
s.
In a better world, you won't even add \numberwithin
s 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 \numberwithin
s 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{mathtools}%%% loads amsmath internally
\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{mathtools}%%% loads amsmath internally
\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}}%
\addtocounter{chapter}{-1}%
\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}}%
\addtocounter{section}{-1}%
\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}}%
\addtocounter{subsection}{-1}%
\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.
\begin{document}
. The only question is what do you want to store and how? You might want to check out the refcount package.\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.