# How can I create copies of a document with a running number?

I am making a questionnaire, to be filled out with pen and paper. I need 40 copies of it, with around 13 pages each. I expect that some participants might take the questionnaire apart, so I have to be able to match the pages from an exemplar to each other. I want to do it in a simple way, by printing a number from 1 to 40 in the footer of each copy.

However, I don't know how to generate different copies from the same TeX source, and I don't want to manually compile 40 times, changing the number each time. I decided to put everything between \begin{document} and \end{document} in a \foreach loop from pgffor, using a counter to print the number in the footer. This produces the 40 copies I need in a single document (not a problem from my point of view), but of course, this changes the page numbering, so the second exemplar starts at page 14 instead of page 1. As the page number is printed in the footer too, this is unacceptable.

Because I am still making some changes to the questionnaire, I might suddenly find myself with 12 or 14 pages. So, taking the page number modulo 13 for printing in the footer is a bad idea.

Any ideas how to make this work? Or will I have to fall back to the hardcoded modulo and just manually change the number if needed? I will prefer it to be really from inside LaTeX, not a script which changes the number in the .tex file and recompiles (I have to compile on different OSes). I don't care if I end up with one very long .pdf or forty small ones, as long as it isn't the same .pdf overwritten forty times.

\documentclass[a4paper,12pt]{article}

\usepackage{pgffor}
\usepackage{fancyhdr}

\newcounter{questionnaireNr}
\setcounter{questionnaireNr}{0}
\pagestyle{fancy}
\fancyfoot[C]{Questionnaire \arabic{questionnaireNr}}
\fancyfoot[R]{page \thepage}

\begin{document}
\foreach \x in {1,2,3,...40}
{
\stepcounter{questionnaireNr}

Lorem Ipsum %here are the questions
}
\end{document}

-

In the start of your \foreach, add

\newpage
\setcounter{page}{1}


This resets the page counter to 1 when the loop is run. (I am assuming you want each new copy to start on a new page.)

-

I have had the same problem, and came up with the following solution. The enclosed LaTeX input takes a given PDF file (= one copy of the questionnaire) and creates N copies, each numbered 000..N. The number is added in light grey to the lower right corner. Feel free to customize as you like.

% Print an exam N times, with serial numbers 000...N
% by Andreas Zeller, Saarland University

\documentclass[a4paper,10pt]{article}

\usepackage{pdfpages}
\usepackage{fancyhdr}
\usepackage{color}
\usepackage{multido}

% What is the starting number?
\def\startnumber{1}

% How many exams do we need?
\def\numberofexams{10}

% Which is the input PDF file?
\def\examfile{ping-exam-1-EN}

% This is to control the placement of the number.
% I am using A4 format, so feel free to adapt to your page size.
\textwidth  16cm
\textheight 23cm

% We use headers to output the serial number
\pagestyle{fancy}
\renewcommand{\footrulewidth}{0pt} % No footer line
\cfoot{}

% Actual command to include the serial number
\rfoot{\Huge\textcolor{gray!25}{\textsf{%
% Some number padding - for up to 999 exams
\ifnum\numberexam<100 0\fi%
\ifnum\numberexam<10 0\fi%
\numberexam}
}}

% And here comes the single loop across all documents
% pagecommand by itself adds nothing,
% but causes our new headers and footers to be printed
\begin{document}
\multido{\numberexam=\startnumber+1}{\numberofexams}{%
\includepdf[pages=-,pagecommand=\strut]{\examfile}
}
\end{document}


Enjoy! -- Andreas

-