6

Is there an easy way to test if a chapter has sections? The question is related to short tables of contents displayed at the beginning of each chapter and that would be empty when the current chapter has no sections.

4

It is not completely clear to me what you are looking for. You can define a boolean/toggle for each chapter and set it to true if the chapter has sections. I would do this by redefining \section to flip the toggle. You also need to redefine chapter to create the toggle.

\documentclass{book}

\usepackage{etoolbox}

\pretocmd{\chapter}{\numdef\chapnum{\thechapter+1}\newtoggle{chapsec\chapnum}}{}{}
\pretocmd{\section}{\toggletrue{chapsec\thechapter}}{}{}

\begin{document}
\chapter{a chapter with sections}
\section{a section}
\section{another section}
\chapter{a chapter with no sections}
\chapter{another chapter with sections}
\section{a section}
\section{another section}
\chapter{a chapter with no sections}
\chapter{another chapter with sections}
\section{a section}
\section{another section}

Chapter 1 has \iftoggle{chapsec1}{}{no} sections

Chapter 2 has \iftoggle{chapsec2}{}{no} sections

Chapter 3 has \iftoggle{chapsec3}{}{no} sections

Chapter 4 has \iftoggle{chapsec4}{}{no} sections

Chapter 5 has \iftoggle{chapsec5}{}{no} sections
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.