# Automatic generation of pages with variable fields

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
}

\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.

-

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 http://tex.stackexchange.com/a/15338/963 for a bit of a longer explanation as to how to manipulate such lists (including alpha-numeric sorting).

-
Perfect. Thanks a lot! –  Filip Feb 11 '12 at 10:29

A solution that allows you to specify the list of students in a quite natural way and also the text of the exam between

\begin{exam}
<text>
\end{exam}


where \thestudent expands to the current student's name.

\documentclass{article}
\usepackage{xparse,environ}
\ExplSyntaxOn
\NewDocumentCommand{\studentlist}{ m }
{
\seq_set_split:Nnn \l_exam_student_list_seq { ; } { #1 }
}
\seq_new:N \l_exam_student_list_seq
\NewEnviron{exam}
{
\seq_map_inline:Nn \l_exam_student_list_seq
{
\cs_set:Npn \thestudent { ##1 }
\BODY\newpage
}
}
\ExplSyntaxOff

\begin{document}

\studentlist{
Copperfield, David;
Doe, John;
Liddell, Alice
}

\begin{exam}

This is the exam for \thestudent

\begin{enumerate}
\item Compute $1+1$
\item Tell if the formula $1+1=10$ is true or false
\end{enumerate}

\end{exam}

\end{document}


This will generate three copies of the text, each one with a different name.

-

You may also consider using the datatool package.

You first create a comma separated variable (.csv) file like this

Number,Name,Subject,Date
PH001,Rajesh,Physics,12.02.2012
PH002,Mahesh,Physics,12.02.2012
PH003,Ganesh,Physics,12.02.2012


Name this file as namelist-1.csv (say). Then your .tex file will be

\documentclass[12pt]{article}
\usepackage{microtype}
\usepackage{datatool}
%==================================================================
\begin{document}
%
\DTLforeach{names}{
\no=Number,  \name=Name,  \sub=Subject, \date=Date}{%
%
This is the exam for \textbf{\name \, (\no)} in \sub \, held on \date.
%
\begin{enumerate}
\item Compute $$1+1$$
\item Tell if the formula $1+1=10$ is true or false
\end{enumerate}
%
\pagebreak
}
%
%
\end{document}


This will generate 3 pages of output corresponding to the three names in the database file namelist-1.csv. Here you can mention their Register number, Subject and dates also without much effort.

-

A \foreach loop is provided by the pgffor package. An example follows.

\documentclass{article}
\usepackage{pgffor}
\begin{document}
\foreach \x in {John, Mary} {My name is \x.\par}
\end{document}


More complex constructions are allowed. For instance

\foreach \x / \y in {John/30, Mary/29} {\x, \y\ years old.\par}

-

The basic idea is to define a macro that takes as parameter the part that varies. Below is a simplistic example. I would need to know more to come up with something that is closer to what you really need. Macros in TeX and LaTeX are a little advanced and I would suggest to read about them in the TeXbook. You can have up to 9 parameters per macro which should give you enough flexibility. A typical application of this outside of exercise sheets would be serial letters. The macro definition is declared as \long such that it can span several paragraphs which otherwise would be illegal (to help TeX catch runaway macro expansions).

\documentclass{article}

\long\def\exercise#1{%
Lorem ipsum ...
#1
Lorem ipsum ..
\newpage
}

\begin{document}
\exercise{Jane}
\exercise{James}
\exercise{Jim}
\end{document}


An advantage over list-based solutions is that there are no special characters (like a comma) that separate list elements and that you would have to avoid in the names and such. On the downside, a little more typing is necessary but could be reduced by picking a shorter name for the macro.

-
Thank you, it is not complicated at all... –  Filip Feb 11 '12 at 10:30

Here is a similar implementation to Yiannis' answer, using the etoolbox package. It provides \docsvlist{<CSV list>} which execute \do{<item>} for every <item> in <CSV list>. A redefinition of \do allows you to modify the behaviour of the output:

\documentclass{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\begin{document}
\renewcommand*{\do}[1]{%
\textbf{#1}\par\lipsum[1]\pagebreak}
\docsvlist{Jon Mac, Yiannis Laz, Filip, Mary}
\end{document}

-