# Define and iterate a list of items

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*{\calclinetotal}[2]{\fpMul{\linetotal}{#1}{#2}}

\initsubtotal

\begin{longtable}{cclcrr}
\textbf{Pos.} &
\textbf{Date} &
\textbf{Description} &
\textbf{Amount} &
\textbf{Price} &
\textbf{Total} \\
\midrule
\textbf{Pos.} &
\textbf{Date} &
\textbf{Description} &
\textbf{Amount} &
\textbf{Price} &
\textbf{Total} \\
\midrule
\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}
%  \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).

-
–  Yiannis Lazarides Nov 30 '11 at 9:47
@Yiannis Thanks a lot. I don't know how I could fail to see the package lstdoc. Ok, it seems to be not documented very well on the Internet. However, the usage is relatively obvious and I was able to quickly adapt the example of your link to fit my requirements. If you write an appropriate answer, I will accept it as the correct answer. –  Flinsch Nov 30 '11 at 10:24
@Flinch Thanks. The lstdoc package is undocumented as it is the class used by listings for its documentation. I am a bit busy today to put a proper answer. Would appreciated it if you answer it on your own. (You can also accept your own answer). –  Yiannis Lazarides Nov 30 '11 at 11:21

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\newitem#1|#2|#3|#4|#5;{%
\expandafter\gdef\csname#1\endcsname{%
#1 &
#2 &
#3 &
#4 &
#5 &
\calclinetotal{#4}{#5}%
\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*{\calclinetotal}[2]{\fpMul{\linetotal}{#1}{#2}}

\initsubtotal

\begin{longtable}{cclcrr}
\textbf{Pos.} &
\textbf{Date} &
\textbf{Description} &
\textbf{Amount} &
\textbf{Price} &
\textbf{Total} \\
\midrule
\textbf{Pos.} &
\textbf{Date} &
\textbf{Description} &
\textbf{Amount} &
\textbf{Price} &
\textbf{Total} \\
\midrule
\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...

-
Unfortunately, longtable defines the footers as boxes, which are unboxed at the appropriate times, so there's no macro expansion at those times: the value of \subtotal at the footer's definition will be used throughout. :( –  egreg Nov 30 '11 at 13:07
@Flinsch: You can avoid creating commands by using a rather more complex loop than \@for. For a long/multipage table, the number of controls will run away. –  Ahmed Musa Nov 30 '11 at 17:22
@AhmedMusa Thanks for your hint. Do you have a concrete example? –  Flinsch Dec 1 '11 at 8:05