60 Comments

  1. Tania

    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 :(

    • 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.

  2. 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

    • 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{\newchapter}[1]{%
      	\chapter{#1}
      	\addcontentsline{lof}{chapter}{%
      		Chapter \thechapter: #1 \vspace{10pt}
      	}
      	\addcontentsline{lot}{chapter}{%
      		Chapter \thechapter: #1 \vspace{10pt}
      	}
      }

      Best, Tom.

  3. 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.[…]

    \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}
    }
    • 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.

  4. 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

    • 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]{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}
      }
      \makeatother

      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

      • 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

        \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}
      • 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:

        \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:

        • Graphicx is the extended/enhanced graphics package, you won’t need to load them both.
        • Subfigure is obsolete and was replaced by subfig (see my post).
        • Hyperref should always be the last package loaded.
        • Ideally, in a minimal example you would remove everything that is not necessary to reproduce the problem, including all packages.
        • I used the document class report for testing, because it’s a standard class.
        • Personally, I prefer relative figures sizes, e.g. width=0.5\textwidth, which are specifically useful for subfigures.

        I hope it works this time and good luck with your thesis.

        Cheers, Tom

  5. 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

    • 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}}
      \makeatother

      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

  6. 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.

    \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

    Thanks in advance

    • 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 caption command of the caption 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

      • 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.

  7. Diaa

    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

    • 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]{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}
      • Diaa

        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 :)

      • 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.

  8. 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?

    • 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 \appendix call and changes the definition accordingly.
      Best, Tom.

  9. 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:

    \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}
    • 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.

      • 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

      • 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{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

      • 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

      • 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{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}
  10. 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

  11. 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}%
    }
    
    • 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.

      \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}
      • 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}%
        }
        
  12. 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)

     \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}
    }
    \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{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.

  13. 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}
    • Hi!

      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{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

      • 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}
  14. 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:

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

    Here is the error:
    ! LaTeX Error: Something’s wrong–perhaps a missing \item.
    l.84 \defcounter
    {refsection}{0}\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.

  15. 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

    \documentclass[12pt,a4paper,twoside]{memoir}
    \usepackage{etoolbox,graphicx}
    \providebool{newchap}
    \newcommand{\mytitle}[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{10pt}}{}}
            \addtocontents{lot}{\protect\contentsline{chapter}{%
                Chapter \thechapter \vspace{10pt}}{}}
        }{}
        \global\setbool{newchap}{false}
        \oldcaption[#1]{#2}
    }
    
    \begin{document}
    \newpage
    \renewcommand{\chaptermark}[1]{\markboth{#1}{}}
    \renewcommand{\sectionmark}[1]{\markright{#1}{}}
    \setcounter{page}{1}
    \pagenumbering{roman}\newpage
    \tableofcontents\newpage
    \listoffigures\newpage
    \listoftables\newpage
    \pagenumbering{arabic}
    \mytitle{Introduction}  \label{ch:1}
    \begin{figure}[t]
    \centering
    \includegraphics[width=5.8in]{ch2_fig2}
    \caption{Figure 1}
    \label{ch1:fig2}
    \end{figure}
    \newpage
    \mytitle{Mathematical Background} \label{ch:2}
    \begin{figure}[t]
    \centering
    \includegraphics[width=5.8in]{ch2_fig2}
    \caption{Figure 2}
    \label{ch2:fig1}
    \end{figure}
    \newpage
    \mytitle{Chapter 3} \label{ch:3}
    \begin{table}[t]
    \renewcommand{\arraystretch}{1.5}
    \centering
    \begin{tabular}{|l | c |c |c |c |}
    \hline
    1, 3, 4 & $5L$ & $5L$ & $2L$ & $2L$\\
    \hline
    \end{tabular}
    \caption{Table 1}
    \label{ch3:table1}
    \end{table}
    \newpage
    \mytitle{Chapter 4} \label{ch:4}
    \begin{table}[t]
    \renewcommand{\arraystretch}{1.5}
    \centering
    \begin{tabular}{|l | c |c |c |c |}
    \hline
    First BC & $L^2+2L$ & $L^2+L$ & $L^2+3L$ & $L^2+2L$\\
    Total & $2L^2+9L$ & $2L^2+8L$ & $2L^2+6L$ & $2L^2+5L$\\
    \hline
    \end{tabular}
    \caption{Table 2}
    \label{ch4:table1}
    \end{table}
    \def\thechapter{A}
    \newpage
    \end{document}
    • 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

        \preto\table{%
          \ifnum\value{table}=0\addtocontents{lot}{{\bfseries Chapter \thechapter\vskip10pt}}\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]{report}
        \usepackage{etoolbox, tocloft, graphicx, blindtext, caption}
        \preto\figure{%
          \ifnum\value{figure}=1\addtocontents{lof}{{\bfseries Chapter \thechapter\vskip10pt}}\fi
        }
        \preto\table{%
          \ifnum\value{table}=0\addtocontents{lot}{{\bfseries Chapter \thechapter\vskip10pt}}\fi
        }
        \begin{document}
        \tableofcontents
        \listoffigures
        \listoftables
        \chapter{First chapter}
        \begin{figure}[ht]\centering\includegraphics[width=0.5\textwidth]{figure1}\caption[]{Decorative figure}\end{figure}
        \blindtext
        \begin{figure}[ht]\centering\includegraphics[width=0.5\textwidth]{figure1}\caption{Regular figure}\end{figure}
        \chapter{Second chapter}
        \begin{figure}[ht]\centering\includegraphics[width=0.5\textwidth]{figure1}\caption[]{Decorative figure}\end{figure}
        \begin{table}[ht]\caption{Some simple table}\centering\begin{tabular}{|c|c|}\hline a&b\\c&d\\\hline\end{tabular}\end{table}%
        \blindtext
        \chapter{Third chapter}
        \blindtext
        \end{document}
  16. 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!

    • 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]{report}
      \usepackage{etoolbox, tocloft, graphicx, wrapfig, blindtext}
      \preto\figure{%
        \ifnum\value{figure}=0\addtocontents{lof}{{\bfseries Chapter \thechapter\vskip10pt}}\fi
      }
      \preto\wrapfigure{%
        \ifnum\value{figure}=0\addtocontents{lof}{{\bfseries Chapter \thechapter\vskip10pt}}\fi
      }
      \begin{document}
      \tableofcontents
      \listoffigures
      \chapter{First chapter}
      \begin{figure}[ht]\centering\rule{0.5\textwidth}{0.3\textwidth}\caption{Some normal figure}\end{figure}
      \begin{wrapfigure}{r}{0.5\linewidth}
      \centering
      \rule{0.9\linewidth}{0.75\linewidth}
      \caption{Some wrapfigure.}
      \end{wrapfigure}
      \blindtext
      \chapter{Second chapter}
      \end{document}
  17. 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.

Leave a Reply