# Include chapters in List of Figures with titletoc?

I'm working on a book using titlesec&titletoc and want to include chapters (with their numbers and names, but no page number) in the list of figures, if and only if they contain figures. Manually adding

\addtocontents{lof}{\contentsline {chapter}{\numberline {\thechapter} {#1}}{} }}


after each chapter works (#1 is the chaptername), and I could also fix the alignment (not part of MWE), but I couldn't append this to \chapter.

Can I get titletoc to include chapters into the list of figures?
Or how do I modify \chapter to add themselves to the lof?

\documentclass[a4paper,12pt]{book}

\usepackage[pagestyles,psfloats,clearempty,newlinetospace]{titlesec}
\usepackage{titletoc}

\titleformat{\chapter}{\huge\bfseries\filcenter}{\vspace{1cm}\thechapter}{0.5cm}{}[]

%\let\Oldchapter\chapter
%{\contentsline {chapter}{\numberline {\thechapter} {#1}}{} }}

\begin{document}

\tableofcontents
\listoffigures

\mainmatter

\chapter{Introduction}
\section{First}

\chapter{Has Figures}
\begin{figure}
\rule{1cm}{1cm}
\caption{caption text}
\end{figure}

\end{document}


The code below illustrates a possible solution; the basic idea is to let the figure environment do the job and decide if the chapter info will be written to the ToC; this is accomplished by using \AtBeginEnvironment (from the etoolbox or xpatch packages); whenever the figure environment is used in the document, it will examine a boolean flag; if the boolean is false, then it adds the chapter information to the LoF and sets the boolean to true; otherwise, it does not add any information.

The \@chapter command was also modified to reset the boolean and to globally store the information about the corresponding title and number; this information will then be used when writing to the LoF.

\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}{
{\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 no Figures}

\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}


The list of figures obtained:

### Remarks:

1. The initial solution was simplified thanks to a comment by Markus Schmassmann.
2. As lockstep mentions in his comment, further refinement would be needed in case one uses a continuous figure numbering scheme. Done by Markus Schmassmann.
3. When using hyperref, the package must be loaded after the lines \apptocmd{\@chapter}....
• @GonzaloMedina You should add a hint that further refinement would be needed in case one uses a continuous figure numbering scheme. Anyway, great answer! Apr 20, 2012 at 23:58
• This be simplified by deleting \newcommand*\l@chapterinfo & \def\@nodottedtocline and replacing \addcontentsline by \addtocontents{lof}{\contentsline {chapter}{\numberline {\thischapternumber} {\thischaptertitle}}{} }. titletoc takes care of the formating. Apr 21, 2012 at 3:46
• @MarkusSchmassmann: you're right. I incorporated your suggestion into my answer. Thank you. Apr 21, 2012 at 4:19
• @GonzaloMedina: I forgot to \protect some commands, otherwise the lof won't be human readable. Apr 21, 2012 at 15:05
• @MarkusSchmassmann: yes. I've also added an extra pair of braces to the \addtocontents to make it compatible with hyperref. Apr 21, 2012 at 15:21

Another way to handle this that doesn't require individually modifying each environment you need (figure, table, algorithm, listing, ...) is to instead patch \addcontentsline. A slight modification of Gonzalo's solution:

\documentclass{book}
\usepackage{etoolbox}

\makeatletter
\def\thischaptertitle{}
\apptocmd{\@chapter}{\gdef\thischaptertitle{#1}}{}{}

\newcommand{\DeclareDividedList}[1]%
{\newcounter{#1@chapter}\setcounter{#1@chapter}{0}}

{\ifltxcounter{#1@chapter}%
{%
\ifnumgreater{\thechapter}{\value{#1@chapter}}{%
\setcounter{#1@chapter}{\thechapter}%
{\protect\numberline {\thechapter} {\thischaptertitle}}{}{} }
}{}%
}{}%
}{}{}
\makeatother

\DeclareDividedList{lof}
\DeclareDividedList{lot}

\begin{document}

\tableofcontents
\listoffigures
\listoftables

\mainmatter

\chapter{Introduction with no Figures}

\chapter{Test Chapter with Figures but no Tables}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{figure}
\caption{caption text}
\end{figure}

\chapter{Test Chapter with Tables but no Figures}
\begin{table}
\caption{caption text}
\end{table}
\begin{table}
\caption{caption text}
\end{table}

\chapter{Test Chapter with Figures and Tables}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{table}
\caption{caption text}
\end{table}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{table}
\caption{caption text}
\end{table}
\begin{figure}
\caption{caption text}
\end{figure}

\end{document}


This gives you chapter headings in both the list of figures and list of tables:

If you don't use the \DeclareDividedList for one of them, say lot, then the counter \lot@chapter isn't created, and the \ifltxcounter switch will fail and the added code is not run when creating lines in the list of tables; the list will be typeset exactly as normal, without chapter headers.

Just as with the previous examples, this tends to break with unnumbered chapters.

• Would you see the problem what I have for appendices, in the following question: tex.stackexchange.com/questions/549556/… ...All this time after ! Jun 15, 2020 at 16:14
• You need to use \value{chapter} instead of \thechapter when using \ifnumgreater  and \setcounter (and watch out for the chapter numbering resetting in the appendix). Jun 15, 2020 at 18:28

More of a comment than an answer actually (sorry, new account no cred' yet). With minor changes, the code proposed also works for adding chapters in a LoT by similarly modding the table environment.

\documentclass{book} \usepackage{etoolbox}  % or xpatch

\makeatletter % initial definitions of the chapter info (name and number) \def\thischaptertitle{}\def\thischapternumber{} \newtoggle{noTabs}

\apptocmd{\@chapter}%   {\gdef\thischaptertitle{#1}\gdef\thischapternumber{\thechapter}%
\global\toggletrue{noTabs}}{}{}

% the table 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{table}{%
\iftoggle{noTabs}{
{\protect\numberline {\thischapternumber} {\thischaptertitle}}{}{} }
\global\togglefalse{noTabs}
}{}   }% }

\makeatother

\begin{document}

\tableofcontents \listoftables

\mainmatter

\chapter{Introduction with no Tables}

\chapter{Test Chapter with Tables} \begin{table} \caption{caption text} \end{table} \begin{table} \caption{caption text} \end{table}

\chapter{Test Chapter with no Tables}

\chapter{Another Test Chapter with Tables} \begin{table} \caption{caption text} \end{table} \begin{table} \caption{caption text} \end{table} \begin{table} \caption{caption text} \end{table} \begin{table} \caption{caption text} \end{table} \begin{table} \caption{caption text} \end{table}

\end{document}


Unfortunately I can't post a snapshot either...

Anyway, great post, exactly what I was looking for!