After the first invocation, \stop
is called and the execution is stopped. Therefore no additional documents are generated. But how can we fix the code?
We still need the documentclass and a way to split the line into its fields:
\documentclass{article}
\def\chopline#1;#2;#3 \\{
\def\name{#1}
\def\vorname{#2}
\def\punkte{#3}
}
Now there are two cases:
Either we are directly called by the user. Then we never defined \conditionmacro
and we want to invoke a new LaTeX instance for every line.
Otherwise we were invoked in this manner and \conditionmacro
is already set to samoe line. To differentiate these cases, we test if \conditionmacro
is undefined:
\ifx\conditionmacro\undefined
In the first case, we now read the file: First open it, then read every line in a loop until the End Of File is reached:
\newread\data
\openin\data=sbriefdata.csv
\loop
\read\data to \line
\unless\ifeof\data
Now we are in the loop body and have to handle one line.
First figure out the name for the filename:
\expandafter\chopline\line\\
We want to pass this line to a new pdflatex
instance, so e.g. if we read A;B;10
the macro \conditionmacro
should be defined as A;B;10
in the inner file.
This can be archived by adding \gef\conditionmacro{A;B;10}
in front of the regular file content. So:
\immediate\write18{%
pdflatex --jobname="\jobname-\name"
"\gdef\string\conditionmacro{\line}\string\input{\jobname}"
}%
The whole "\gdef\string\conditionmacro{\line}\string\input{\jobname}
" will be expanded in the outer TeX instance, resulting in our case in \gdef\conditionmacro{A;B;10}\input{sbrief}
if our original file is called sbrief.tex
. Here the \string
was just used to stop \conditionmacro
and \input
from being expanded.
How this does exactly what we want: \conditionmacro
is defined and then the original file content is read.
Now our loop is finished, we can close the file end end the outer TeX run.
\repeat
\closein\data
\expandafter\stop
\fi
In our first case, we stopped at the \stop
above, so if we reach this point we are in the second case: \conditionmacro
is set and we have to create one of the actual documents.
First split \conditionmacro
into it's parts:
\expandafter\chopline\conditionmacro\\
then "just" create the document:
\begin{document}
Hallo \vorname\space\name,
Sie haben \punkte\space Punkte erzielt.
\end{document}
The full code:
\documentclass{article}
\def\chopline#1;#2;#3 \\{
\def\name{#1}
\def\vorname{#2}
\def\punkte{#3}
}
\ifx\conditionmacro\undefined
\newread\data
\openin\data=sbriefdata.csv
\loop
\read\data to \line
\unless\ifeof\data
\expandafter\chopline\line\\
\immediate\write18{%
pdflatex --jobname="\jobname-\name"
"\gdef\string\conditionmacro{\line}\string\input{\jobname}"
}%
\repeat
\closein\data
\expandafter\stop
\fi
\expandafter\chopline\conditionmacro\\
\begin{document}
Hallo \vorname\space\name,
Sie haben \punkte\space Punkte erzielt.
\end{document}
If you additionally want to collect all the subdocuments in a collected PDF, you can include the PDF files after compiling into the outer document:
\documentclass{article}
\def\chopline#1;#2;#3 \\{
\def\name{#1}
\def\vorname{#2}
\def\punkte{#3}
}
\ifx\conditionmacro\undefined
\usepackage{pdfpages}
\begin{document}
\newread\data
\openin\data=sbriefdata.csv
\loop
\read\data to \line
\unless\ifeof\data
\expandafter\chopline\line\\
\immediate\write18{%
pdflatex --jobname="\jobname-\name"
"\gdef\string\conditionmacro{\line}\string\input{\jobname}"
}%
\includepdf[pages=-]{\jobname-\name.pdf}
\repeat
\closein\data
\csname fi\endcsname
\end{document}
\fi
\expandafter\chopline\conditionmacro\\
\begin{document}
Hallo \vorname\space\name,
du hast \punkte\space Punkte erzielt.
\end{document}