4

My goal is to display the number of sections the chapter contains, at the beginning of every chapter.

Currently, my idea is to store in the aux file the value of the section counter at the end of every chapter, and display it on the second pass. Example of the commands I wrote:

\newcommand{\store@sect@count}{\write\@auxout{\@backslashchar expandafter\@backslashchar gdef\@backslashchar csname sects@in@chap@\roman{chapter}\@backslashchar endcsname{\arabic{section}}}}
% Writes something like \expandafter\gdef\csname sects@in@chap@i\endcsname{2}

\newcommand{\get@sect@count}{\csname sects@in@chap@\roman{chapter}\endcsname}

With this approach, I can use \get@sect@count at the begining of every chapter to print the section count. However, it also requires running the \store@sect@count command at the end of every chapter, and I haven't found how to do so.

A possibility would be to create a custom environment for this, however this would end up very cumbersome, especially if I apply it to other sectioning levels.

  • How can I reliably run a command at the end of every chapter (and other levels of document hierarchy)?
  • Is there a better approach to reach my goal, than the one I am currently trying to use?

Edit: MWE on Overleaf: https://www.overleaf.com/read/hcqjhwbthngs (code below)

\documentclass{book}
\usepackage[utf8]{inputenc}

\makeatletter
\newcommand{\store@sect@count}{\write\@auxout{\@backslashchar expandafter\@backslashchar gdef\@backslashchar csname sects@in@chap@\roman{chapter}\@backslashchar endcsname{\arabic{section}}}}
\newcommand{\get@sect@count}{\csname sects@in@chap@\roman{chapter}\endcsname}


\newenvironment
{countingchapter}
{\chapter{} Number of sections: \get@sect@count \\}
{\store@sect@count}

\newenvironment
{countingsection}
{\section{}}
{}
\makeatother

\begin{document}

\begin{countingchapter}
My chapter

\begin{countingsection}
Fisrt section
\end{countingsection}
\begin{countingsection}
Section section
\end{countingsection}
\begin{countingsection}
Third section
\end{countingsection}

\end{countingchapter}

\end{document}
2
  • Is this possible to post MWE of your code for better understanding? Happy New Year 2021
    – MadyYuvi
    Commented Dec 31, 2020 at 13:15
  • there is no dedicated command at the end of a chapter, but you could rather easily hook into the begin of the next chapter. But if you have a table of contents you could also hook into it, it has all the chapters and section and so counting them should be easy. Commented Dec 31, 2020 at 14:16

2 Answers 2

5

You can hook in \refstepcounter (with care), so that if it is performed on chapter the number of sections will be recorded in the .aux file.

Special care for appendices has to be taken and also for the last chapter, so the appropriate record is added when \appendix is called and also at end document.

Caveat: \backmatter would break this.

\documentclass[oneside]{book} % oneside just to make a smaller picture
\usepackage[a6paper]{geometry} % just to make a smaller picture
\usepackage{xpatch}
\usepackage{hyperref}

\ExplSyntaxOn
\AtBeginDocument
 {
  \NewCommandCopy{\latexrefstepcounter}{\refstepcounter}
  \cs_set_eq:NN \refstepcounter \countsections_refstepcounter:n
 }
\AtEndDocument
 {
  \countsections_count:
 }
\xpretocmd{\appendix}{\countsections_count:}{}{}

\NewDocumentCommand{\sectioncount}{O{\thechapter}}
 {
  \ref*{cs@#1}
 }

\cs_new_protected:Nn \countsections_refstepcounter:n
 {
  \str_if_eq:nnT { #1 } { chapter } { \countsections_count: }
  \latexrefstepcounter{#1}
 }

\cs_new_protected:Nn \countsections_count:
 {
  \iow_now:cx { @auxout }
   {
    \string\newlabel { cs@\thechapter } { {\arabic{section}}{}{}{}{} }
   }
 }
\ExplSyntaxOff

\begin{document}

Chapter 1 has \sectioncount[1]~sections.
Chapter 2 has \sectioncount[2]~sections.
Chapter A has \sectioncount[A]~sections.

\chapter{A}

This chapter has \sectioncount~sections.

\section{B}
\section{C}

\chapter{D}

This chapter has \sectioncount~sections.

\section{E}
\section{F}
\section{G}

\appendix

\chapter{H}

This chapter has \sectioncount~sections.

\section{I}
\section{J}

\end{document}

You may need to load xparse if you're running a LaTeX release prior to 2020-10-01.

enter image description here

1

As Ulrike mentioned, you can tap into \chapter and write the number of sections to the .aux with every call to start a new chapter.

enter image description here

\documentclass{book}

\usepackage{lipsum}

\makeatletter
\let\oldchapter\chapter% Store \chapter in \oldchapter
\renewcommand{\chapter}{%
  \immediate\write\@auxout{% Write section count to .aux
    \global\noexpand\@namedef{chap\roman{chapter}sectioncount}{\arabic{section}}%
  }%
  \oldchapter% Call original \chapter
}

\newcommand{\thischaptersectioncount}{%
  This chapter contains \@nameuse{chap\roman{chapter}sectioncount} sections.%
}
\makeatother

\begin{document}

\sloppy

\chapter{First chapter}
\thischaptersectioncount% 5
\section{First section}\lipsum[1-10]
\section{Second section}\lipsum[11-20]
\section{Third section}\lipsum[21-30]
\section{Fourth section}\lipsum[31-40]
\section{Final section}\lipsum[41-50]

\chapter{Second chapter}
\thischaptersectioncount% 2
\section{First section}\lipsum[1-10]
\section{Second section}\lipsum[11-20]

\chapter{Third chapter}
\thischaptersectioncount% 4
\section{First section}\lipsum[1-10]
\section{Second section}\lipsum[11-20]
\section{Third section}\lipsum[21-30]
\section{Fourth section}\lipsum[31-40]

\chapter{Final chapter}
\thischaptersectioncount% 1
\section{First section}\lipsum[1-10]

\end{document}

Since you're using the .aux as an intermediate storing mechanism, you'll have to compile at least twice (on the first go or with any changes in the number of sections), as the section numbers will only be available on the second compilation.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .