This question is based on Peter Grill's answer to "Table including rows of a master table"..
The function \PrintDTLTable takes an optional list of rowIDs
(first row of the data file) as its first argument, and makes a
table containing only those rows. The rowIDs are also used as
labels.
BibTeX is usually used so that it only lists those papers that are cited in the bibligraphy. I don't know how this mechanism works, but I was wondering if can be adapted to this situation. I.e., if I reference some rows by label in the text, then is there some way to automatically generate a table containing only the rows corresponding to the labels appearing in the text?
\documentclass{article}
\usepackage{array}
\usepackage{multirow}
\usepackage{datatool}
\usepackage{longtable}
\usepackage{etoolbox}
\usepackage{filecontents}
\newcommand{\colhead}[1]{\multicolumn{1}{>{\bfseries}l}{#1}}
\newcounter{tabenum}\setcounter{tabenum}{0}
\newcommand{\nextnuml}[1]{\refstepcounter{tabenum}\thetabenum.\label{#1}}
\begin{filecontents*}{foo.dat}
Hammer001, Hammer, 1 , 0 , 1 , 10 , 1 , \multirow{2}{2in}{light (add some words here to wrap around)}\\
Hammer002, Hammer, 2 , 0 , 1 , 10 , 1 , heavy
Hammer003, Hammer, 3 , 0 , 1 , 10 , 1 , really heavy
Longsword001,Longsword, 1 , -1 , 2 , 75 , 2 , one-handed
Longsword002,Longsword, 2 , -1 , 2 , 75 , 2 , two-handed
Longsword003,Longsword, 3 , -1 , 2 , 75 , 2 , three-handed
\end{filecontents*}
\newcommand{\PrintDTLTable}[2][]{%
% #1 = list of rowIDs
% #2 = database to search
\begin{longtable}{l l l l l l l l}
% & \colhead{Date} & \colhead{From} & \colhead{To} & \colhead{Email} & \colhead{Subject}\\\hlin
& \colhead{Label} & \colhead{Cost} & \colhead{Weight} & \colhead{PropA} & \colhead{PropB} & \colhead{PropC} & \colhead{Description}\\\hline
\DTLforeach
[\ifblank{#1}{\boolean{true}}{\DTLisSubString{#1}{\RowID}}]
{#2}{%
\RowID=RowID,%
\Label=Label,%
\Cost=Cost,%
\Weight=Weight,%
\PropA=PropA,%
\PropB=PropB,%
\PropC=PropC,%
\Description=Description%
}{%
\nextnuml{\RowID} & \Label &\Cost & \Weight & \PropA & \PropB & \PropC & \Description \\
}%
\end{longtable}
}%
\begin{document}
% \DTLsetseparator{&}% Define separator of the data
\DTLloaddb[noheader,keys={RowID,Label,Cost,Weight,PropA,PropB,PropC,Description}]{myDB}{foo.dat}
% \DTLdisplaydb{myDB}% Useful for debugging.
\PrintDTLTable[Hammer001,Hammer003,Longsword003]{myDB}
\PrintDTLTable{myDB}
This is a reference to ~\ref{Hammer003}.
\end{document}
The tables produced by the code should be as follows: In the case of
\PrintDTLTable[Hammer001,Hammer003,Longsword003]{myDB}
the table by default consists of the three rows
Hammer001,Hammer003,Longsword003
Since the only reference is to Hammer003, the table produced should only contain the Hammer003 row.
Similarly, in the case of \PrintDTLTable{myDB}, the table by default consists of all 6 rows. Again, since the only reference is to Hammer003, the table produced should only contain the Hammer003 row.

