Tag Archives: addcontentsline

Adding additional structure to the list of figures/tables

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

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.


10 ways to customize toc/lof/lot

I put together this list of 10 ways to customize the Table of Contents, List of Figures and List of Tables. Some of them are pretty common, some may be new to you. Hope you enjoy the list…

Some pieces of code below require the tocloft package which provides extensive customization functionality for table of contents, list of figures and list of tables. It will be indicated wherever the tocloft or any other package needs to be loaded in the preamble.

Note: I will use the abbreviations toc for table-of-contents, lof for list-of-figures and lot for list-of-tables in the article below.

1. Change the lists headings

Change the heading can be done without loading any specific package. Obviously, the name has to be changed before creating the list.

\renewcommand\contentsname{}
\tableofcontents
\renewcommand\listfigurename{}
\listoffigures
\renewcommand\listtablename{}
\listoftables

2. Add “Page” above page numbers

Several people asked me how to place the word “Page” on top of the page numbers in toc/lof/lot. Here is how you do it:

\tableofcontents
\addtocontents{toc}{~\hfill\textbf{Page}\par}
\chapter{...}

"Page" above page numbers

It works the same way with lof and lot. The code was taken from here.

3. Change depth of entries

You can change the depth, i.e. how many levels shall be printed using the respective counter:

\setcounter{tocdepth}{1}
\tableofcontents

0: chapter (not available for \documentclass{article),
1: section,
2: subsection, etc.
The default depth is 3, subsubsection.
Similarly, including subfigures and subtables can be achieved using:

\setcounter{lofdepth}{2}
\setcounter{lotdepth}{2}

4. Roman page numbers for toc/lof/lot

To get a different page number style for toc/lof/lot, use:

\pagenumbering{}

and change it back to arabic before the first chapter starts.

Available styles are arabic, roman, Roman, alph and Alph.

Complete code example:

...
\pagenumbering{roman}
\tableofcontents
\listoffigures
\listoftables
\clearpage
\pagenumbering{arabic}
\chapter{...}
...

5. Hyperlinks to content

Loading the hyperref package will let you navigate from toc/lof/lot entries directly to the respective content:

\usepackage{hyperref}

If you only want the page number to be clickable, you’ll need to load the package with the following option:

\usepackage[linktocpage=true]{hyperref}

6. Adding the lists to toc

Adding entries to toc/lof/lot can be done manually with a single command. Usually, to be sure the page number is correct, it’s advisable to add entries directly before or after the actual content to list. And here is how:

\addcontentsline{}{}{}

The file line(s) shall be added (toc, lof or lot), the type of the entry (chapter, figure, etc.) and the entry text itself.

To add the three lists to the toc use:

\tableofcontents
\addcontentsline{toc}{chapter}{Contents}
\listoffigures
\addcontentsline{toc}{chapter}{List of Figures}
\listoftables
\addcontentsline{toc}{chapter}{List of Tables}

Adding toc/lof/lot to contents

7. Changing the font of entries

To change the way the actual entries look, you can make use of a command provided by the tocloft package. The command will affect the number as well as the text. However, it will not change the page number and the separator (if there are any). The nice thing here is that since it’s placed just in front of the sequence number, you can also use it to add words such as “Chapter 1: Biology … Here are a few examples for illustration:

\usepackage{tocloft}
\renewcommand{\cftchapfont}{\scshape}
\renewcommand{\cftsecfont}{\bfseries}
\renewcommand{\cftfigfont}{Figure }
\renewcommand{\cfttabfont}{Table }

Try it and make sure you don’t forget the space behind Figure_ and Table_, otherwise it will be glued to the number.

8. Creating you own list of …

Again, the tocloft package has to be loaded. First, define a new list environment as well as the item which are later being listed. In this case, we are defining lemmas, but it could also be proofs, equations, a special list of figures, footnotes, anything. The last line of the lemma command defines how lemmas should be numbered, here “chapter.lemma”.

\usepackage{tocloft}
...
\newcommand{\listoflemmas}{List of Lemmas}
\newlistof{lemma}{lem}{\listoflemmas}
\newcommand{\lemma}[1]{%
  \refstepcounter{lemma}
  \par\noindent\textbf{Lemma \thelemma. #1}
  \addcontentsline{lem}{lemma}
  {\protect\numberline{\thechapter.\thelemma}#1}\par
}
...
\listoflemmas
...
\lemma{My first lemma}
\label{lem:lemma1}

You can find a complete code example here.

9. Alternative text in toc/lof/lot for headings and captions

In case your caption is too long for the lof/lot, you can give it an alternative text. Similarly, the headings can be given an alternative name in the toc:

\caption[]{}
\chapter[]{}

10. Chapter specific tocs with minitoc

Finally, minitoc produces beautiful tocs at the beginning of every chapter (or any other heading) and therefore, I will dedicate this 10th and last entry to that package:

\usepackage{minitoc}
...
\dominitoc
\tableofcontents
...
\chapter{...}
\minitoc

A minitoc example

Here is an earlier post I wrote about the package as well as the complete documentation.

Wow, that’s the longest post I ever wrote. Hope you’ve enjoyed my list, have fun customizing :-) .


Glossary in Latex

A glossary is a nice thing to have in a report and usually very helpful. As you probably can imaging, it is very easy to create in Latex. Nevertheless, there are a few things to be done, especially generating the glossary-files.

First you have to tell Latex to use the glossary package and to create the glo-file containing all the glossar-entries in your document:

\usepackage{glossary}
\makeglossary

Next you have to add glossary entries to your document. They are of the following form:

\glossary{name={entry name}, description={entry description}}

Note: Usually, the glossary-entry should be added to keywords where they first appear.

A glossary-entry produces by default the following format:

"entry name" "entry description (on multiple lines if necessary)", "page number"

And finally you have to tell Latex where to place you glossary inside the document which is done by the following command at the location you want to produce the glossary:

\printglossary

Optinally, you can reference to the glossary in the index (toc-file) by adding the following command after \printglossary":

\addcontentsline{toc}{chapter}{Glossary}

What you do first is generate your PDF once. An ist-file as well as a glossary file (*.glo) are generated. The glossary-file contains all the glossary entries found in the document in plain text. Next you type the following command in the command-line:

makeindex document.glo -s document.ist -t document.glg -o document.gls

generating the two files with the extensions *.gls and *.glg. If entries are ignored or rejected, which can be seen either in the glg-file or directly in the output of the makeindex-command, you have to check your glossary entries. The important of the two files is the *.gls-file, as it is used by Latex for the actual glossary. You now need to re-generate the PDF and if everything works fine, your glossary should appear where you wanted it.

Good luck:-)


Follow

Get every new post delivered to your Inbox.

Join 316 other followers