because LaTeX matters

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.

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:

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:

Your code seems fine. The only thing I can think of is that you didn’t replace all \chapter{...} commands in your document by \newchapter{...}. If this wasn’t the problem, please provide a minimal example for me to reproduce the issue.

Thanks for your comment. You are right, I should have been clearer on that point. The whole code covers the list-of-figures only. If you want both, lof and lot, you will have to duplicate the code for \addcontentsline and replace lof with lot.

For simplicity, I use the first (incomplete) piece of code for illustration:

Hi Tom,
Thanks so much for this doc. I’ve got one problem; I want to apply all to both lof and lot. Following your reply to Gine, I figured to use the code below. But now it fails. Apparently, it doesn’t like me to use \addcontents twice in this manner. With only once using \addcontents, it works fine for either lof or lot. Please help!
best, Hilco

The star only works for the item itself. In other words, the chapter won’t be listed in the table of contents, but figures will be added to the list of figures. If you want to keep figures from being listed in the lof, use captionsetup of the captionpackage. Here is an example:

Hi Tom,
Thanks a lot for your reply. You guessed it right, I am using both, \chapter and \chapter*. But using the second piece of code, you suggested, I am getting another error. While compiling using pdflatex, it complains that “perhaps \item is missing” and opens up the TOC file.

It’s not a hack, it’s a redefinition of the chapter command :-).
I took the liberty of simplifying the preamble of your minimal example slightly. Turns out the amsmath package creates the error, but there is no conflict. I forgot to add some “%” inside the command definitions in the code I provided, my apologies. To be on the save side, I provide the complete code this time, works like a charm:

Hi Tom,
Thanks a ton for detailed reply. I am a newbie to Latex and using these templates provided by senior students.
It certainly solved the problem of the LOF. But now even using \chapter*, I am getting Chapter number on top of the chapter. Not just that, it puts a chapter number on top of the TOC and LOF, which I do not want.
Thanks again
-Annet

You are so awesome. This worked like charm, except it puts the “{long caption}” instead of “[short caption]”in the LOF.
And guess what, I found a solution on my own which is damn easier and I am sharing it with you.
Using the caption package, if I use \caption[ ]{long caption}, it automatically get skipped from the LOF. So in my chaper*, I am using this and it works beautifully, without me redefining the \chapter and \chapter* commands in the preamble.
Thanks a ton for pointing out caption package. I learnt a lot from you.
-regards
Annet

The code couples chapter listing to its figure listings. In case you need occasional suppression of list-of-figures listings, you could as well use what Annet proposed, the starred caption command of the caption package.

However, since I am using relatively many packages including caption and graphicx, I just copied from your proposed syntax the lines 3 => 12 and pasted them after putting the packages of caption and graphicx in my preamble. Unfortunately, the result didnt change. So, If I did it in the wrong way, it would be appreciated to resolve this for me.

Ops, my mistake. Because I used the same figure to test, I didn’t realize list=yes and list=no were mixed up. My apologies. I will change it in the example above. Thanks for insisting! Below is the correct code for your example. Thanks, Tom.

This works really well for me (I’m using the last version, with the redefinition of the chapter command).

The only problem I’m running into is that appendices that include figures are listed in the lof as “Chapter A”, etc. rather than “Appendix A”. This appears to be because the term “Chapter” is hard-coded into the \addtocontents command. Is there a way to fix this?

Hi!
I’ve got a little problem here:
With scrbook and book aswell I have the lines starting with: \makeatletter … and I get an error for newchap. So I providebool with gives me error for no package – so far, so clear. I now use the etoolbox and the effect of the Chapter sorting in my tof is gone! Same with the * thing – so the \chapter* figures are now listed again. This is my minimal example:

Thanks for your question. Looking at the code I provided in the post. It assumes every non-starred chapter has a figure. In that case, one does not have to redefine the caption command. If I understand correctly, what you are trying to do is combine two things, the title in the list-of-figures/tables and suppress captions from being listed for starred chapters. Here is a minimal working example:

Sorry, that I didn’t state quite well, what I was trying to do. I want the wto things you put in this last post and, in case a chapter is figure free, this one not listed in the lif. Thanks for your effort and time, I hope you can find the time to help me with this last little cosmetic surgery. Really helpful and charming site btw! Regards, Mike

I almost gave up, but found a solution after a long search. The tocloft package has a lofdepth counter, which provides control over whether a figure is added to the list of figures (1) or not (0). The same works for tables (lotdepth). Here is a code:

Works like a charm! Thank you very much for the effort!
This is really cool!
I’ve tried to get it, but to fully understand somebody elses code is horrible. I would really love this last little change, but I’m to stupid even for that: I would like to change the Names of the chapters in the tof to “1 Summary” -> the actual name of each chapter. But as I can see, you refer not to the chapter as is, but rather to your set bool. A possible way should include the nameref package (wich is part of hyperref). But I cant find a variable for the Chapter the figures are in. Do you know an easy workaround? This would be the topping of this so delicious icecream :)!
Thank you anyway,
greets, Mike

Thanks for the idea with the nameref package, which saved me time searching. The trick is to automatically label the chapter and then read the name using the reference. It’s probably not very clean. So, if you run into problems, try cleaning all meta-files and re-typeset. Good luck, Tom.

Hi,
I found this snippet and really liked it. However, there really needs to be separate detection of figures and tables existing. I couldn’t find a smart way to do it, so I used a hacky way. Somebody else might find this useful though, so I post it here. Feel free to tell me a smart way to do it 😉
Enjoy,
Jolyon

Whenever a \begin{table} or \begin{figure} is called, I call \settable or \setfigure as the first step, so that the code knows which entry to add. Seems to work 😉 I have the below pasted in a class file.

Wow, thanks for sharing the code. Here is a little extension that automatically sets infigure and intable whenever the environments are called (taken from here). I also added a document skeleton for easier reuse.

While browsing for something completely different, I came across some neat commands in the etoolbox package, which I think do a really nice job of automatically setting the figure/table flag. These lines would replace 46-56 in my original code.

Hey Tom,
Great piece, really useful and will tidy up a page brilliantly. I’m not sure where I’m going wrong though. I am hoping to have my list of tables, figures and equations all subdivided out, but I can’t seem to get any response from my text (Even the figures and tables haven’t been re-structured)

Hmm, that’s quite a lot of stuff to look at. One thing that I saw, you defined the list as equations with an ‘s’ at the end, but later on you used equation. That might fix some if not all the errors you get. Otherwise, if you have that much code, I suggest commenting out everything and than add one by one. A good starting point would be to get the equation environment running.

Oh, and check the brackets inside \ifbool{newchap}{%, they are wrong.

Concerning your chapter question, the idea is to redefine the chapter command, but not changing its usage. So yes, \chapter{} should still work.

This is the most minimal I came up with (sorry I have no idea how to post ‘code’ here, please feel free to change it accordingly), the code is basically the example by Jolyon Bloomfield with ‘hyperreff’ enabled (with hyperreff disabled, it works like a charm).

I already noticed that it should be loaded at the end but even if I do so, the Chapter markers disappear from the \listoffigures. Can you help?

Thanks for the nice code example. Obviously, hyperref redefines the chapter and figure commands, by adding bookmarks to toc and lof. Unfortunately, I don’t know how to fix this problem. Maybe someone else can help. Also, you might want to consider posting your question here. They have very skilled tex people and usually questions are answered quickly.

Hi Tom, thanks for this wonderful post. It really helps me a lot. I have no problem with the first part, i.e. grouping the lof and lot with chapters. However, when I use the code in the post trying to determine if there is a table in the chapter, I encountered with this compiling error:
Here is the code:

Thanks for your comment. I won’t be able to help if you just copy the code from above. You would need to show me a complete minimal working example. It might be a package you load that is not compatible, possibly because it redefines chapter itself. But as I said, I would need to see what you are doing. Also, add the last part to your code, \makeatletter ... \makeatother, it seems it is not working without it.

Thanks for your comment. You are right, the code doesn’t distinguish between figures and tables, but stupidly adds it to both lists. There is a comment which shows how to fix this.

Btw., just recently I figured out there is a much easier way to do this using another command of the etoolbox package. Just scroll to the end of this recent post. It only works with numbered chapters though.

Thanks Tom. My problem is that this solution with the tocloft package works partially for me. You see, in the frontpage of each chapter i place a picture, for decorative let’s say reasons, and then the rest of the chapter might contain no figures at all. Obviously in these cases the code identifies the \figure of the decorative one and places the corresponding Chapter X entry. which is not what i want since the rest chapter has no figures. Any idea how to overcome this would be very helpful.

I assume your intention is not to list the ‘decorative’ figures in the LOF. Try the code below. It uses an empty optional argument of the caption package to suppress LOF entries. The solution assumes every chapter has an initial figure which has a numbered caption. I don’t see the problem you are describing for tables.

A more creative approach would be to omit interference of ‘decorative’ figures with heading placement by producing them with minipage instead. See here for how to add a caption.

I figured out my problems. I had created some commands for special formatting of the abstract at the beginning of each chapter that contained a table. Code was recognizing this so it printed a Chapter X for every chapter. I also experimented and saw that a \wrapfigure would cause problems for the solution above. Any code enhancement to include correctly wrapfigure environments as well?

I saw your comments, just hadn’t had the time to answer. I’m glad you sorted most of the issues out. Concerning wrapfigure, the simplest solution would be to add another conditional statement for those. Wrapfigure uses the same figure counter as the standard figure environment. Therefore, the heading is added by either the first figure or wrapfigure, whichever comes first. See the code example below.

Thank you for this post, it has helped a lot as I am very new to latex. I am having one issue though. my latex code is still printing subtitles for chapters that have no content i.e. no figures. I am having his same issue for my list of tables. I tried using this code described in the article, and it still print every chapter heading even if there are no figures in it

Hi Tom,
I’m wondering if this is simple and possible to do in the article documentclass with sections (and ideally subsections). I’ve tried adapting your code, but with no luck (to be fair, I know very little about writing TeX macros!). Thanks!