Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
\DTLloaddb[]{activities}{activities.csv}
\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.

share|improve this question
    
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

1 Answer 1

up vote 2 down vote accepted

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:

enter image description here

Notes:

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

References:

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}
\DTLloaddb{activities}{activities.csv}%  Read original DB
\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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.