3

I wrote the following TeX program. This is copied straight from page 8 (Figure 1.2) of lshort.pdf.

\documentclass[a4paper,11pt]{article}
% define the title
\author{H.~Partl}
\title{Minimalism}
\begin{document}
% generates the title
\maketitle
% insert the table of contents
\tableofcontents
\section{Some Interesting Words}
Well, and here begins my lovely article.
\section{Good Bye World}
\ldots{} and here it ends.
\end{document}

But when I convert it to pdf using the pdflatex foo.tex command, I see the following output in the PDF.

Screenshot of TeX Output showing no content under the Contents section

Why doesn't the \tableofcontents command generate any content under the "Contents" heading?

Lone Learner
  • 3,226
  • 24
  • 44
  • 6
    You need to compile twice. – cfr Jun 15 '16 at 03:45
  • @cfr Thanks. I see that the first pdflatex foo.tex generates foo.toc and foo.pdf with an empty table of contents. Then the second pdflatex foo.tex perhaps uses the generated foo.toc and creates foo.pdf with a populated table of contents. Is there a way to generate only foo.toc separately in the first compilation without generating foo.pdf? – Lone Learner Jun 15 '16 at 04:38
  • That would not make any sense. The toc contents are made during the compilation, the compilation process creates the pdf. The cross references also need several compilations to work, so does citations – daleif Jun 15 '16 at 05:55
  • @daleif I think the answer to what I was asking is: latex foo.tex && latex foo.tex && dvipdf foo.dvi. – Lone Learner Jun 15 '16 at 07:21
  • @LoneLearner: TeX is a linear machine, it can't know in advance which chapters etc. there will be. The information is written to the .aux file and this is read again at the second run (and creates the .toc etc. files, so two runs are necessary! –  Jun 15 '16 at 07:40
  • @LoneLearner or pdflatex foo.tex && pdflatex foo.tex :) – cgnieder Jun 15 '16 at 07:47

3 Answers3

5

Since LaTeX can not guess which chapters, sections, figures or tables will appear in the document later on, the content to be display by \tableofcontents is empty if it is used somewhere in the document, i.e. it needs two runs of LaTeX (at least), so even placing \tableofcontents at the end of the document won't work if LaTeX is run only once!

The information generated by \chapter etc. are written to the .aux file with \addcontentsline (or \addtocontents) and this .auxfile is read again at the 2nd run of LaTeX, thereby generating the real .toc or .lof or .lot etc. file (unless `\nofiles is stated).

Thus: Compile twice (at least) to get the information and the typesetting of ToC.

Details about the generation of ToC etc. can be found here: Mimicking LaTeX's "table of contents" functionality

  • One may assume after reading your first sentence, "Since LaTeX can not guess which chapters, sections, figures or tables will appear in the document later on, the content to be display by \tableofcontents is empty if it is used at the beginning of a document, ...", that the table of contents would be populated after just one run if \tableofcontents is used at the end of the document just before \end{document}. However this does not seem to be true. I tried doing this and still got an empty table of contents after one run. – Lone Learner Jun 16 '16 at 01:46
  • 1
    That's why I said that the contents are written to the .aux file and this file is read again in the second run. –  Jun 16 '16 at 04:19
0

To get the content, you need to keep *.aux and *.toc after compiling. You can check your setting whether you clean them automatically.

0

For a really short answer:

When you first run pdflatex on the .tex source, the pdf produced doesn't contain a table of contents.

When you run it a second time, pdflatex will produce the contents table from the working files it generated the first time.

Solution: run it a second time.

Owl
  • 101