# Extra pages generated by afterpage

I am creating a set of solutions to a test and need to have a header table that indicates the question number, year of test, etc.

I have created the code below that uses tikz to create the table and afterpage to add it to every page of solutions.

This works, but the pdf generation creates several additional pages at the end of the document. Can anyone see why? It appeared to do this whenever one solution ran over more than one page, but even the simplified example below generates two additional pages.

Any help would be appreciated. The example code is:

\documentclass[12pt,a4paper]{article}

\usepackage[top=6cm, bottom=1.5cm, left=1.5cm, right=1.5cm]{geometry}
\usepackage{fancyhdr}
\usepackage{tikz}
\usepackage{array}
\usepackage{booktabs}
\usepackage{afterpage}

\setlength\extrarowheight{5px}
\setlength{\aboverulesep}{0pt}
\setlength{\belowrulesep}{0pt}

\pagestyle{fancyplain}
\fancyhf{}
\cfoot{\thepage}

\newcounter{qno}
\setcounter{qno}{1}
\newcommand{\question}[1]{
\begin{itemize}\item[\arabic{qno}.]\hfill\textbf{[#1]}\end{itemize}}

\newcommand\module{XXX}
\newcommand\examyear{XXX}
\newcommand\setter{XXX}

\newcommand\examtable{%
\begin{tikzpicture}[remember picture,overlay]
\node [yshift=-1.2cm,below,align=center,font=\bfseries] at (current page.north)
{
TEST\\
\\
\hspace{14cm} for Coordinator\\
\begin{tabular} { | p{4.1cm} | >{\centering\arraybackslash}m{6.5cm} | p{0.5cm} | >{\centering\arraybackslash}m{2.9cm} | >{\centering\arraybackslash}m{2.1cm} |}
\cmidrule{1-2} \cmidrule{4-5}
Name 1 & {\centering\setter} &  & Paper & Question \\
\cmidrule{1-2} \cmidrule{4-5}
Name 2 & \module &  &  & \\
\cmidrule{1-2}
Year & \examyear &  & \module & \theqno \\
\cmidrule{1-2}
Initials &  &  &  & \\
\cmidrule{1-2} \cmidrule{4-5}
\end{tabular}\\
\vspace{2\baselineskip}
};
\end{tikzpicture}
\afterpage{\examtable}%
}

% End of preamble

\begin{document}

% Beginning of title page

\examtable

\question{25} % Question 1

XXX

\stepcounter{qno}
\newpage
\question{25} % Question 2

XXX

\end{document}

• I would never use \afterpage here, it is really coded as an infinite loop, each call to \examtable ends with a call to the same macro. I haven't traced exactly what happens but luckily somewhere in \end{document} the input stack gets dropped and so the loop terminates, but only getting two extra pages seems like a better outcome than expected. this appears to be a modified page heading, why not use fancyhdr or similar just to specify it as the page head? – David Carlisle Oct 23 '15 at 11:26
• Note also you are missing a lot of % from ends of lines in these definitions (although I don't think that is the cause of the extra pages here) – David Carlisle Oct 23 '15 at 11:29

As noted in comments the original markup was specifying that each page generated a further page, so only by chance did the document terminate at all.

The tables appear to be a page heading and fancyhdr package is already loaded, so this just specifies that the table is the page head. As such it needs to pick up the current equation number from the \mark system.

This just produces two pages,

\documentclass[12pt,a4paper]{article}

\usepackage[top=6cm, bottom=1.5cm, left=1.5cm, right=1.5cm]{geometry}
\usepackage{fancyhdr}
\usepackage{tikz}
\usepackage{array}
\usepackage{booktabs}
\usepackage{afterpage}

\setlength\extrarowheight{5px}
\setlength{\aboverulesep}{0pt}
\setlength{\belowrulesep}{0pt}

\pagestyle{fancyplain}
\fancyhf{}
\cfoot{\thepage}

\newcounter{qno}
\setcounter{qno}{1}
\newcommand{\question}[1]{%
\begin{itemize}\item[\arabic{qno}.]\markright{\theqno}\hfill\textbf{[#1]}\end{itemize}}

\newcommand\module{XXX}
\newcommand\examyear{XXX}
\newcommand\setter{XXX}

\newcommand\examtable{%
\begin{tikzpicture}[remember picture,overlay]
\node [yshift=-1.2cm,below,align=center,font=\bfseries] at (current page.north)
{%
TEST\\[\baselineskip]%
\hspace{14cm} for Coordinator\\
\begin{tabular} { | p{4.1cm} | >{\centering\arraybackslash}m{6.5cm} | p{0.5cm} | >{\centering\arraybackslash}m{2.9cm} | >{\centering\arraybackslash}m{2.1cm} |}
\cmidrule{1-2} \cmidrule{4-5}
Name 1 & {\centering\setter} &  & Paper & Question \\
\cmidrule{1-2} \cmidrule{4-5}
Name 2 & \module &  &  & \\
\cmidrule{1-2}
Year & \examyear &  & \module & \rightmark\\
\cmidrule{1-2}
Initials &  &  &  & \\
\cmidrule{1-2} \cmidrule{4-5}
\end{tabular}\\
\vspace{2\baselineskip}
};
\end{tikzpicture}%
}

% End of preamble

\begin{document}

% Beginning of title page

\question{25} % Question 1

XXX

\stepcounter{qno}
\newpage
\question{25} % Question 2

XXX

\end{document}

• Excellent. Thank you. It now does what I need it to. – David H. Oct 23 '15 at 11:58