# Chapter titles as hyperlinks in the list-of-figures

I would like to include the chapter titles in my list of figures. @GonzaloMedina previously answered this question using titletoc. However, not all my chapters are numbered, so I would like the chapter titles in the lof to be shown just as they are in the toc.

This is Gonzalo Medina's preamble:

\documentclass{report}

\usepackage{chngcntr}
\counterwithout{figure}{chapter}

\usepackage{etoolbox}  % or xpatch
\usepackage{titletoc}

\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


And creating the following document:

\begin{document}

\tableofcontents
\listoffigures

\setcounter{secnumdepth}{-1}
\chapter{Preface with a figure}
\setcounter{chapter}{0}
\setcounter{secnumdepth}{2}

\begin{figure}
\caption{caption text}
\end{figure}

\chapter{Test Chapter with Figures}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{figure}
\caption{caption text}
\end{figure}
\end{document}


yields

and

My questions are

1. How to surpress the preface's chapter number to be shown?
2. How to combine this with the hyperref package to let the chapters in the lof be hyperlinks?
-
This here seems interesting: latex-community.org/forum/… –  Keks Dose Oct 17 '13 at 17:53

The example controls the chapter numbering by counter secnumdepth. It also be used to define \thischapternumber with a special value (empty macro = \@empty), if the chapter number should be suppressed. The code in \addtocontents checks \thischapternumber to suppress \numberline if necessary.

The changes for hyperref are small. The anchor is remembered in \thischapterHref and used in \contentsline:

\documentclass{report}

\usepackage{chngcntr}
\counterwithout{figure}{chapter}

\usepackage{etoolbox}  % or xpatch
\usepackage{titletoc}

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

\apptocmd{\@chapter}{%
\gdef\thischaptertitle{#1}%
\ifnum\value{secnumdepth}>-1 %
\gdef\thischapternumber{\thechapter}%
\else
\global\let\thischapternumber\@empty
\fi
\global\let\thischapterHref\@currentHref % hyperref
\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\contentsline{chapter}{%
\ifx\thischapternumber\@empty
\else
\protect\numberline{\thischapternumber}%
\fi
\thischaptertitle
}{}{%
\thischapterHref % hyperref
}%
}%
\global\togglefalse{noFigs}%
}{}%
}%
}
\makeatother

\begin{document}

\tableofcontents
\listoffigures

\setcounter{secnumdepth}{-1}
\chapter{Preface with a figure}
\setcounter{chapter}{0}
\setcounter{secnumdepth}{2}

\begin{figure}
\caption{caption text}
\end{figure}

\chapter{Test Chapter with Figures}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{figure}
\caption{caption text}
\end{figure}
\end{document}


-
How can I control the page breaks in the list of figures? Simply \addtocontents{lof}{\clearpage} didn't do it... –  D.Roepo Dec 2 '13 at 15:35
@D.Roepo: You can put \addtocontents{lof}{\protect\clearpage} inside a figure environment before or after the caption, where you want to have the page break. –  Heiko Oberdiek Dec 4 '13 at 16:07