I am trying to get a table of contents based on the page number (in the footer on the right side) but in some strange way it is counting from the wrong start. It says page 3 instead of 2 and page 4 instead of 3. I have played with the positions of the code but nothing seems to help.

This is my code:

\documentclass[11pt, A4]{article}
\usepackage[a4paper,top=2cm,hmargin=2cm,bottom=2.9cm,]{geometry}
\usepackage{tikz}
\usepackage{tikzpagenodes}
\usetikzlibrary{calc}
\usepackage{fancyhdr}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{colortbl}
\usepackage[utf8]{inputenc}
\usepackage{titletoc}
\usepackage[T1]{fontenc}
\usepackage{sectsty}
\usepackage{float}
\usepackage{atbegshi}
\makeatletter
\renewcommand{\@seccntformat}[1]{}
\makeatother
\renewcommand\numberline[1]{}
\renewcommand{\contentsname}{Inhoudsopgave}
\chapterfont{\color[RGB]{227,24,24}}
\sectionfont{\color[RGB]{227,24,24}}
\subsectionfont{\color[RGB]{227,24,24}}
\definecolor{bcsred}{RGB}{227,24,24}
\newcommand\Footer{%
\begin{tikzpicture}[remember picture, overlay]
\node[yshift=1.75cm] at (17, 24.5) {};
\draw [line width=0.55mm] (-0.5,-23.5) -- (16.5,-23.5);
\node[yshift=1.75cm, text=red] at (1.5, -26) {Test};
\node[yshift=1.75cm, text=gray] at (15, -26) {2019-03-21} ;
\node[fill=black, text=white, yshift=1.75cm] at (17, -25.5)             {\thepage};
\fill[bcsred]
(current page.south west) -- (current page.south east) --
([yshift=-30pt]current page.south east|-current page text area.south east) --
([yshift=-30pt]current page.south west|-current page text area.south west) -- cycle;
\end{tikzpicture}%
}
\pagestyle{fancy}
\cfoot{}
\AtBeginShipout{\Footer}
\AtBeginShipoutFirst{}
\begin{document}
\begin{titlepage}
\begin{center}
\line(1,0){300}\\
[0.25in]
\huge\bfseries Test \\
[2mm]
\line(1,0){300}\\
[1.5cm]
\fontfamily{Arial}\selectfont
\textsc\LARGE Test\\
[1cm]
\textsc\Large Test \\
[12cm]
\end{center}
\end{titlepage}
\setcounter{page}{2}
%\newpage
\tableofcontents
\newpage
\section{Test}
\newpage
\end{document}

• You should use a4paper as class option instead of A4.
– user156344
Mar 25 '19 at 13:49
• Welkom bij TeX.SE! There is a \setcounter{page}{2} in your code (just before \tableofcontents), that might be the problem? Try setting it to 1 or just remove the command. Mar 25 '19 at 14:03
• @Marijn I have tried that before, i have tried it to set it to 1 and 0 but nothing seems to work. I also placed it on different places in my code, before tableofcontent and after. On the top of my code and after /document but nothing worked so far. I dont understand why it takes the wrong page number it so confusing. If i change it to 1 then the table of contents is correct but then the page numbering in the footer is wrong and says number 1 on two pages before it goes to 2. Mar 25 '19 at 14:05

The code in the question uses the page number during the \AtBeginShipout call. This is tricky because the page counter is modified by different macros and hooks at various times, which makes it difficult to always get the correct value during page shipout.

As an alternative you can use a custom counter that is displayed and increased in the \Footer macro, which is responsible for the page number in the box, independent from the actual page counter. The 'real' page counter is still used for the numbers in the Table of Contents.

Partial code:

\newcounter{mypagenumber}
\setcounter{mypagenumber}{1}
\newcommand\Footer{%
\begin{tikzpicture}[remember picture, overlay]
\node[yshift=1.75cm] at (17, 24.5) {};
\draw [line width=0.55mm] (-0.5,-23.5) -- (16.5,-23.5);
\node[yshift=1.75cm, text=red] at (1.5, -26) {Test};
\node[yshift=1.75cm, text=gray] at (15, -26) {2019-03-21} ;
\node[fill=black, text=white, yshift=1.75cm] at (17, -25.5) {\themypagenumber};
\fill[bcsred]
(current page.south west) -- (current page.south east) --
([yshift=-30pt]current page.south east|-current page text area.south east) --
([yshift=-30pt]current page.south west|-current page text area.south west) -- cycle;
\end{tikzpicture}%
\stepcounter{mypagenumber}
}


Result:

The next pages also show the correct number, consistent with the Table of Contents (i.e., 2, 3, etc., not shown in the screenshot).

Note that there is a different problem with the footer macro that adds an empty page with only a header and footer at the end, but that is most likely unrelated to the page counter issue.

• Thanks for your reply, i will check it now Mar 26 '19 at 12:23
• I get the following error: Runaway argument? {\newcounter {mypagenumber} \setcounter {mypagenumber}{1} \newcommand \ETC. ! File ended while scanning use of \@argdef. <inserted text> \par <*> rapportage2.tex ? ! Emergency stop. <inserted text> \par <*> rapportage2.tex Mar 26 '19 at 12:29
• Let's debug in chat, chat.stackexchange.com/rooms/91547/… Mar 26 '19 at 12:33
• i can not talk to you, my reputation is too low :( need 20 Mar 26 '19 at 12:35
• Thank you it works! found the solution Mar 26 '19 at 12:39