Creating a Table of Contents
5.4 Creating a Table of Contents
Once you have all your sectioning commands , such as \chapter and \section , you can create a table of contents with the command
\tableofcontents
Definition
This command should go where you want your table of contents to appear (usually after \maketitle ). The KOMA-Script classes provide two options [FAQ: The format that govern the format of the table of contents: toc=graduated and toc= of the Table of flat . The first is the default and indents the different sectioning levels. The Contents, etc ]
second doesn’t use any indentation.
Example:
\documentclass[ 12pt,toc=flat ]{ scrreprt }
Input
You may recall from the previous section that the sectioning commands all had an optional argument ⟨short title⟩. If your chapter or section title is particularly long, you can use ⟨short title⟩ to specify a shorter title that [FAQ: My section should go in the table of contents. 5.1 The longer title (given by the other title is too wide
for the page and in the page header, depending on the page style.
header ] header ]
L A TEX processes all source code sequentially, so when it first encoun- ters the \tableofcontents command, it doesn’t yet know anything about the chapters, sections etc. So the first time the document is L A TEXed the necessary information is written to the table of contents (.toc) file (see Sec- tion 2.4 ). The subsequent pass reads the information in from the .toc file, and generates the table of contents. You will therefore need to L A TEX your document twice to make sure that the table of contents is up-to-date.
[FAQ: Numbers too large in table
Adding Extra Information
of contents, etc ] The starred versions of the sectional commands (such as \chapter*) don’t
get added to the table of contents. It may be that you want to add it, in which case you need to use
\addcontentsline{ ⟨toc⟩ }{ ⟨section unit⟩ }{ ⟨text⟩ }
Definition
after the heading. The first argument ⟨toc⟩ is the file extension without the dot. As mentioned above, the table of contents file has the extension .toc, so the first argument should be toc ( later in Chapter 7 (Floats), we’ll be adding a list of figures and a list of tables, and those have file extensions .lof and .lot, respectively). The second argument ⟨section unit⟩ is the name of the section unit. This is just the name of the relevant sectioning command without the backslash. The final argument ⟨text⟩ is the entry text. For example (using scrreprt class):
\chapter ↑ Input * { Acknowledgments } \addcontentsline{ toc }{ chapter }{ Acknowledgements }
↓ Input
Exercise 11 (Creating a Table of Contents)
Try modifying your document so that it has a table of contents. Modifi- cations from the previous exercise are illustrated like this:
↑ Code
\documentclass[ 12pt ]{ scrreprt } \usepackage{ datetime } \title{
A Simple Document } \author{ Me }
\begin{ document } \maketitle \tableofcontents
\begin{ abstract }
A brief document to illustrate how to use \LaTeX . \end{ abstract }
\chapter{ Introduction } \section{ The First Section } This is a simple \LaTeX\␣ document. Here is the first paragraph. \section{ The Next Section } Here is the second paragraph \footnote{ with a footnote } .
As you can see it’s a rather short paragraph, but not as short as the previous one. This document was created on: \today\␣ at \currenttime .
\chapter{ Another Chapter } Here’s another very interesting chapter.
We’re going to put a picture here later. \chapter * { Acknowledgements }
I would like to acknowledge all those very helpful people who have assisted me in my work.
\appendix \chapter{ Tables }
We will turn this tabular environment into a table later. \begin{ tabular }{ lrr }
& \multicolumn{ 2 }{ c }{\bfseries Expenditure }\\
& \multicolumn{ 1 }{ c }{ Year1 } & \multicolumn{ 1 }{ c }{ Year2 }\\ \bfseries Travel & 100,000 & 110,000 \\ \bfseries Equipment & 50,000 & 60,000 \end{ tabular }
\end{ document }
↓ Code
If your table of contents doesn’t come out right, try L A TEXing it again. (Again, you can download this file.)
You might want to try experimenting with the toc=flat class options to see what difference it makes:
\documentclass[ 12pt,toc=flat ]{ scrreprt }
Input