texblog

Adding additional structure to the list of figures/tables

In large documents, you may end up with a pretty long list of figures or tables. In this post, I will show how to nicely structure these lists by adding subtitles. The whole thing turned out to be more tricky than I first thought, but more on that later. The my examples, I will show how to organize the list of figures by chapters. However, the code can be adapted and extended with little effort, including doing the same for the list of tables, organizing the lists by different levels (part, section) for the various documentclasses, the actual subtitle format (with/without name, page number), etc.

The main idea is to add a line to the list of figures whenever a new chapter is created. That’s relatively straight forward, we extend the chapter command by defining a custom command.

\newcommand{\newchapter}[1]{%
	\chapter{#1}
	\addcontentsline{lof}{chapter}{%
		Chapter \thechapter: #1 \vspace{10pt}
	}
}

The lines highlighted add a full entry to the list of figures. In order to omit the page number, use addtocontents as follows instead.

\addtocontents{lof}{\contentsline{chapter}{%
	Chapter \thechapter \vspace{10pt}}{}
}

This is all great and easy. The problem starts, however, when individual chapters have no figures. Then, the subtitle will still be printed, however, that there is no content. This is not exactly what we want. Therefore, we add a condition on when to print the subtitle and when not. The etoolbox package comes in handy here, it provides “if-then-else-like” statements. Here is the documentation. Briefly, whenever a new chapter starts, we set the parameter newchap to true. The subtitle is printed along with the first caption added to the list of figures. Therefore, if a chapter has no figure, the subtitle will not show up in the list.

First, we define the boolean parameter newchap:

\providebool{newchap}

Next, we define an alternative chapter command newchapter. This time, however, instead of directly adding the subtitle to the list, we only set the boolean parameter to true:

\newcommand{\newchapter}[1]{%
	\chapter{#1}
	\global\setbool{newchap}{true}
}

And finally, we redefine the caption command. The additional definition of shortcaption is necessary to support the optional caption argument, the alternative short caption for the list of figures.

\let\oldcaption\caption
\renewcommand{\caption}[2][\shortcaption]{%
\def\shortcaption{#2}
\ifbool{newchap}{%
	\addtocontents{lof}{\protect\contentsline{chapter}{%
		Chapter \thechapter \vspace{10pt}
	}{}}}{}
	\global\boolfalse{newchap}
	\oldcaption[#1]{#2}
}

If you never use the optional argument of the chapter command (chapter[alternative toc name]{real chapter name}) throughout your document, this is basically it, we are done.

In case you use the optional augment, however, we are not quite done yet, since we redefined chapter and by doing so omitted some of the functionality. We can fix this issue by redefining chapter in a different way, similarly to what we did for caption which brings additional advantages. On one hand, the code becomes more consistent and on the other hand, you won’t have to change chapter to newchapter throughout your document, since we just redefine the command rather than replace it. And here is the code:

\makeatletter
\newcommand{\saved@chapter}{}
\let\saved@chapter\chapter
\renewcommand{\chapter}{%
  \@ifstar {\saved@chapter*}{\@dblarg\my@chapter}%
}
\newcommand*{\my@chapter}[2][]{%
  \saved@chapter[#1]{#2}%
  \global\setbool{newchap}{true}
}
\makeatother

I got this piece of code from this google group.

And here is what the final result looks like. Pretty cool, huh :-).

List of figures with subtitles

The whole story only makes sense and works with numbered chapters, (i.e. when using the starred version: \chapter*{} you may run into problems or get undesired results, but you would have to try).

I am pretty sure there is an alternative, maybe simpler way to do the same thing. Maybe the tocloft package has a solution. Feel free to drop a comment and let me know about it. Thanks.

Exit mobile version