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 :-).
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.
Tania
I can do that 🙁 even for the first example
tom
Hey Tania,
Can you be a little more specific. Thanks, Tom.
Tania
Hi, in all the chapters of my thesis I have at least one figure and one table, if I put this:
in the main document, but nothing happen to my list of figures 🙁
tom
Hi Tania,
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, Tom.
Gine
Hi Tom!
Thanks, this is GREAT! and it works perfectly for me! Except one thing: It does work for the table of Figures, but it does not work for the table of Tables.
I have already gone a bit through the code, but I don’t find a piece that indicates that this is just for the figures and not for all tables.
Do I have to redefine anything if I want to make it work for both?
🙂 thanks!
Gine
tom
Hey Gine!
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 replacelof
withlot
.For simplicity, I use the first (incomplete) piece of code for illustration:
Best, Tom.
Gine
Thanks! this works! 🙂
Angela Ndundee
Thanks for that awesome posting. It saved MUCH time 🙂
Hilco
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
[…]Some code remove by Tom.[…]
tom
Hey Hilco,
Some parentheses aren’t closed correctly in the code. Here is the corrected version for both, lof and lot:
Best, Tom.
Annet
Hi Tom,
Having a big trouble using it with starred version of chapter (chapter*). Do you any solution.
I don’t want the figures of chapter* to appear in List of figures.
Thanks in advance.
-Annet
tom
Hey Annet,
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 thecaption
package. Here is an example:In case you need to switch back and forth between the two settings, you would have to redefine the chapter command. Try adding the following lines of code to your preamble:
Hope it works!
Best, Tom.
Annet
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.
Any piece of advice on this?
– Annet
tom
Hi Annet,
Please provide a minimal example that produces the error.
Thanks, Tom.
Annet
Hi Tom,
I am putting the code below. Hope you will be able to skim through. I am using MikTex distribution and Winedt as front end.
Thanks
-Annet
tom
Hi Annet,
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:A few more things worth mentioning:
Graphicx
is the extended/enhancedgraphics
package, you won’t need to load them both.Subfigure
is obsolete and was replaced bysubfig
(see my post).Hyperref
should always be the last package loaded.report
for testing, because it’s a standard class.width=0.5\textwidth
, which are specifically useful for subfigures.I hope it works this time and good luck with your thesis.
Cheers, Tom
Annet
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
tom
Hi Annet,
I wasn’t very concentrated yesterday while writing the answer, sorry for that. There was a star missing on one line (see code below). Try again and let me know if you still have problems.
Best, Tom.
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
tom
Nice, thanks for the update. Tom.
Diaa
Hi Tom,
I know it may be awkward, but I am newbie to Latex and using the ‘scrbook’ class for my thesis. I put your piece of code at the preamble end but without any change in the output.
Thanks in advance
tom
Hi Diaa,
Thanks for your question. Here is a minimal example that works with
scrbook
.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 thecaption
package.Hope it helps,
Tom.
Diaa
Thanks for help
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.
Thanks in advance
tom
Hmmm, do you use \chapter*{…} as in my example (Chapter Two)? Other than that, it’s hard to help without seeing your code. Please provide a minimal working example.
Cheers, Tom.
Diaa
Yup, I’m using the form of \chapter*{}
Here is an example of my preamble
Thanks for help
tom
Ops, my mistake. Because I used the same figure to test, I didn’t realize
list=yes
andlist=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.Diaa
I used your syntax after altering the figures contents
However, the same result.
I am not relying on it in my thesis, since I finished printing it, however, it was tempting enough to stick with it. 🙂
Sorry for wasting your time and grateful for help 🙂
tom
Interesting, when I run your code, it works perfectly, only the second figure gets added to the list-of-figures. Anyway, good luck with your thesis! Best, Tom.
Jen
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?
tom
Hi Jen,
What you can do is the following: add the following lines to your preamble and replace the hard-coded chapter command with the condition as shown below:
The command relies on an
\appendix
call and changes the definition accordingly.Best, Tom.
Jen
Awesome – that worked exactly as needed. Thanks!
tom
You are welcome! Tom.
Michael
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:
tom
Hi Michael,
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:
Let me know if you have any further questions.
Best, Tom.
Michael
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
tom
I almost gave up, but found a solution after a long search. The
tocloft
package has alofdepth
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:Hope it helps,
Tom
Michael
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
tom
Hi Michael,
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.Michael
Perfect! Thanks very much, this is exactly, what I wanted!
I will continue to read your blog – please don’t give up on that, you’re really good at this! And it’s really helpful!
Thanks again,
cheers Mike
tom
Thanks, I’m glad I was able to help. Cheers, Tom.
Jolyon Bloomfield
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.
tom
Hi Jolyon!
Wow, thanks for sharing the code. Here is a little extension that automatically sets
infigure
andintable
whenever the environments are called (taken from here). I also added a document skeleton for easier reuse.Thanks again!
Tom.
Jolyon Bloomfield
Hi again 🙂
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.
Best,
Jolyon
tom
Thank you for the update, really nice!
Best, Tom.
Magical Marshmallow
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)
I’m also a little confused as to which command I should begin a chapter with….Still using
\chapter
Cheers
tom
Hi!
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 usedequation
. 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.
Hope this helps,
Tom.
Christoph Spiegl
Hey!
This article is golden. But I found a problem with ‘hyperref’.
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?
tom
Hi!
Thanks for the nice code example. Obviously,
hyperref
redefines thechapter
andfigure
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.Good luck,
Tom.
Christoph Spiegl
Just yesterday I found a fix for the problem above.
If I place the \usepackage{hyperref} right after line 3 (so in front of the code taht manipulates the toc / lof etc), it works.
Magic! So in this case: the hyperref should not be loaded before the “ListOfFigures” manipulating code.
I just tested my assumption that you should place hyperref at the end every time. In this case its better not to do that 😉
Thanks again for the awesome blog,
Chris
tom
Thanks Chris for the providing your solution.
Here is a shorter minimal working example adding chapter titles to the list of figures only. It also includes a minor bug fix on line 23 which gave an error otherwise.
shea1986
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:
Here is the error:
! LaTeX Error: Something’s wrong–perhaps a missing \item.
l.84 \defcounter
{refsection}{0}\relax
?
Thanks a million.
tom
Hi!
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.Dimitris
Hi Tom,
I can’t seem to make these examples work in the memoir environment. The code alsways prints a “Chapter X” entry even for chapters that do not contain a figure or table. See below a MWE please
tom
Hi Dimitris,
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.HTH,
Tom
Dimitris
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.
Also i noticed that the tocloft solution modified like
to include tables as well does not work well. It places the “Chapter X” entry for all of the chapters even for those which don’t contain a table.
tom
Hi Dimitris,
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.dimmu4853
Hi Tom,
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?
Many thanks in advance for this huge effort of yours!
tom
Hi Dimitris,
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.
Dimitris
That worked like a charm Tom! You just contributed to the PhD thesis of someone far away 😉 Regards and keep up the good work.
tom
Great! Glad to know. Tom.
Adwoa
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
When I tried putting this code instead, the file won’t run
Can you provide some help please
tom
Hi Adwoa,
Below is a minimal working example that works for me.
HTH, Tom
Karen
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!
tom
Please send me what you have so far and I will try to look into it. You can paste the code as a comment.
Thanks,
Tom
Ayomikun
Hi Tom, i am working on my thesis and i want my list of tables and figures, numbered according to chapters. Like 4.1,4.2, 3.1, 3.2. How do i go about this? Here is my preamble.
[…]
[…]
tom
Hi Ayomikun
Thanks for your question. It seems what you would like to achieve is the default behaviour of the
report
class, which you overwrite with the two lines in your comment. Remove those lines and try again.Here a MWE:
Fabian
Hey Tom,
does your code work with documentclass scrreprt ?
Unfortunately I have some trouble trying to implement your solution. There is only the first chapter headline visible in the lof; the following are not.
tom
Hi Fabian,
Can you provide a minimal working example, please?
Tom