Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?
share|improve this question
1  
This here seems interesting: latex-community.org/forum/… –  Keks Dose Oct 17 '13 at 17:53

1 Answer 1

up vote 4 down vote accepted

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

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.