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[1] \addcontentsline > >
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><> >
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 :
\provideboolNext, 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[1] \global\setbool >
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[2][\shortcaption] \ifbool<\protect\contentsline><>>><> \global\boolfalse \oldcaption[#1] >
If you never use the optional argument of the chapter command ( chapter[alternative toc 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<> \let\saved@chapter\chapter \renewcommand<\@dblarg\my@chapter>% > \newcommand*<\my@chapter>[2][]% \global\setbool > \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.
\newcommand[1] \addcontentsline > >in the main document, but nothing happen to my list of figures 🙁
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.
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
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 replace lof with lot . For simplicity, I use the first (incomplete) piece of code for illustration:
\newcommand[1] \addcontentsline > \addcontentsline > >
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.[…]
\let\oldcaption\caption \renewcommand[2][\shortcaption] \ifbool<\protect\contentsline> \addtocontents<\protect\contentsline ><>>><> \global\boolfalse \oldcaption[#1] >
Hey Hilco, Some parentheses aren’t closed correctly in the code. Here is the corrected version for both, lof and lot:
\let\oldcaption\caption \renewcommand[2][\shortcaption] \ifbool<\protect\contentsline><>> \addtocontents<\protect\contentsline ><>> ><> \global\boolfalse \oldcaption[#1] >
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
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 the caption package. Here is an example:
\documentclass[11pt] \usepackage \captionsetup \begin \listoffigures \chapter* \begin[ht] \centering\rule \caption \end \endIn 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:
\usepackage \makeatletter \newcommand<> \let\saved@chapter\chapter \renewcommand < \captionsetup\@ifstar <\@dblarg\my@chapter>> \newcommand*<\my@chapter>[2][] < \saved@chapter[#1]\captionsetup > \makeatotherHope it works! Best, Tom.
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
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
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:
I hope it works this time and good luck with your thesis.
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
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.
\makeatletter \newcommand<> \let\saved@chapter\chapter \renewcommand% \@ifstar <\@dblarg\my@chapter>>% \newcommand*<\my@chapter>[2][]% \captionsetup> \makeatother
You are so awesome. This worked like charm, except it puts the “
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[ ]
Thanks a ton for pointing out caption package. I learnt a lot from you.
-regards
Annet
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.
\makeatletter \newcommand<> \let\saved@chapter\chapter \renewcommand% \@ifstar <\@dblarg\my@chapter>>% \newcommand*<\my@chapter>[2][]% \captionsetup> \makeatotherThanks in advance Hi Diaa, Thanks for your question. Here is a minimal example that works with scrbook . \documentclass[11pt] \usepackage \makeatletter \newcommand<> \let\saved@chapter\chapter \renewcommand% \@ifstar <\@dblarg\my@chapter>>% \newcommand*<\my@chapter>[2][]% \captionsetup> \makeatother \begin \tableofcontents \listoffigures \chapter \begin[ht] \centering \includegraphics[width=0.3\textwidth] \caption \label \end \chapter* \begin[ht] \centering \includegraphics[width=0.3\textwidth] \caption \label \end \end
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. Hope it helps,
Tom.
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
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.
Yup, I’m using the form of \chapter*<>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.
\documentclass[14pt,listof=totoc] \usepackage[center] \makeatletter \newcommand<> \let\saved@chapter\chapter \renewcommand% \@ifstar <\@dblarg\my@chapter>> \newcommand*<\my@chapter>[2][]% \captionsetup> \makeatother \begin \tableofcontents \listoffigures \chapter* <\hfill SUMMARY\hfill>\begin[ht] \centering \rule\\ \caption \label \end \chapter \begin[ht] \centering \rule\\ \caption \label \end \endHowever, 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 🙂
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.
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 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:
\providebool \let\oldappendix\appendix \renewcommand<% \global\booltrue% \oldappendix% > . \ifbool% >% >%The command relies on an \appendix call and changes the definition accordingly.
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:
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:
\documentclass \usepackage \makeatletter \newcommand<> \let\saved@chapter\chapter \renewcommand% \@ifstar <\@dblarg\my@chapter>> \newcommand*<\my@chapter>[2][]% \saved@chapter[#1] \addtocontents<\protect\contentslineSorry, 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:
\documentclass \usepackage \providebool \makeatletter \let\oldcaption\caption \renewcommand[2][\shortcaption] \ifbool<\protect\contentslineWorks 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
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.
\documentclass \usepackage \providebool \makeatletter \let\oldcaption\caption \renewcommand[2][\shortcaption] \ifbool<\protect\contentslinePerfect! 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
% Create a flag \providebool \providebool % Fix the \chapter command to set a flag \newcommand<> \let\saved@chapter\chapter \renewcommand<\@dblarg\my@chapter>% > \newcommand*<\my@chapter>[2][]% \global\setbool \global\setbool > % Fix the \caption command to check the flag and insert a LOF line \let\oldcaption\caption \renewcommand[2][\shortcaption] \ifbool< \ifbool<\protect\contentsline% >% >% ><>>><> \global\boolfalse ><> \ifbool< \ifbool<\protect\contentsline % >% >% ><>>><> \global\boolfalse ><> \oldcaption[#1] > % Add in appendix detection \providebool \let\oldappendix\appendix \renewcommand<% \global\booltrue% \oldappendix% > % Add in figure/table detection \providebool \providebool \newcommand<% \global\booltrue% \global\boolfalse% > \newcommand<% \global\booltrue% \global\boolfalse% >
Hi Jolyon! 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. Thanks again!
Tom.
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
% Add in figure/table detection \providebool \providebool \AtBeginEnvironment< \global\booltrue% \global\boolfalse% > \AtBeginEnvironment< \global\booltrue% \global\boolfalse% >Thank you for the update, really nice!
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)
\addcontentsline \listoffigures \addcontentsline \listoftables \addcontentsline \newcommand<\listequationsname> \newlistof <\listequationsname>\newcommand[1]<% \addcontentsline<\protect\numberline<\theequation>#1>\par> \listofequations \addcontentsline \chaptermark \printnomenclature[2.5cm] \newcommand[1] \addtocontents<\contentsline><>> \addtocontents<\contentsline><>> \addtocontents<\contentsline><>>> \providebool \newcommand[1] \global\setbool > \let\oldcaption\caption \renewcommand[2][\shortcaption] \ifbool<% \addtocontents<\protect\contentsline ><>><> \addtocontents<\protect\contentsline ><>><> \addtocontents<\contentsline><>><>> \global\boolfalse \oldcaption[#1] > \makeatletter \newcommand<> \let\saved@chapter\chapter \renewcommand<\@dblarg\my@chapter>% > \newcommand*<\my@chapter>[2][]<% \saved@chapter[#1]% \global\setbool > \makeatother
I’m also a little confused as to which command I should begin a chapter with….Still using \chapter Cheers
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 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
Tom.
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?
\documentclass[12pt,oneside] \usepackage % Required for including pictures \usepackage % --------------------------------------------------- Add Chapters to the ListOfFigures \makeatletter % Create a flag \providebool \providebool % Fix the \chapter command to set a flag \newcommand<> \let\saved@chapter\chapter \renewcommand<\@dblarg\my@chapter>% > \newcommand*<\my@chapter>[2][]% \global\setbool \global\setbool > % Fix the \caption command to check the flag and insert a LOF line \let\oldcaption\caption \renewcommand[2][\shortcaption] \ifbool< \ifbool<\protect\contentslineHi! 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. Good luck,
Tom.
Just yesterday I found a fix for the problem above. If I place the \usepackage
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.
\documentclass[12pt] \usepackage \usepackage % used for \nameref \makeatletter % Create a flag \providebool \providebool % Fix the \chapter command to set a flag \newcommand<> \let\saved@chapter\chapter \renewcommand<\@dblarg\my@chapter>> \newcommand*<\my@chapter>[2][]% \global\setbool> % Fix the \caption command to check the flag and insert a LOF line \let\oldcaption\caption \renewcommand[2][\shortcaption] \ifbool<% \ifbool<\protect\contentslineHi 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:
\providebool \newcommand[1] \global\setbool > \let\oldcaption\caption \renewcommand[2][\shortcaption] \ifbool<\protect\contentsline><>> \addtocontents<\protect\contentsline ><>> ><> \global\boolfalse \oldcaption[#1] >
Here is the error:
! LaTeX Error: Something’s wrong–perhaps a missing \item.
l.84 \defcounter
\relax
? Thanks a million.
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.
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
\documentclass[12pt,a4paper,twoside] \usepackage \providebool \newcommand<\mytitle>[1]\global\setbool> \let\oldcaption\caption \renewcommand[2][\shortcaption] \ifbool<\protect\contentslineHi 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
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
\preto\table=0\addtocontents>\fi
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.
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.
\documentclass[12pt] \usepackage \preto\figure=1\addtocontents>\fi > \preto\table=0\addtocontents>\fi > \begin \tableofcontents \listoffigures \listoftables \chapter \begin[ht]\centering\includegraphics[width=0.5\textwidth]\caption[]\end \blindtext \begin[ht]\centering\includegraphics[width=0.5\textwidth]\caption\end \chapter \begin[ht]\centering\includegraphics[width=0.5\textwidth]\caption[]\end \begin[ht]\caption\centering\begin<|c|c|>\hline a&b\\c&d\\\hline\end\end% \blindtext \chapter \blindtext \endHi 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!
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.
\documentclass[12pt] \usepackage \preto\figure=0\addtocontents>\fi > \preto\wrapfigure=0\addtocontents>\fi > \begin \tableofcontents \listoffigures \chapter \begin[ht]\centering\rule\caption\end \begin <0.5\linewidth>\centering \rule<0.9\linewidth> <0.75\linewidth>\caption \end \blindtext \chapter \endThat worked like a charm Tom! You just contributed to the PhD thesis of someone far away 😉 Regards and keep up the good work.
Great! Glad to know. Tom.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
\newcommand[1] \addcontentsline > \addcontentsline > >When I tried putting this code instead, the file won’t run
\usepackage \providebool \newcommand[1] \global\setbool > \let\oldcaption\caption \renewcommand[2][\shortcaption] \ifbool<\protect\contentslineCan you provide some help please><>>><> \global\boolfalse \oldcaption[#1] >
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!
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
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. […]
\renewcommand-\arabic> \renewcommand-\arabic>
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:
\documentclass[11pt] \begin \listoffigures \chapter \begin[ht] \centering \rule \caption \end \endHey 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.