[Tex/LaTex] Wrong page numbering in the Table of contents, of figures and of tables

page-numberingtable of contents

my problem is related to the number of pages in the table of contents, list of figures and list of contents. Everything in my .pdf works well except that! I don't understand why!
Could you help me?

In the following my main:

\begin{document}
\hyphenation{Com-po-si-to-ri}
\hyphenation{Giu-sep-pe}
\hyphenation{Ba-lu-stra-des}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}
\fancyhf{} 
\fancyhead[LE,RO]{\bfseries\thepage}
\fancyhead[LO]{\bfseries\rightmark}
\fancyhead[RE]{\bfseries\leftmark}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}
\addtolength{\headheight}{0.5pt} 
\fancypagestyle{plain}{
\fancyhead{} 
\renewcommand{\headrulewidth}{0pt} % e la linea
}

\includepdf[pages={1}]{Frontespizio.pdf}

\newpage
\thispagestyle{empty}
\clearpage{\pagestyle{empty}\cleardoublepage}
\selectlanguage{english}
\tableofcontents
\clearpage{\pagestyle{empty}\cleardoublepage}
\listoffigures
\clearpage{\pagestyle{empty}\cleardoublepage}
\listoftables
\clearpage{\pagestyle{empty}\cleardoublepage}

\large
\setcounter{secnumdepth}{-1}
\selectlanguage{italian}
\input{Sommario}
\clearpage{\pagestyle{empty}\cleardoublepage}
\selectlanguage{english}
\input{Acronym.tex}
\clearpage{\pagestyle{empty}\cleardoublepage}
\input{Abstract}
\clearpage{\pagestyle{empty}\cleardoublepage}
\input{Introduction.tex}
\clearpage{\pagestyle{empty}\cleardoublepage}
\clearpage
\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{4}
\onehalfspacing
\input{Iso.tex}
\clearpage{\pagestyle{empty}\cleardoublepage}
\input{Literature.tex}
\clearpage{\pagestyle{empty}\cleardoublepage}
\input{Storia.tex}
\clearpage{\pagestyle{empty}\cleardoublepage}
\input{IRMeasurement.tex}
\clearpage{\pagestyle{empty}\cleardoublepage}
\input{ComputerModelling.tex}
\clearpage{\pagestyle{empty}\cleardoublepage}
\input{Discussion.tex}
\clearpage{\pagestyle{empty}\cleardoublepage}
\input{Conclusions.tex}
\clearpage{\pagestyle{empty}\cleardoublepage}
\input{Bibliografia.tex}
\clearpage{\pagestyle{empty}\cleardoublepage}
\input{Ringraziamenti.tex}
\clearpage{\pagestyle{empty}\cleardoublepage}


\end{document}

Best Answer

You need at least two up to three (or more) LaTeX runs to get the table of contents right:

During one LaTeX run the following happens:

  • \begin{document}: The .aux files are read, entries for the table of contents or list of figures/tables are ignored (because of \let\@writefile\@gobbletwo).

  • \tableofcontents/\listoffigures/\listoftables call \@startoc{<ext>} (<ext> = toc, lof, or lot).

    1. It reads the file \jobname.<ext>, the table of contents (or list of ...) is set.

    2. The \jobname.<ext> is cleared and opened for writing.

  • \chapter, \section, \caption, ... internally call \addcontentsline{}{...}{...}and\addtocontents{}{...}. These write\@writefile{}{...}commands into the.auxfile. This can also happen before\tableofcontents` and friends are called.

  • \end{document}. After the last page is shipped out, the .aux file is closed and read in again. Now the \@writefile{<ext>}{<contents>} commands are executed and the <contents> is written to the correspondent \jobname.<ext> file or ignored, if \jobname.<ext> is not opened. For example, if there are figure captions, but \listoffigures is not called, then \jobname.lof is not written.

That means the printed table of contents (list of figures, ...) has the status of the previous LaTeX run. In the first LaTeX run, the \jobname.<ext> files are not yet written and the table of contents and the lists are empty. Thus the minimal requirement are two LaTeX runs.

If the table of contents and the lists are not numbered independently, then it can happen, that there are so many entries, that there are more pages than an empty list would need. Then the following page number after the lists increases as all following entries for the lists. Then another LaTeX run is needed.

For example, two side setting with \cleardoublepage:

\begin{document}
\tableofcontents
\cleardoublepage
\chapter{...}
...

An empty table of contents needs one page for the title (Contents) and an empty page (because of \cleardoublepage). The table of contents is three pages long:

  1. First LaTeX run:

    1. Page: Contents <otherwise empty>
    2. Page: <empty>
    3. Page: Chapter 1 ...
    
  2. Second LaTeX run:

    1. Page: Contents, Chapter 1 -> page 3, <other entries>
    2. Page: <other entries>
    3. Page: <other entries>
    4. Page: <empty>
    5. Page: Chapter 1 ...
    

    Thus the page number is wrong, but LaTeX does not warn.

  3. Third LaTeX run:

    1. Page: Contents, Chapter 1 -> page 5, <other entries>
    2. Page: <other entries>
    3. Page: <other entries>
    4. Page: <empty>
    5. Page: Chapter 1 ...
    

    Now, the table of contents is correct.

Rerun warnings

LaTeX does not warn, if another LaTeX run is needed for the table of contents and the other lists. There is a package rerunfilecheck that uses \pdfmdfivesum of pdfTeX (both DVI or PDF mode) to calculate a MD5 checksum over an auxiliary file at the beginning and the end of the job. If something has changed, then the package prints a rerun warning:

\documentclass{book}

\usepackage[starttoc]{rerunfilecheck}

\newcommand*{\cleardoubleemptypage}{%
  \clearpage
  \begingroup
    \pagestyle{empty}%
    \cleardoublepage
  \endgroup
}

\begin{document}
Title page
\cleardoubleemptypage
\tableofcontents
\cleardoubleemptypage
\listoffigures
\cleardoubleemptypage
\listoftables
\cleardoubleemptypage
\chapter{First chapter}
\begin{figure}
\caption{First figure}
\end{figure}
\addtocontents{toc}{\protect\clearpage}% short for many \chapter/\section/... entries
\chapter{Second chapter}
\addtocontents{toc}{\protect\clearpage}% short for many \chapter/\section/... entries
\chapter{Third chapter}
\end{document}

The first and second run generates the warnings:

Package rerunfilecheck Warning: File `test.toc' has changed. Rerun.
Package rerunfilecheck Warning: File `test.lof' has changed. Rerun.

The third run is successful without warnings.