Solution:
Replace the last \addtocontents
with the following code:
\makeatletter
\immediate\write\@auxout{\noexpand\@writefile{toc}{\noexpand\thispagestyle{headings}}}
\makeatother
If this is required in more than one document, you can define a macro for it.
In this case I would also set \protect
properly (as the normal \addtocontents
does). This is done inside a group of course (note the extra { }
pair).
\makeatletter
\newcommand\immaddtocontents[1]{{%
\let\protect\@unexpandable@protect
\immediate\write\@auxout{\noexpand\@writefile{toc}{#1}}%
}}
\makeatother
which can be used like this:
\immaddtocontents{\protect\thispagestyle{headings}}
Explanation:
The problem is that \addtocontents
is intended to be used together with material which is typeset. The underlying \write
macro is a so called whatsit, i.e. it is only stored when found and executed later when its box is typeset, i.e. when the material is (virtually) written on the page. This is done, because only at this moment the final position of the material i.e. the page number is known.
Your last \addtocontents
is never typeset, so comes never into the file.
Using the code above will write the contents line directly without creating a whatsit. Please note that if you are using this code in the middle of the document your entries could be out of order.
Alternatives:
My first idea was to add an empty box \hbox{}
after the last \addtocontents
to force that the whatsit is executed. However because the last text was in an \include
which executes \clearpage
, adding this box afterwards would create a new empty page at the end of the document. However, if the last content of the document comes from the main file or an \input
file (and does not end in an manual \clearpage
) this solution should work.
Other possibility would be to use the filehook
package to place the \addtocontents
direct before the \clearpage
of the last \include
file:
\usepackage{filehook}
\AtEndOfIncludeFile{<last file>}{\addtocontents{toc}{\protect\thispagestyle{headings}}}
or place the following directly before the last \include
:
\AtEndOfIncludes{\addtocontents{toc}{\protect\thispagestyle{headings}}}
Further Reading:
See The TeXBook it learn all abouts whatsits. For example in Chapter 21: Making Boxes, p.226.
Here's a possible solution.
- I used the
titletoc
and tocloft
packages to customize the ToC, LoF and LoT titles and entries as required. Firther customization might be required for the titles, but this might depend on the general layout for the chapter titles and this information was not provided in the question.
- The
tocbibind
packge was used to automatically include the LoF and the LoT in the table of contents.
The afterpage
package can be used to produce the corresponding headings in every page of the ToC, the LoF and the LoT; since these lists typically span few pages, one can use
\addtocontents{toc}{\protect\afterpage{tocheading}}
\addtocontents{lof}{\protect\afterpage{lofheading}}
\addtocontents{lot}{\protect\afterpage{lotheading}}
respectively, in the appropriate locations in the body of the document.
- I used the
book
document class (instead of using report
) to profit from the \frontmatter
, \mainmatter
(and \backmatter
) commands.
The code:
\documentclass{book}
\usepackage{titletoc}
\usepackage{tocloft}
\usepackage[nottoc]{tocbibind}
\usepackage{afterpage}
% Change the name of the ToC
\AtBeginDocument{%
\renewcommand\contentsname{Table of Contents}}
% Headings for every page of ToC, LoF and Lot
\newcommand\tocheading{\par\bigskip\MakeUppercase{\chaptername}\hfill Page\par}
\newcommand\lofheading{\par\bigskip\figurename\hfill Page\par}
\newcommand\lotheading{\par\bigskip\MakeUppercase{\tablename}\hfill Page\par}
% Centering titles for the ToC, Lof and Lot
\renewcommand{\cfttoctitlefont}{\hfill\normalfont\MakeUppercase}
\renewcommand{\cftaftertoctitle}{\hfill}
\renewcommand{\cftloftitlefont}{\hfill\normalfont\MakeUppercase}
\renewcommand{\cftafterloftitle}{\hfill}
\renewcommand{\cftlottitlefont}{\hfill\normalfont\MakeUppercase}
\renewcommand{\cftafterlottitle}{\hfill}
% Chapter entries formatting for frontmatter chapters
\newcommand\frontmatterchaptoc{%
\titlecontents{chapter}
[1.5em]{\addvspace{\baselineskip}}
{\contentslabel{1.5em}\MakeUppercase}
{\hspace*{-1.5em}\MakeUppercase}
{\titlerule*[1pc]{.}\contentspage}
}
% Chapter entries formatting for mainmatter chapters
\newcommand\mainmatterchaptoc{%
\titlecontents{chapter}
[5em]{\addvspace{\baselineskip}}
{\contentslabel{3em}\hspace*{-1em}\MakeUppercase}
{\MakeUppercase}
{\titlerule*[1pc]{.}\contentspage}
}
% Section, subsection, table and figure entries formatting
\titlecontents{section}
[7em]{}{\hspace{-1em}}{}{\titlerule*[1pc]{.}\contentspage}
\titlecontents{subsection}
[7.5em]{}{\hspace{-1em}}{}{\titlerule*[1pc]{.}\contentspage}
\titlecontents{figure}
[5em]{}
{\contentslabel{3em}\hspace*{-1em}}{}
{\titlerule*[1pc]{.}\contentspage}
\titlecontents{table}
[5em]{}
{\contentslabel{3em}\hspace*{-1em}}{}
{\titlerule*[1pc]{.}\contentspage}
\begin{document}
\frontmatter
\frontmatterchaptoc % activation of chapter entries formatting in the frontmatter
\chapter{Acknowledgements}
\cleardoublepage
\tableofcontents
\cleardoublepage
\addtocontents{lof}{\lofheading}% add heading to the first page in LoF
\listoffigures
\cleardoublepage
\addtocontents{lot}{\lotheading}% add heading to the first page in LoT
\listoftables
\cleardoublepage
\mainmatter
\addtocontents{toc}{\tocheading}% add heading to the first page in ToC, after frontmatter entries
\mainmatterchaptoc% activation of chapter entries formatting in the mainmatter
\chapter{Introduction}
\section{Section One One}
\begin{figure}
\centering
A
\caption{Test figure one}
\label{fig:test}
\end{figure}
\chapter{Policy divergence and traditional research}
\section{Section Two One}
\begin{figure}
\centering
A
\caption{William Adolphe Bouguereau, \emph{Youth of Bacchus} (initial sketch), 1884}
\label{fig:wab}
\end{figure}
\subsection{Section Two One One}
\end{document}
An image of the obtained ToC:
an Image of the obtained LoF:
Best Answer
You're placing
\phantomsection
and\addcontentsline
in the wrong place: they should be immediately after going to the new page, otherwise, if the list of figures occupies more than one page, the page reference in the table of contents would be wrong. Also, the anchor will be at the start of the list, rather than at the end.The easiest method for doing what you want is to load
etoolbox
:I chose
\clearpage
because it's “better programming”, but there should be no real difference; I used alsoso that it works independently of loading
hyperref
.