I am using the code in Include chapters in List of Figures with titletoc? to include chapters in the LOF. The problem is that some chapters may have at most one figure but with no caption. How to ignore these chapters?
Blew is the code:
\documentclass{book}
\usepackage{etoolbox} % or xpatch
\makeatletter
% initial definitions of the chapter info (name and number)
\def\thischaptertitle{}\def\thischapternumber{}
\newtoggle{noFigs}
\apptocmd{\@chapter}%
{\gdef\thischaptertitle{#1}\gdef\thischapternumber{\thechapter}%
\global\toggletrue{noFigs}}{}{}
% the figure environment does the job: the first time it is used after a \chapter command,
% it writes the information of the chapter to the LoF
\AtBeginDocument{%
\AtBeginEnvironment{figure}{%
\iftoggle{noFigs}{
\addtocontents{lof}{\protect\contentsline {chapter}%
{\protect\numberline {\thischapternumber} {\thischaptertitle}}{}{} }
\global\togglefalse{noFigs}
}{}
}%
}
\makeatother
\begin{document}
\tableofcontents
\listoffigures
\mainmatter
\chapter{Introduction with no Figures}
\chapter{Test Chapter with Figures}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{figure}
\caption{caption text}
\end{figure}
\chapter{Test Chapter with only one Figure and no caption}
\begin{figure}
\rule{1cm}{1cm}
\end{figure}
\chapter{Another Test Chapter with Figures}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{figure}
\caption{caption text}
\end{figure}
\end{document}
Best Answer
I've reused the code from my answer to this question Add both part and chapter titles to list of figures with formatting and line wrapping
and just added a condition if
figure
counter is greater than zero, a\caption
has been used (most likely). Clearly, this requires a per chapter resetting of the figure counter, which is true in standard classes (and unless not changed withchngcntr
or\@remfromreset
)Note that the chapters without figures or with figures but without caption, they aren't added to the LoF!