有没有一种简单的方法可以测试章节是否有节?问题与每章开头显示的简短目录有关,如果当前章节没有节,则该目录为空。
答案1
我不太清楚您在寻找什么。您可以为每个章节定义一个布尔值/切换,如果章节有部分,则将其设置为 true。我会通过重新定义\section
来翻转切换来实现这一点。您还需要重新定义章节来创建切换。
\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}