[Tex/LaTex] Define and iterate a list of items

lists

I'd like to have a dynamic list of items that I define at one point in my document and that I use in a table elsewhere by iterating through the items. To clarify what I mean, here you go with a minimal solution:

\documentclass{scrartcl}

\usepackage{ltxtable}
\usepackage{booktabs}

\usepackage{fltpoint}
\usepackage{numprint}

%\newitem{nameoflist,
%  date = {2011-11-03},
%  description = {Item A},
%  amount = {29}
%  price = {19}
%}
%\newitem{nameoflist,
%  date = {2011-11-27},
%  description = {Item B},
%  amount = {32}
%  price = {22}
%}

\begin{document}

\newcommand*{\initsubtotal}{\global\def\subtotal{0}}
\newcommand*{\addsubtotal}[1]{\fpAdd{\subtotal}{\subtotal}{#1}\global\let\subtotal\subtotal}
\newcommand*{\calclinetotal}[2]{\fpMul{\linetotal}{#1}{#2}}

\initsubtotal

\begin{longtable}{cclcrr}
    \textbf{Pos.} &
    \textbf{Date} &
    \textbf{Description} &
    \textbf{Amount} &
    \textbf{Price} &
    \textbf{Total} \\
    \midrule
  \endfirsthead
    \textbf{Pos.} &
    \textbf{Date} &
    \textbf{Description} &
    \textbf{Amount} &
    \textbf{Price} &
    \textbf{Total} \\
    \midrule
  \endhead
    \multicolumn{5}{r}{Sum} & \numprint{\subtotal} \\
  \endfoot
    \midrule
    \multicolumn{5}{r}{Sum} & \numprint{\subtotal} \\
  \endlastfoot

  %\foreachitem{nameoflist}{index}{
  %  \itemindex &
  %  \itemdate &
  %  \itemdescription &
  %  \itemamount &
  %  \numprint{\itemprice} &
  %  \calclinetotal{\itemamount}{\itemprice}
  %  \addsubtotal{\linetotal}
  %  \numprint{\linetotal} \\
  %}

\end{longtable}

\end{document}

I've commented out the definition and usage of the items because these points in code are only exemplary solutions of how I'd like to have it.

I already tried packages like arrayjob, forloop, forarray, and other similar packages but them packages and/or their usage turned out to be absolutely uncomfortable provided that it did work at all (a complete, working solution I couldn't figure out so far regardless of being comfortable or not).

As you can see, the definition of the items I've inspired by the common style of BibTeX entries. The usage is just a straightforward idea of how it would be absolutely comfortable (at least for the given purpose).

Best Answer

Based on the example of the link provided by Yiannis in his comment to my question, I worked out the following solution using the package lstdoc:

\documentclass{scrartcl}

\usepackage{ltxtable}
\usepackage{booktabs}

\usepackage{fltpoint}
\usepackage{numprint}

\usepackage{lstdoc}

\begin{document}

\makeatletter

\let\itemlist\@empty
\def\addtolist#1#2{%
  \lst@lAddTo#1{#2}%
}
\def\newitem#1|#2|#3|#4|#5;{%
  \addtolist{\itemlist}{#1,}%
  \expandafter\gdef\csname#1\endcsname{%
    #1 &
    #2 &
    #3 &
    #4 &
    #5 &
    \calclinetotal{#4}{#5}%
    \addsubtotal{\linetotal}%
    \numprint{\linetotal} \\
  }%
}

\newitem 1 | 2011-11-03 | Item A | 29 | 19;
\newitem 2 | 2011-11-27 | Item B | 32 | 22;

\newcommand*{\initsubtotal}{\global\def\subtotal{0}}
\newcommand*{\addsubtotal}[1]{\fpAdd{\subtotal}{\subtotal}{#1}\global\let\subtotal\subtotal}
\newcommand*{\calclinetotal}[2]{\fpMul{\linetotal}{#1}{#2}}

\initsubtotal

\begin{longtable}{cclcrr}
    \textbf{Pos.} &
    \textbf{Date} &
    \textbf{Description} &
    \textbf{Amount} &
    \textbf{Price} &
    \textbf{Total} \\
    \midrule
  \endfirsthead
    \textbf{Pos.} &
    \textbf{Date} &
    \textbf{Description} &
    \textbf{Amount} &
    \textbf{Price} &
    \textbf{Total} \\
    \midrule
  \endhead
    \multicolumn{5}{r}{Sum} & \numprint{\subtotal} \\
  \endfoot
    \midrule
    \multicolumn{5}{r}{Sum} & \numprint{\subtotal} \\
  \endlastfoot

  \@for\item:=\itemlist \do{\ifx\@empty\item\vspace{-14pt}\else\csname\item\endcsname\fi}

\end{longtable}

\end{document}

Now, I just have to figure out how to handle \subtotal so that it is displayed in the foot line(s) correctly. I already have an approach but didn't test it so far. However, that's another story...

Related Question