TransWikia.com

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

TeX - LaTeX Asked by GeekestGeek on June 22, 2021

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:

intro, chapter 1

In part I:

part I, chapter 2

In part II:

part II, chapter 3

In part III:

part III, chapter 4

In part IV:

part IV, chapter 5

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{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]{expandaftergdefcsname chapter#1endcsname{#2}}% see aux file
newcommand{newsection}[2]{expandaftergdefcsname section#1endcsname{#2}}
newcommand{newsubsection}[2]{expandaftergdefcsname subsection#1endcsname{#2}}
makeatletter
newenvironment{Chapter}{stepcounter{chapter}%
  @ifundefined{chapterthechapter}{numberwithin{theorem}{chapter}}%
    {numberwithin{theorem}{csname chapterthechapterendcsname}}%
  addtocounter{chapter}{-1}%
chapter}{parifnumc@section>0%
  immediatewrite@auxout{stringnewchapter{thechapter}{section}}%
fi}
newenvironment{Section}{stepcounter{section}%
  @ifundefined{sectionthesection}{numberwithin{theorem}{section}}%
    {numberwithin{theorem}{csname sectionthesectionendcsname}}%
  addtocounter{section}{-1}%
section}{parifnumc@subsection>0%
  immediatewrite@auxout{stringnewsection{thesection}{subsection}}%
fi}
newenvironment{Subsection}{stepcounter{subsection}%
  @ifundefined{subsectionthesubsection}{numberwithin{theorem}{subsection}}%
    {numberwithin{theorem}{csname subsectionthesubsectionendcsname}}%
  addtocounter{subsection}{-1}%
subsection}{parifnumc@subsubsection>0%
  immediatewrite@auxout{stringnewsubsection{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):

differences in the output

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.

One Answer

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
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.

makeatletter
newcommand{postchapter}{ifnumc@section>0
  immediatewrite@auxout{stringnewchapter{thechapter}{section}}
fi}
newcommand{newchapter}[2]{expandaftergdefcsname chapter#1endcsname{#2}}% at begin{document}
newcommandstartchapter{@ifundefined{chapterthechapter}{numberwithin{theorem}{chapter}}%
  {numberwithin{theorem}{csname chapterthechapterendcsname}}ignorespaces}
  
newcommand{postsection}{ifnumc@subsection>0
  immediatewrite@auxout{stringnewsection{thesection}{subsection}}
fi}
newcommand{newsection}[2]{expandaftergdefcsname section#1endcsname{#2}}% at begin{document}
newcommandstartsection{@ifundefined{sectionthesection}{}%
  {numberwithin{theorem}{csname sectionthesectionendcsname}}ignorespaces}
  
newcommand{postsubsection}{ifnumc@subsubsection>0
  immediatewrite@auxout{stringnewsubsection{thesubsection}{subsubsection}}
fi}
newcommand{newsubsection}[2]{expandaftergdefcsname subsection#1endcsname{#2}}% at begin{document}
newcommandstartsubsection{@ifundefined{subsectionthesubsection}{}%
  {numberwithin{theorem}{csname subsectionthesubsectionendcsname}}ignorespaces}
makeatother

begin{document}
csname subsection5.1.1csname
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
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.

newcommand{newchapter}[2]{expandaftergdefcsname chapter#1endcsname{#2}}% see aux file
newcommand{newsection}[2]{expandaftergdefcsname section#1endcsname{#2}}
newcommand{newsubsection}[2]{expandaftergdefcsname subsection#1endcsname{#2}}

makeatletter
newenvironment{Chapter}{stepcounter{chapter}%
  @ifundefined{chapterthechapter}{numberwithin{theorem}{chapter}}%
    {numberwithin{theorem}{csname chapterthechapterendcsname}}%
  addtocounter{chapter}{-1}%
chapter}{parifnumc@section>0
  immediatewrite@auxout{stringnewchapter{thechapter}{section}}
fi}


newenvironment{Section}{stepcounter{section}%
  @ifundefined{sectionthesection}{numberwithin{theorem}{section}}%
    {numberwithin{theorem}{csname sectionthesectionendcsname}}%
  addtocounter{section}{-1}%
section}{parifnumc@subsection>0
  immediatewrite@auxout{stringnewsection{thesection}{subsection}}
fi}

newenvironment{Subsection}{stepcounter{subsection}%
  @ifundefined{subsectionthesubsection}{numberwithin{theorem}{subsection}}%
    {numberwithin{theorem}{csname subsectionthesubsectionendcsname}}%
  addtocounter{subsection}{-1}%
subsection}{parifnumc@subsubsection>0
  immediatewrite@auxout{stringnewsubsection{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}

Answered by John Kormylo on June 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP