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.

January 22nd, 2012 at 6:08 pm
I can do that
even for the first example
January 23rd, 2012 at 5:01 pm
Hey Tania,
Can you be a little more specific. Thanks, Tom.
January 23rd, 2012 at 9:24 pm
Hi, in all the chapters of my thesis I have at least one figure and one table, if I put this:
\newcommand{\newchapter}[1]{% \chapter{#1} \addcontentsline{lof}{chapter}{% Chapter \thechapter: #1 \vspace{10pt} } }in the main document, but nothing happen to my list of figures
January 24th, 2012 at 6:48 am
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.
January 27th, 2012 at 6:05 pm
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
January 27th, 2012 at 6:30 pm
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
\addcontentslineand replacelofwithlot.For simplicity, I use the first (incomplete) piece of code for illustration:
\newcommand{\newchapter}[1]{% \chapter{#1} \addcontentsline{lof}{chapter}{% Chapter \thechapter: #1 \vspace{10pt} } \addcontentsline{lot}{chapter}{% Chapter \thechapter: #1 \vspace{10pt} } }Best, Tom.
January 27th, 2012 at 6:45 pm
Thanks! this works!
January 28th, 2012 at 11:07 pm
Thanks for that awesome posting. It saved MUCH time
February 16th, 2012 at 11:17 am
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{\caption}[2][\shortcaption]{% \def\shortcaption{#2} \ifbool{newchap}{% \addtocontents{lot}{\protect\contentsline{chapter}{% Chapter \thechapter \vspace{10pt} } \addtocontents{lof}{\protect\contentsline{chapter}{% Chapter \thechapter \vspace{10pt} }{}}}{} \global\boolfalse{newchap} \oldcaption[#1]{#2} }February 17th, 2012 at 7:26 pm
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{\caption}[2][\shortcaption]{% \def\shortcaption{#2} \ifbool{newchap}{% \addtocontents{lof}{\protect\contentsline{chapter}{% Chapter \thechapter \vspace{10pt}}{}} \addtocontents{lot}{\protect\contentsline{chapter}{% Chapter \thechapter \vspace{10pt}}{}} }{} \global\boolfalse{newchap} \oldcaption[#1]{#2} }Best, Tom.
March 12th, 2012 at 3:53 pm
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
March 13th, 2012 at 3:48 am
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
captionsetupof thecaptionpackage. Here is an example:\documentclass[11pt]{report} \usepackage{caption} \captionsetup{list=no} \begin{document} \listoffigures \chapter*{First chapter} \begin{figure}[ht] \centering\rule{4cm}{3cm} \caption{default} \end{figure} \end{document}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:
\usepackage{caption} \makeatletter \newcommand{\saved@chapter}{} \let\saved@chapter\chapter \renewcommand{\chapter}{ \captionsetup{list=no} \@ifstar {\saved@chapter}{\@dblarg\my@chapter} } \newcommand*{\my@chapter}[2][]{ \saved@chapter[#1]{#2} \captionsetup{list=yes} } \makeatotherHope it works!
Best, Tom.
March 13th, 2012 at 1:12 pm
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
March 13th, 2012 at 1:49 pm
Hi Annet,
Please provide a minimal example that produces the error.
Thanks, Tom.
March 13th, 2012 at 2:11 pm
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
\documentclass[12pt,oneside]{tifrthesis} \usepackage{caption,graphicx} \usepackage[linktocpage=true]{hyperref} \usepackage{amsmath} % piece of code from Tom for LOF hack \makeatletter \newcommand{\saved@chapter}{} \let\saved@chapter\chapter \renewcommand{\chapter}{ \captionsetup{list=no} \@ifstar {\saved@chapter}{\@dblarg\my@chapter} } \newcommand*{\my@chapter}[2][]{ \saved@chapter[#1]{#2} \captionsetup{list=yes} } \makeatother % END of Hack \begin{document} %%%%%%% This is my chapter* \chapter*{Synopsis} \section*{Introduction} \begin{figure} \includegraphics[width=80mm]{Syn-Fig0} \caption[short caption 1]{More blah} \end{figure} \tableofcontents \listoffigures \listoftables %%%%%%%% This is my chapter \chapter{Thesis Chapter} \begin{figure} \includegraphics[width=80mm]{Main-Fig0} \caption[short caption 2]{More blah} \end{figure} \end{document}March 13th, 2012 at 2:55 pm
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
amsmathpackage 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:\documentclass[12pt,oneside]{report} \usepackage{caption, graphicx} \usepackage[linktocpage=true]{hyperref} \usepackage{amsmath} \makeatletter \newcommand{\saved@chapter}{} \let\saved@chapter\chapter \renewcommand{\chapter}{% \captionsetup{list=no}% \@ifstar {\saved@chapter*}{\@dblarg\my@chapter}} \newcommand*{\my@chapter}[2][]{% \saved@chapter[#1]{#2}% \captionsetup{list=yes}} \makeatother \begin{document} \chapter*{Synopsis} \section*{Introduction} \begin{figure} \includegraphics[width=80mm]{test} \caption[short caption 1]{More blah} \end{figure} \tableofcontents \listoffigures \chapter{Thesis Chapter} \begin{figure} \includegraphics[width=80mm]{test} \caption[short caption 2]{More blah} \end{figure} \end{document}A few more things worth mentioning:
Graphicxis the extended/enhancedgraphicspackage, you won’t need to load them both.Subfigureis obsolete and was replaced bysubfig(see my post).Hyperrefshould always be the last package loaded.reportfor 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
March 13th, 2012 at 5:59 pm
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
March 14th, 2012 at 3:08 am
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{\saved@chapter}{} \let\saved@chapter\chapter \renewcommand{\chapter}{% \captionsetup{list=no}% \@ifstar {\saved@chapter*}{\@dblarg\my@chapter}}% \newcommand*{\my@chapter}[2][]{% \saved@chapter[#1]{#2}% \captionsetup{list=yes}} \makeatotherBest, Tom.
March 14th, 2012 at 12:27 pm
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
March 14th, 2012 at 4:28 pm
Nice, thanks for the update. Tom.
March 15th, 2012 at 5:41 pm
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{\saved@chapter}{} \let\saved@chapter\chapter \renewcommand{\chapter}{% \captionsetup{list=no}% \@ifstar {\saved@chapter*}{\@dblarg\my@chapter}}% \newcommand*{\my@chapter}[2][]{% \saved@chapter[#1]{#2}% \captionsetup{list=yes}} \makeatotherThanks in advance
March 18th, 2012 at 4:13 am
Hi Diaa,
Thanks for your question. Here is a minimal example that works with
scrbook.\documentclass[11pt]{scrbook} \usepackage{caption, graphicx} \makeatletter \newcommand{\saved@chapter}{} \let\saved@chapter\chapter \renewcommand{\chapter}{% \captionsetup{list=no}% \@ifstar {\saved@chapter*}{\@dblarg\my@chapter}}% \newcommand*{\my@chapter}[2][]{% \saved@chapter[#1]{#2}% \captionsetup{list=yes}} \makeatother \begin{document} \tableofcontents \listoffigures \chapter{One} \begin{figure}[ht] \centering \includegraphics[width=0.3\textwidth]{test} \caption{default} \label{default} \end{figure} \chapter*{Two} \begin{figure}[ht] \centering \includegraphics[width=0.3\textwidth]{test} \caption{default} \label{default} \end{figure} \end{document}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
captioncommand of thecaptionpackage.Hope it helps,
Tom.
March 22nd, 2012 at 11:48 pm
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
March 24th, 2012 at 5:41 am
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.
March 24th, 2012 at 7:41 pm
Yup, I’m using the form of \chapter*{}
Here is an example of my preamble
\documentclass[14pt,listof=totoc,bibliography=totoc]{scrbook} \usepackage[center]{caption} \usepackage{subcaption} \makeatletter \newcommand{\saved@chapter}{} \let\saved@chapter\chapter \renewcommand{\chapter}{% \captionsetup{list=no}% \@ifstar {\saved@chapter*}{\@dblarg\my@chapter}}% \newcommand*{\my@chapter}[2][]{% \saved@chapter[#1]{#2}% \captionsetup{list=yes}} \makeatother \begin{document} \chapter*{\begin{center}SUMMARY\end{center}} \chapter{Intro} \begin{figure}[H] \centering \includegraphics[scale=.63]{CHB_schem.pdf}\\ \caption{General layout of conventional hydraulic braking system configuration}\label{CHB_lay} \end{figure} \end{document}Thanks for help
March 25th, 2012 at 5:17 am
Ops, my mistake. Because I used the same figure to test, I didn’t realize
list=yesandlist=nowere 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]{scrbook} \usepackage[center]{caption} \makeatletter \newcommand{\saved@chapter}{} \let\saved@chapter\chapter \renewcommand{\chapter}{% \captionsetup{list=no}% \@ifstar {\saved@chapter*}{\@dblarg\my@chapter}} \newcommand*{\my@chapter}[2][]{% \saved@chapter[#1]{#2}% \captionsetup{list=yes}} \makeatother \begin{document} \tableofcontents \listoffigures \chapter*{\hfill SUMMARY\hfill} \begin{figure}[ht] \centering \rule{4cm}{3cm}\\ \caption{Non-listed caption for summary figure.} \label{fig:sum} \end{figure} \chapter{Intro} \begin{figure}[ht] \centering \rule{4cm}{3cm}\\ \caption{Listed caption for intro figure.} \label{fig:int} \end{figure} \end{document}March 25th, 2012 at 3:14 pm
I used your syntax after altering the figures contents
\documentclass[14pt,listof=totoc]{scrbook} \usepackage{graphicx} \usepackage[center]{caption} \makeatletter \newcommand{\saved@chapter}{} \let\saved@chapter\chapter \renewcommand{\chapter}{% \captionsetup{list=no}% \@ifstar {\saved@chapter*}{\@dblarg\my@chapter}} \newcommand*{\my@chapter}[2][]{% \saved@chapter[#1]{#2}% \captionsetup{list=yes}} \makeatother \begin{document} \tableofcontents \listoffigures \chapter*{\hfill SUMMARY\hfill} \begin{figure}[H] \centering \includegraphics[scale=.63]{CHB_schem.pdf}\\ \caption{General layout of conventional hydraulic braking system configuration} \end{figure} \chapter{Intro} \begin{figure}[H] \centering \includegraphics[scale=.63]{CHB_schem.pdf}\\ \caption{General} \end{figure} \end{document}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
March 25th, 2012 at 4:49 pm
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.
April 5th, 2012 at 5:29 pm
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?
April 8th, 2012 at 12:46 pm
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{appendix} \let\oldappendix\appendix \renewcommand{\appendix}{% \global\booltrue{appendix}% \oldappendix% } ... \ifbool{appendix}% {Appendix \thechapter \vspace{10pt}}% {Chapter \thechapter \vspace{10pt}}%The command relies on an
\appendixcall and changes the definition accordingly.Best, Tom.
April 9th, 2012 at 6:12 pm
Awesome – that worked exactly as needed. Thanks!
April 10th, 2012 at 2:13 am
You are welcome! Tom.
April 11th, 2012 at 8:49 am
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:
\documentclass{book} \usepackage{caption} \usepackage{etoolbox} \providebool{newchap} \let\oldcaption\caption \renewcommand{\caption}[2][\shortcaption]{% \def\shortcaption{#2} \ifbool{newchap}{% \addtocontents{lof}{\protect\contentsline{chapter}{% Chapter \thechapter \vspace{10pt}}{}} \addtocontents{lot}{\protect\contentsline{chapter}{% Chapter \thechapter \vspace{10pt}}{}}}{} \global\boolfalse{newchap} \oldcaption[#1]{#2}} \makeatletter \newcommand{\saved@chapter}{} \let\saved@chapter\chapter \renewcommand{\chapter}{% \captionsetup{list=no}% \@ifstar {\saved@chapter*}{\@dblarg\my@chapter}} \newcommand*{\my@chapter}[2][]{% \saved@chapter[#1]{#2}% \captionsetup{list=yes}} \makeatother \begin{document} \tableofcontents \listoffigures \chapter*{\hfill SUMMARY\hfill} \begin{figure}[ht] \centering \rule{4cm}{3cm}\\ \caption{Non-listed caption for summary figure.} \label{fig:sum} \end{figure} \chapter{Intro} \begin{figure}[ht] \centering \rule{4cm}{3cm}\\ \caption{Listed caption for intro figure.} \label{fig:int} \end{figure} \end{document}April 12th, 2012 at 5:15 am
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{book} \usepackage{caption} \makeatletter \newcommand{\saved@chapter}{} \let\saved@chapter\chapter \renewcommand{\chapter}{% \captionsetup{list=no}% \@ifstar {\saved@chapter*}{\@dblarg\my@chapter}} \newcommand*{\my@chapter}[2][]{% \captionsetup{list=yes}% \saved@chapter[#1]{#2} \addtocontents{lof}{\protect\contentsline{chapter}{% Chapter \thechapter \vspace{10pt}}{}} } \makeatother \begin{document} \tableofcontents \listoffigures \chapter*{Summary} \begin{figure}[ht]\centering\rule{4cm}{3cm}\caption{Non-listed.}\end{figure} \chapter{Introduction} \begin{figure}[ht]\centering\rule{4cm}{3cm}\caption{Listed.}\end{figure} \end{document}Let me know if you have any further questions.
Best, Tom.
April 15th, 2012 at 8:46 pm
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
April 17th, 2012 at 2:30 pm
I almost gave up, but found a solution after a long search. The
tocloftpackage has alofdepthcounter, 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{book} \usepackage{tocloft, etoolbox} \providebool{newchap} \makeatletter \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}} \newcommand{\saved@chapter}{} \let\saved@chapter\chapter \renewcommand{\chapter}{% \addtocontents{lof}{\protect\setcounter{lofdepth}{0}} \@ifstar {\saved@chapter*}{\@dblarg\my@chapter}} \newcommand*{\my@chapter}[2][]{% \saved@chapter[#1]{#2}% \global\setbool{newchap}{true}% \addtocontents{lof}{\protect\setcounter{lofdepth}{1}} } \makeatother \begin{document} \tableofcontents \listoffigures \chapter*{Summary} \begin{figure}[ht]\centering\rule{4cm}{3cm}\caption{Non-listed.}\end{figure} \chapter{Introduction} \begin{figure}[ht]\centering\rule{4cm}{3cm}\caption[List of figures]{Listed.}\end{figure} \chapter{Methods} \chapter{Results} \begin{figure}[ht]\centering\rule{4cm}{3cm}\caption{Listed.}\end{figure} \end{document}Hope it helps,
Tom
April 18th, 2012 at 4:26 pm
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
April 18th, 2012 at 4:54 pm
Hi Michael,
Thanks for the idea with the
namerefpackage, 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{book} \usepackage{tocloft, etoolbox, nameref} \providebool{newchap} \makeatletter \let\oldcaption\caption \renewcommand{\caption}[2][\shortcaption]{% \def\shortcaption{#2} \ifbool{newchap}{% \addtocontents{lof}{\protect\contentsline{chapter}{% \nameref{cpt:\thechapter} \vspace{10pt}}{}}}{} \global\boolfalse{newchap}% \oldcaption[#1]{#2}} \newcommand{\saved@chapter}{} \let\saved@chapter\chapter \renewcommand{\chapter}{% \addtocontents{lof}{\protect\setcounter{lofdepth}{0}} \@ifstar {\saved@chapter*}{\@dblarg\my@chapter}} \newcommand*{\my@chapter}[2][]{% \saved@chapter[#1]{#2} \label{cpt:\arabic{chapter}}% \global\setbool{newchap}{true}% \addtocontents{lof}{\protect\setcounter{lofdepth}{1}}} \makeatother \begin{document} \tableofcontents \listoffigures \chapter*{Summary} \begin{figure}[ht]\centering\rule{4cm}{3cm}\caption{Non-listed.}\end{figure} \chapter{Introduction} \begin{figure}[ht]\centering\rule{4cm}{3cm}\caption[List of figures]{Listed.}\end{figure} \chapter{Methods} \chapter{Results} \begin{figure}[ht]\centering\rule{4cm}{3cm}\caption{Listed.}\end{figure} \end{document}April 19th, 2012 at 5:14 pm
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
April 20th, 2012 at 12:53 am
Thanks, I’m glad I was able to help. Cheers, Tom.
August 17th, 2012 at 1:34 am
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.
% Create a flag \providebool{newchapfig} \providebool{newchaptab} % Fix the \chapter command to set a flag \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{newchapfig}{true} \global\setbool{newchaptab}{true} } % Fix the \caption command to check the flag and insert a LOF line \let\oldcaption\caption \renewcommand{\caption}[2][\shortcaption]{% \def\shortcaption{#2} \ifbool{infigure}{ \ifbool{newchapfig}{% \addtocontents{lof}{\protect\contentsline{chapter}{% \ifbool{appendix}% {Appendix \thechapter \vspace{10pt}}% {Chapter \thechapter \vspace{10pt}}% }{}}}{} \global\boolfalse{newchapfig} }{} \ifbool{intable}{ \ifbool{newchaptab}{% \addtocontents{lot}{\protect\contentsline{chapter}{% \ifbool{appendix}% {Appendix \thechapter \vspace{10pt}}% {Chapter \thechapter \vspace{10pt}}% }{}}}{} \global\boolfalse{newchaptab} }{} \oldcaption[#1]{#2} } % Add in appendix detection \providebool{appendix} \let\oldappendix\appendix \renewcommand{\appendix}{% \global\booltrue{appendix}% \oldappendix% } % Add in figure/table detection \providebool{infigure} \providebool{intable} \newcommand{\setfigure}{% \global\booltrue{infigure}% \global\boolfalse{intable}% } \newcommand{\settable}{% \global\booltrue{intable}% \global\boolfalse{infigure}% }August 20th, 2012 at 7:45 am
Hi Jolyon!
Wow, thanks for sharing the code. Here is a little extension that automatically sets
infigureandintablewhenever the environments are called (taken from here). I also added a document skeleton for easier reuse.Thanks again!
Tom.
\documentclass{report} \usepackage{tocloft, etoolbox} \makeatletter % --> Jolyon's code goes here %Automatically call \setfigure and \settable \makeatletter \let\oldfigure\figure \def\figure{\@ifnextchar[\figure@i \figure@ii} \def\figure@i[#1]{\oldfigure[#1]\setfigure} \def\figure@ii{\oldfigure\centering} \let\oldtable\table \def\table{\@ifnextchar[\table@i \table@ii} \def\table@i[#1]{\oldtable[#1]\settable} \def\table@ii{\oldtable\settable} \makeatother \begin{document} \tableofcontents \listoffigures \listoftables % Your document \end{document}August 20th, 2012 at 8:52 pm
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{infigure} \providebool{intable} \AtBeginEnvironment{figure}{ \global\booltrue{infigure}% \global\boolfalse{intable}% } \AtBeginEnvironment{table}{ \global\booltrue{intable}% \global\boolfalse{infigure}% }August 21st, 2012 at 1:57 am
Thank you for the update, really nice!
Best, Tom.
January 22nd, 2013 at 3:24 pm
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{toc}{chapter}{List of Figures} \listoffigures \addcontentsline {toc}{chapter}{List of Tables} \listoftables \addcontentsline{toc}{chapter}{List of Equations} \newcommand{\listequationsname}{List of Equations} \newlistof{equations}{equ}{\listequationsname} \newcommand{\equations}[1]{% \addcontentsline{equ}{equations}{\protect\numberline{\theequation}#1}\par} \listofequations \addcontentsline{toc}{chapter}{List of Abbreviations} \chaptermark{List of Abbreviations} \printnomenclature[2.5cm] \newcommand{\newchapter}[1]{% \chapter{#1} \addtocontents{lof}{\contentsline{chapter}{% Chapter \thechapter \vspace{5pt}}{}} \addtocontents{lot}{\contentsline{chapter}{% Chapter \thechapter \vspace{5pt}}{}} \addtocontents{listofequations}{\contentsline{chapter}{% Chapter \thechapter \vspace{5pt}}{}}} \providebool{newchap} \newcommand{\newchap}[1]{% \chapter{#1} \global\setbool{newchap}{true} } \let\oldcaption\caption \renewcommand{\caption}[2][\shortcaption]{% \def\shortcaption{#2} \ifbool{newchap}{% \addtocontents{lof}{\protect\contentsline{chapter}{% Chapter \thechapter \vspace{5pt} }{}}{} \addtocontents{lot}{\protect\contentsline{chapter}{% Chapter \thechapter \vspace{5pt} }{}}{} \addtocontents{listofequations}{\contentsline{chapter}{% Chapter \thechapter \vspace{5pt}}{}}{}} \global\boolfalse{newchap} \oldcaption[#1]{#2} } \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} } \makeatotherI’m also a little confused as to which command I should begin a chapter with….Still using
\chapterCheers
January 24th, 2013 at 10:09 am
Hi!
Hmm, that’s quite a lot of stuff to look at. One thing that I saw, you defined the list as
equationswith 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.
January 24th, 2013 at 10:28 pm
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]{book} \usepackage{graphicx} % Required for including pictures \usepackage{etoolbox} % --------------------------------------------------- Add Chapters to the ListOfFigures \makeatletter % Create a flag \providebool{newchapfig} \providebool{newchaptab} % Fix the \chapter command to set a flag \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{newchapfig}{true} \global\setbool{newchaptab}{true} } % Fix the \caption command to check the flag and insert a LOF line \let\oldcaption\caption \renewcommand{\caption}[2][\shortcaption]{% \def\shortcaption{#2} \ifbool{infigure}{ \ifbool{newchapfig}{% \addtocontents{lof}{\protect\contentsline{chapter}{% \ifbool{appendix}% {Appendix \thechapter \vspace{10pt}}% {Chapter \thechapter \vspace{10pt}}% }{}}}{} \global\boolfalse{newchapfig} }{} \ifbool{intable}{ \ifbool{newchaptab}{% \addtocontents{lot}{\protect\contentsline{chapter}{% \ifbool{appendix}% {Appendix \thechapter \vspace{10pt}}% {Chapter \thechapter \vspace{10pt}}% }{}}}{} \global\boolfalse{newchaptab} }{} \oldcaption[#1]{#2} } % Add in appendix detection \providebool{appendix} \let\oldappendix\appendix \renewcommand{\appendix}{% \global\booltrue{appendix}% \oldappendix% } % Add in figure/table detection \providebool{infigure} \providebool{intable} \newcommand{\setfigure}{% \global\booltrue{infigure}% \global\boolfalse{intable}% } \newcommand{\settable}{% \global\booltrue{intable}% \global\boolfalse{infigure}% } % Add in figure/table detection \providebool{infigure} \providebool{intable} \AtBeginEnvironment{figure}{ \global\booltrue{infigure}% \global\boolfalse{intable}% } \AtBeginEnvironment{table}{ \global\booltrue{intable}% \global\boolfalse{infigure}% } % --------------------------------------------------- END Add Chapters to the ListOfFigures \usepackage{hyperref} % used for \nameref \begin{document} \tableofcontents \listoffigures \chapter{Sonny} \begin{figure}[htbp] \centering \includegraphics[width=\linewidth]{test} \caption[short for lof]{long figure caption} \label{fig:default} \end{figure} \chapter{Hello World} \begin{figure}[htbp] \centering \includegraphics[width=\linewidth]{test} \caption[short for lof]{long figure caption} \label{fig:default} \end{figure} \end{document}January 27th, 2013 at 7:44 am
Hi!
Thanks for the nice code example. Obviously,
hyperrefredefines thechapterandfigurecommands, 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.
January 28th, 2013 at 1:31 pm
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
January 28th, 2013 at 2:16 pm
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]{book} \usepackage{etoolbox} \usepackage{hyperref} % used for \nameref \makeatletter % Create a flag \providebool{newchapfig} \providebool{infigure} % Fix the \chapter command to set a flag \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{newchapfig}{true}} % Fix the \caption command to check the flag and insert a LOF line \let\oldcaption\caption \renewcommand{\caption}[2][\shortcaption]{% \def\shortcaption{#2} \ifbool{infigure}{% \ifbool{newchapfig}{% \addtocontents{lof}{\protect\contentsline{chapter}{% Chapter \thechapter \vspace{10pt}}{\protect\relax}{}} }{} \global\boolfalse{newchapfig} }{} \oldcaption[#1]{#2} } % Add in figure/table detection \newcommand{\setfigure}{% \global\booltrue{infigure}} % Add in figure/table detection \providebool{infigure} \AtBeginEnvironment{figure}{ \global\booltrue{infigure}} \begin{document} \tableofcontents \listoffigures \chapter{Hello World} \begin{figure}[htbp] \centering \rule{\textwidth}{0.75\textwidth} \caption{long figure caption} \label{fig:default2} \end{figure} \end{document}