Glad to see it worked for you and -- even better -- you were able to understand and extend the suggested solution.
I think it's fairly acceptable but if you wish, you could make it a little more portable. For example, you could make it a little package and share the code among multiple documents. Also, you needn't duplicate the code for each floating environment if you write a more generic command that does the hooking. Finally, note the
\AtBeginEnvironment command from the
etoolbox package.
Here is a suggestion for a very small package. Just name it
chaptersinindex.sty and put it somewhere LaTeX can find it.
Code: Select all
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{chaptersinindex}[2012/01/21]
\RequirePackage{etoolbox}
\providecommand{\thechaptername}{}
\renewcommand{\chaptermark}[1]
{%
\markboth{#1}{}%
\renewcommand{\thechaptername}{#1}%
}
\newcommand%
{\AddChaptersToIndex}[2]% #1: index name (e.g. `lof')
% #2: environment to patch (e.g. `figure')
{%
\newcounter{chapter@last@#1}
\AtBeginEnvironment
{#2}
{
\ifnumequal%
{\value{chapter}}%
{\value{chapter@last@#1}}%
{}% already added
{%
\addtocontents{#1}{\protect\numberline{\vspace{\BeforeChapterInIndex}}}%
\addtocontents%
{#1}%
{\protect\numberline{\ChapterInIndex{\thechapter}{\thechaptername}}}%
\setcounter{chapter@last@#1}{\value{chapter}}%
\addtocontents{#1}{\protect\numberline{\vspace{\AfterChapterInIndex}}}%
}
}
}
\newlength{\BeforeChapterInIndex}
\setlength{\BeforeChapterInIndex}{0ex plus 1ex}
\newlength{\AfterChapterInIndex}
\setlength{\AfterChapterInIndex}{0ex}
\newcommand{\ChapterInIndex}[2]% #1: number, #2: title
{{\bfseries#1\quad#2}}
Then in your document, the relevant code reduces to
Code: Select all
\usepackage{chaptersinindex}
\AddChaptersToIndex{lof}{figure}
\AddChaptersToIndex{lot}{table}
If the user (you) wish, she can redefine the macro
\ChapterInIndex that takes the chapter number as its first and the chapter name as its second argument and typesets them in the indices. Also, the dimensions
\BeforeChapterInIndex and
\AfterChapterInIndex can be changed by the user to adjust the spacing before and after the chapter entries. Note however that these dimensions
add to the height of a single line of text. You could make the solution even more flexible by allowing the heading level (
\chapter at the moment) to be specified as a package option so the package would also be useful to an
article, say.
The posted solution is not so nice however in the way that the
\renewcommand{\chaptermark}[1]... stuff is likely to be broken by a user who uses customized headings. Maybe you have a better idea for implementing that.
Happy TeXing!