4

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}{
       \addtocontents{lof}{\protect\contentsline {chapter}%
        {\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

enter image description here

and

enter image description here

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?
1

1 Answer 1

5

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}{%
      \addtocontents{lof}{%
        \protect\contentsline{chapter}{%
          \ifx\thischapternumber\@empty
          \else
            \protect\numberline{\thischapternumber}%
          \fi
          \thischaptertitle
        }{}{%
          \thischapterHref % hyperref
        }%
      }%
      \global\togglefalse{noFigs}%
    }{}%
  }%
}
\makeatother

\usepackage[colorlinks]{hyperref}

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

Result

2
  • How can I control the page breaks in the list of figures? Simply \addtocontents{lof}{\clearpage} didn't do it...
    – D.Roepo
    Dec 2, 2013 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. Dec 4, 2013 at 16:07

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.