The tocloft package implements a number of commands to customize the table of contents, list of figures and list of tables. One of these commands provided by the package adds whitespace before entries. This is particularly neat for short documents, but might also improve the readability in longer content lists. However, it has its limitations and I will therefore introduce a more flexible approach allowing conditional addition of whitespace.
Lets assume we have the following chapters and sections in our thesis.
\documentclass[12pt]{report} \begin{document} \tableofcontents \chapter{Introduction} \chapter{Main chapter one} \section{Background} \section{Methods} \section{Results} \section{Conclusion} \chapter{Main chapter two} \section{Background} \section{Methods} \section{Results} \section{Conclusion} \chapter{Main chapter three} \section{Background} \section{Methods} \section{Results} \section{Conclusion} \chapter{Conclusion} \end{document}
Let’s add some whitespace between sections to spread the contents more evenly over the entire page. Here, I use \cftbeforeXskip
of the tocloft package to add some whitespace before every section entry. X is replaced by chap
, sec
, etc. (see documentation).
\usepackage{tocloft} \setlength{\cftbeforesecskip}{10pt}
Frankly, it doesn’t look great. Better would be to add whitespace after chapter entries and to keep the sections together. \cftXafterpnum
does the trick.
\usepackage{tocloft} \renewcommand{\cftchapafterpnum}{\vspace{10pt}}
Much better, but now we get extra whitespace after “Introduction” because this chapter has no sections. Without sections, the whitespace after the previous and before the next chapter creates an uneven picture.
The solution is to “conditionally” add whitespace depending on whether a chapter sections or not. This requires the etoolbox
package. It implements functionality to “append” and “prepend” code to other commands. In the example below, I use the “prepend”-command: \preto
. Similarly, \appto
can be used to “append” something. See the package documentation for more details.
We would like to have additional whitespace before the first section of a chapter. Therefore, before starting a new section we check if the section counter has been reset to 0. If so, we add whitespace to the table of contents using \addtocontents
.
\usepackage{etoolbox} \preto\section{% \ifnum\value{section}=0\addtocontents{toc}{\vskip10pt}\fi }
Using the same idea, we could add chapter headings between list of figure entires for a better separation.
\usepackage{etoolbox, tocloft} \preto\figure{% \ifnum\value{figure}=0\addtocontents{lof}{{\bfseries Chapter \thechapter\vskip10pt}}\fi }
Note: Without loading tocloft
chapter headings are indented except for the first.
Before:
After:
And possibly lots of other cool stuff. Let me know your ideas below! 🙂
Jan
Using
tocloft
andetoolbox
, the header offancyhdr
disappears on the TOC page. Using[title]{tocloft}
it returns, but the spacing is like without usingtocloft
andetoolbox
. Any idea?tom
Hi Jan,
I tried to reproduce what you describe, without success. Please send a minimal working example similar to the code below to illustrate your problem. Thanks, Tom.
rDuck
Hi there, excellent guide, however I have been unable to get the chapter headings in list of figures to work while using the chemstyle package, loading order doesn’t seem to matter either. and i cant see any problems in the log file, but the chapters simply disappear when i run this package, do you know why? I included a minimal (non) working example below.
tom
Hi!
I believe that’s because the package or one of its dependencies redefines the list of figure using hooks (
\AtEndOfPackage
and\AtBeginDocument
). It works if you move\preto...
further down. Place it just after\begin{document}
.Tom
rDuck
Awesome! worked like a charm, thank you so much!
tom
You are very welcome! Tom
Reity
YES, YES AND YES…. thank u… the list of figures changes compiled PERFECTLY… at the first shot… and my thesis was already finished… I just added the commands:
tom
Great, thanks for the feedback and all the best with your thesis! Tom
Amsul
Thanks for this Tom, I had a quick question, could you tell me instead of just chapter 1 in the list of figures, I want the whole chapter title how I would go about doing that?
tom
Hi Amsul,
Thanks for the question. Here’s how to produce the name of the chapter in addition to the number.
Amsul
Thanks for the reply Tom. I ran into a small problem, with the “chaptername” ‘s c captialized, it would not work, and kept giving me an error. When I made it smaller case, it compiled However instead of the actual chapter name it just kept giving me chapter 1 chapter. Any ideas as to what might be going on?
tom
You might be using a package that conflicts with this code. Also,
\chaptername
doesn’t work, because it is reserved for ‘Chapter’. Actually, I should have used the command instead of the word (see code below). Did you try with another word for\Chaptername
, e.g.\chapterdef
?If this doesn’t work, please provide a minimal working example.
Thanks, Tom
Amsul
Thanks Tom, I think that might be true, I have way too many packages and renewcommands I had to use. I have had to change a lot of things about the chapter heading so that might be why. I’ll try a few things and if not I will post a minimal working example here. Thanks.
tom
Great! Good luck and let me know should you get stuck again. Best, Tom
Amsul
Tom,
I tried doing the same thing with tables as you did with figures by changing lof to lot and figure to table in the code, but for some reason my first table in chapter 2 does not show up under chapter but rather by itself. Might you know a reason this is happening?
Amsul
Actually, just realized, it is doing so with the figure as well, figure 2.1 show up above chapter 2. for some reason?
tom
Hi Amsul,
Might be a package that you load, which causes a conflict. Please provide a minimal working example so I can reproduce the problem. Thanks.
Here is a basic MWE that works:
Amsul
Got it to work, apparently thee was some spacing issue
tom
Great, thanks for the feedback!
doragasu
I have also tried it. It works flawlessly with the table of contents, but It makes weird things with the list of figures and the list of tables. Sometimes “Chapter #” appears after the item it corresponds, and also sometimes “Chapter #” gets printed twice…
So I’m using it only for the table of contents. Thanks!
tom
Would you mind sending a minimal working example to illustrate the issue? I tried and it works for me with figures and tables…
MANSI CHAHAL
Sir, how can I modify the default top margin in table of content page and some other pages like list of figures?
tom
Hi there,
Standard chapters are used to generate these lists. You could temporarily change the chapter margins. See here for more details.
HTH, Tom
Mark Sconce
How do you increase or control horizontal spacing between section number like 2.1 and section title like Background?
tom
Hi Mark,
Apologies for the late reply. See here for a possible solution.
HTH, Tom