I am quite frustrated about the following: Iam writing a document with requirements which have requirement IDs as a beginning of a line (I'm using these in an enumerate environment). I want to collect the requirements in a DATATOOL database and output the requirements at the end of the document in a table.
Whatever I tried, the following macro works fine in the sense that it adds a requirement line with proper requirement ID into the enumerate environment, but it odes NOT add output the requirement ID in the column "reqKey" when I display the table at the end - I have the suspicion that something with the variable access to "\@reqID" does not work.
Any help very much appreciated.
\DTLnewdb{reqs}
\DTLnewdbcolumn{reqs}{reqKey}
\DTLnewdbcolumn{reqs}{reqText}
...
\makeatletter
\newcommand\req@nomen@count[1]{%
\@ifundefined{c@#1}
{% the counter doesn't exist
\newcounter{#1}\setcounter{#1}{1}%
}
{% the counter exists
\stepcounter{#1}%
}%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% markers for requirements
\newcommand{\req}[2][0]{%
\req@nomen@count{#2}%
\ifthenelse{\equal{#1}{0}}
{REQ.#2.\padzeroes[3]{\decimalnum{\value{#2}}}}
{REQ.#2.\padzeroes[3]{\decimalnum{\value{#2}}}.\padzeroes[2]{\decimalnum{#1}}}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\@reqID}{}
\newcommand{\itemReq}[3][0]{
\DTLnewrow{reqs}
\ifthenelse{\equal{#1}{0}}
{\renewcommand{\@reqID}{\req{#2}}}
{\renewcommand{\@reqID}{\req{#1}{#2}}}
\item[\textcolor{blue}{[\@reqID]}]#3%
\DTLnewdbentry{reqs}{reqKey}{\@reqID}
\DTLnewdbentry{reqs}{reqText}{#3}
}
\makeatother
...
\begin{document}
\begin{enumerate}
\itemReq{reqCategory}{Requirement text, can be lengthy}
\end{enumerate}
...
\DTLdisplaylongdb{reqs}
\end{document}
The output of this code looks like this:
Edit as requested I want to share an minimal adjusted (according to esdd's solution) example:
\documentclass{article}
\usepackage{enumitem}
\usepackage{fmtcount}
\usepackage{datatool}
\DTLnewdb{reqs}
\DTLaddcolumn{reqs}{reqKey}
\DTLaddcolumn{reqs}{reqText}
\makeatletter
% macro creating a new latex counter for every new entitiy name and requirement
\newcommand\req@nomen@count[1]{%
\@ifundefined{c@#1}
{% the counter doesn't exist
\newcounter{#1}\setcounter{#1}{1}%
}%
{% the counter exists
\stepcounter{#1}%
}%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% markers for requirements
\newcommand{\req}[2][0]{%
\ifthenelse{\equal{#1}{0}}
{REQ.#2.\padzeroes[3]{\decimalnum{\value{#2}}}}
{REQ.#2.\padzeroes[3]{\decimalnum{\value{#2}}}.\padzeroes[2]{\decimalnum{#1}}}%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\@reqID}{}
\newcommand{\itemReq}[3][0]{
\req@nomen@count{#2}%
\DTLnewrow{reqs}%
\ifthenelse{\equal{#1}{0}}
{\renewcommand{\@reqID}{\req{#2}}}
{\renewcommand{\@reqID}{\req[#1]{#2}}}%
\item[{[\@reqID]}]#3%
\dtlexpandnewvalue%
\DTLnewdbentry{reqs}{reqKey}{\@reqID}%
\DTLnewdbentry{reqs}{reqText}{#3}%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\begin{enumerate}
\itemReq{CategoryA}{Requirement category 1 text 1.}
\itemReq{CategoryA}{Requirement category 1 text 2.}
\itemReq{CategoryB}{Requirement category 1 text 1.}
\end{enumerate}
\DTLdisplaydb{reqs}
\end{document}
This code has another problem: the IDs in the table are always the ID with the highest counter in the text.