# datatool repeat row

I intend to create a document where parts of the text are read from the entries of a .csv file using the package datatool (version 2012/09/25 v2.11., updated manually under debian). Everything works fine as I want:

activities.csv:

"Role","Activity","NbActivity"
"Role 1","Activity A",1
"Role 1","Activity B",2
"Role 2","Activity C",3


read by the following .tex file:

\documentclass{minimal}
\usepackage[english]{babel}
\usepackage{datatool}
\begin{document}
\DTLforeach{activities}
{\role=Role, \activity=Activity, \NbA=NbActivity}{
\role
\activity \\
}
\end{document}


For the purpose of the document that I would like to create, rows of the database should be copied by the number indicated in the "NbActivity" column, resulting in something like that,...

"Role","Activity","NbActivity"
"Role 1","Activity A",1
"Role 1","Activity B",2
"Role 1","Activity B",2
"Role 2","Activity C",3
"Role 2","Activity C",3
"Role 2","Activity C",3


... in order to repeat those entries in the text as follows:

Role 1 Activity A

Role 1 Activity B

Role 1 Activity B

Role 2 Activity C

Role 2 Activity C

Role 2 Activity C

I tried to solve this by using \DTLnewdbentry and loops as suggested in Repeat command n times?, but just can't figure out how to do this. I am new to LaTeX programming/ macros, and would really appreciate any help, thank you.

-
Welcome to TeX.SE. Do you actually want the data duplicated in the DB, or do you want to just typeset the data that many times? –  Peter Grill Oct 2 '12 at 9:27
@PeterGrill Thank you very much for your really quick answer! This is exactly what I needed: duplicates in the database so that the \DTLforeach produces the right outcome. Thank you! –  Ernst Oct 2 '12 at 11:45

If you want to duplicate the entries in the DB, I think the only way to do it is to user a new DB and create entries in the new DB as you step thru the original one. Below is the activities DB (your original DB as read form the file), and the activitiesExpanded DB which contains the duplicated entries you desire:

## Notes:

• I used \DTLforeach* instead as we are in read only mode for the DB thru which we are stepping (and is faster).

## Code:

\documentclass{article}

\usepackage{datatool}
\usepackage{pgffor}

%\usepackage{filecontents}% <-- commented out so file does not get overwritten
\begin{filecontents*}{activities.csv}
"Role","Activity","NbActivity"
"Role 1", "Activity A", 1
"Role 1", "Activity B", 2
"Role 2", "Activity C", 3
\end{filecontents*}

\begin{document}
\DTLnewdb{activitiesExpanded}%           Initialize new DB

\noindent\textbf{activities DB:}
\DTLforeach*{activities}{%
\role=Role, \activity=Activity, \NbA=NbActivity% % <-- This percent is important
}{%
\par\noindent\role\ \activity
\dtlexpandnewvalue
\foreach \x in {1,...,\NbA} {% Duplicate entries as desired
\DTLnewrow{activitiesExpanded}
\DTLnewdbentry{activitiesExpanded}{Role}{\role}
\DTLnewdbentry{activitiesExpanded}{Activity}{\activity}
\DTLnewdbentry{activitiesExpanded}{NbActivity}{\NbA}
}%
}

\bigskip
\noindent\textbf{ActivitiesExpanded DB:}
\DTLforeach*{activitiesExpanded}{%
\role=Role, \activity=Activity, \NbA=NbActivity% <-- This percent is important
}{%
\par\noindent\role\ \activity
}

\end{document}

-