[Tex/LaTex] Automatic generation of pages with variable fields

automationexam

I need to produce dozens of same pages, except for the part that each page has to have a different person's name on it. Imagine it as an exam, where the content is equal for every page, except that I want to print the student's name in the header of somewhere else on the page.

From my programming perspective, one clue would be to make a list/series (or array in programming terms) of names in the beginning of a document, and invoke it further in the text as, say, \student[i], where i is an integer (i.e. number of current page). But I have no idea how to implement it as this is my first touch of this topic.

My current document looks like this:

\begin{document}
\newcounter{i} 
\setcounter{i}{1} 
\whiledo{\value{i} < 50}{ 

Lorem ipsum ...
% The place where I need to put a variable string
Lorem ipsum ...

\newpage
\addtocounter{i}{1} 
}

\end{document}

This produces 49 equal pages, however, I don't know how to create a series of names in a variable, and then print on every page a different name following the series.

Best Answer

You can store the names in a comma delimited list and then iterate over it, using the LaTeX kernel command \@for.

So the names are stored as:

\def\names{Jon Mac, Yiannis Laz, Filip, Mary}

And iteration is achieved as shown in the minimal below.

\documentclass{article}
\usepackage{lipsum}
\begin{document}
\makeatletter
\def\names{Jon Mac, Yiannis Laz, Filip, Mary}
\@for\next:=\names\do{%
  \textbf{\next}\par\lipsum[1]\pagebreak
}
\makeatother
\end{document}

Have a look at https://tex.stackexchange.com/a/15338/963 for a bit of a longer explanation as to how to manipulate such lists (including alpha-numeric sorting).