# Wrong page with \addcontentsline{toc}{section} after a list of figures

Consider the code presented at the end of this message, with the figure downloadable here (it is just a pdf of a rectangle). The problem is that the \addcontentsline{toc}{section} links to the wrong page, after all figures. How to make it link to the exact page? (without changing the current alignment of figures with 2 figures vertically centered per page and no text). The result is here and the problem is illustrated below:

% DOCUMENT TYPE
\documentclass[a4paper]{book}
\usepackage[english]{babel}
\usepackage[margin=10pt,font=footnotesize,labelfont=bf]{caption}
\usepackage{lipsum}
\usepackage{fancyhdr}
\usepackage{graphicx}
\usepackage{pgffor}
\usepackage{xifthen}
\usepackage[top=1in, bottom=1in, left=1in, right=1in]{geometry}
\usepackage{hyperref}

\fancyhf{}
\renewcommand{\footrulewidth}{1pt}
\fancyfoot[CE,CO]{\thepage}

% INVISIBLE SECTION

% DOCUMENT INFORMATION
\title{Title}
\author{Author}
\date{\today}

% DOCUMENT
\begin{document}
\maketitle
\tableofcontents
\mainmatter
\chapter{First chapter}
\section{First section}
\lipsum
\lipsum
\lipsum
\lipsum
\section{Second section}
\lipsum
\lipsum
\lipsum
\lipsum
\chapter{Second chapter}
\foreach\n in {1,3,...,17}{%
\ifthenelse{\cnttest{\n}{=}{1}}{\invisiblesection{First section}}{}
\ifthenelse{\cnttest{\n}{=}{5}}{\invisiblesection{Second section}}{}
\ifthenelse{\cnttest{\n}{=}{9}}{\invisiblesection{Third section}}{}
\ifthenelse{\cnttest{\n}{=}{13}}{\invisiblesection{Fourth section}}{}
\begingroup\fboxsep=0pt
\begin{figure}[!p]
\makebox[\textwidth][c]{\fbox{\includegraphics[page=1, width=\textwidth]{pdfpage.pdf}}}
\end{figure}
\begin{figure}[!p]
\makebox[\textwidth][c]{\fbox{\includegraphics[page=1, width=\textwidth]{pdfpage.pdf}}}
\end{figure}
\endgroup
\clearpage
}
\chapter{Third chapter}
\section{First section}
\lipsum
\lipsum
\lipsum
\lipsum
\section{Second section}
\lipsum
\lipsum
\lipsum
\lipsum
\end{document}

• The contentsline stuff isn't connected to any printed stuff on the page. I think you want to print some kind of weird appendix. I think you don't want the figures to float, so leave out the figure-environment. – Johannes_B May 11 '15 at 7:17
• What's the purpose of those \invisiblesections? – user31729 May 11 '15 at 12:29

Since floating is discouraged anyway ...

% DOCUMENT TYPE
\documentclass[a4paper]{book}
\usepackage[english]{babel}
\usepackage[margin=10pt,font=footnotesize,labelfont=bf]{caption}
\usepackage{lipsum}
\usepackage{fancyhdr}
\usepackage{graphicx}
\usepackage{pgffor}
\usepackage{afterpage}
\usepackage{xifthen}
\usepackage[top=1in, bottom=1in, left=1in, right=1in]{geometry}
\usepackage[pdfpagelabels]{hyperref}

\fancyhf{}
\renewcommand{\footrulewidth}{1pt}
\fancyfoot[CE,CO]{\thepage}

% INVISIBLE SECTION

% DOCUMENT INFORMATION
\title{Title}
\author{Author}
\date{\today}

% DOCUMENT
\begin{document}
\maketitle
\tableofcontents
\mainmatter
\chapter{First chapter}
\section{First section}
\lipsum
\lipsum
\lipsum
\lipsum
\section{Second section}
\lipsum
\lipsum
\lipsum
\lipsum
\chapter{Second chapter}
\foreach\n in {1,3,...,17}{%
\ifthenelse{\cnttest{\n}{=}{1}}{\invisiblesection{First section}}{}
\ifthenelse{\cnttest{\n}{=}{5}}{\invisiblesection{Second section}}{}
\ifthenelse{\cnttest{\n}{=}{9}}{\invisiblesection{Third section}}{}
\ifthenelse{\cnttest{\n}{=}{13}}{\invisiblesection{Fourth section}}{}
\begingroup\fboxsep=0pt
\setlength{\parindent}{0pt}
\fbox{\includegraphics[page=1, width=\textwidth]{example-image.pdf}}
\fbox{\includegraphics[page=1, width=\textwidth]{example-image.pdf}}
\endgroup
\clearpage
}
\chapter{Third chapter}
\section{First section}
\lipsum
\lipsum
\lipsum
\lipsum
\section{Second section}
\lipsum
\lipsum
\lipsum
\lipsum
\end{document}

• Except that the figures are not centered/aligned in the same way now... – Vincent May 11 '15 at 7:37
• Add a clearpage after \invisiblechapter? To be honest, the example is huge and there are multiple things that i don't understand the reasoning for. figures, but floating disabled, the makeboxes (ok, i get this, but in the example we are talking about 2 times .4 pt; not really an issue) and section headings that do not exist and are likely to confuse the reader to the max. Maybe you can elaborate a bit in the question. – Johannes_B May 11 '15 at 7:43