# Generating lists and tables of items from commands in the document

I would like to define objects all over my document and later evaluate certain components from these objects.

Example:

\documentclass{minimal}
\newcommand{\requirement}[3]{#1, #2, #3}
\begin{document}

\requirement{Uwe}{2013-10-12}{Remove window}

\requirement{John}{2013-10-13}{Clean window}

\end{document}


How can I generate a table of all \requirements where each given argument of the requirement generates one column? How can I generate an list of all persons.

Background: I'd like to use LaTeX to write Business Requirements Documents, where I need to define objects such as stakeholders, business requirements and functional requirements that are connected to each other. I might be able to parse the document with Perl/Python etc. but I'd like to develop a TeX-only solution.

-
I'd suggest looking at Nicola Talbot's datatool package, which I think will help you with almost all the heavy lifting required to do this. –  Brent.Longborough Oct 12 '13 at 10:53
Do you (1) need to externalize the data (e.g., as CSV) or do you (2) just want to collect them for some LaTeX tables within the document. Are these tables (a) printed behind all data acquisition commands in the document or may they (b) also have to be printed beforehand? Inside the tables, shall the data be sorted (I) in the order of definition or (II) by some other sorting criteria (e.g., alphabetically)? –  Daniel Oct 12 '13 at 18:30
My intention at this stage is just to collect them in the document, it should be sufficient to print them after the aquisition. Following Brent's advice I managed to put something together that I'll post tomorrow as an update to my question. Looks pretty promising at the moment. Sorting is definitely a requirement, I guess I can easily add this to my code. –  Uwe Ziegenhagen Oct 12 '13 at 18:44

Based on Brent's hint here is what I have come up so far.

\documentclass{article}
\usepackage{datatool}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{booktabs}
\usepackage{longtable}

\DTLnewdb{stakeholders}

\newcommand{\stakeholder}[2]{%
\DTLnewrow{stakeholders}
\DTLnewdbentry{stakeholders}{ID}{#1}
\DTLnewdbentry{stakeholders}{Name}{#2}
\par Stakeholder #2 defined!
}

\newcommand{\breq}[5]{%
\par BR #3 defined
}

% http://tex.stackexchange.com/questions/40901/datatool-getting-a-specific-value-given-the-value-of-another-column
\newcommand*{\thestakeholder}{}
\newcommand*{\getstakeholder}[1]{%
\DTLgetvalueforkey{\thestakeholder}{Name}{stakeholders}{ID}{#1}%
\thestakeholder
}

\begin{document}

\section{Stakeholder Definition}

\stakeholder{dag}{Dagobert Duck}

\stakeholder{don}{Donald Duck}

\stakeholder{mic}{Mickey Mouse}

\breq{seca}{dag}{Security}{The system must be secured against internal attacks.}{high}

\breq{secb}{don}{Security}{The system must be secured against external attacks.}{medium}

\breq{secc}{mic}{Security}{The system must be secured against internal and external attacks.}{low}

\section{Query the databases}

The stakeholders DB contains \DTLrowcount{stakeholders} stakeholders.

\DTLdisplaydb{stakeholders}\vspace*{1cm}

\section{For-loop with query ID$\rightarrow$Name}

\begin{tabular}{cllp{8cm}l} \toprule
ID &  Initiator &  Name &  Description &  Priority \\ \midrule