# Table including rows of a master table

I have a big master-table* containing reference items and that is a simple cross-project database of such items. Now I'd like to include in different documents parts of this master table and select arbitrary rows depending on the particular document's foucs.

To give an example: The master table contains items one can buy in a game. A typical row contains an item's label, cost, weight, some properties and a description column. Now I have different XeLaTeX documents containing game rules that focus on certain time periods (medieval, renaissance, modern day, sci-fi, ...). Some items of the master table are in use in some time periods (e.g. a sword, or a cell phone). Here a short example of two such rows:

Hammer & 1 & 0 & 1 & 10 & 1 & \\
Longsword & 2 & -1 & 2 & 75 & 2 & two-handed \\


I would like to hand-pick the items from the master table to be shown in the individual table (and not filter by some field or flag), but like to reference the base record only, so that if I make changes on the master table and e.g. change the weight or price, that automatically is also done in all derived tables.

Is there a best practice how to do that?

-
Can you provide an actual example of what the data looks like. This seems like a job for the datatool package. –  Peter Grill Jun 24 '12 at 8:16
I've added two example rows to the initial post. –  TeXter Jun 24 '12 at 8:27
How do you want to hand pick the items? By row number? by first column entry? –  Peter Grill Jun 24 '12 at 8:29
I am open to suggestions here. One could be that each master-row gets an ID of some kind as long as this is hidden in the final tables. In above case I would also be fine with writing \hammer \longsword as if each row would be an individual macro. –  TeXter Jun 24 '12 at 8:31
I assume then that the first column is not unique, so then a ID would be required. So you basically want to print the entire row for a given ID (but not include the ID)? –  Peter Grill Jun 24 '12 at 8:33

Here is an example of using the datatool package to extract the data form the following table defined below. The list of row ids to print is specified in the call to:

\PrintDTLTable{myDB}{Hammer001,Hammer003,Longsword003}


which yields the following table:

## Note:

• The first column (the RowID) must be unique, and can not be subsets of each other. So, if you have a RowID=foo and another RowID=foobar, a request to locate RowID=foo will also matchRowID=foobar.
• I changed the separator to be a comma, but if you want to keep it as the &, you should just need to uncomment the \DTLsetseparator line.

## Code:

\documentclass{article}
\usepackage{datatool}

\usepackage{filecontents}
\begin{filecontents*}{foo.dat}
Hammer001,   Hammer,    1 ,  0 , 1 , 10 , 1 , light\    Hammer002,   Hammer,    2 ,  0 , 1 , 10 , 1 , heavy\    Hammer003,   Hammer,    3 ,  0 , 1 , 10 , 1 , realy 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 = database to search
% #2 = list of rowIDs
\begin{tabular}{c c c c c c p{3.0cm}}
Label & Cost & Weight & PropA & PropB & PropC & Description\\\hline
\DTLforeach[\DTLisSubString{#2}{\RowID}]{#1}{%
\RowID=RowID,%
\Label=Label,%
\Cost=Cost,%
\Weight=Weight,%
\PropA=PropA,%
\PropB=PropB,%
\PropC=PropC,%
\Description=Description%
}{%
\Label & \Cost & \Weight & \PropA & \PropB & \PropC & \Description
}%
\end{tabular}
}%

\begin{document}
%\DTLsetseparator{&}% Define separator of the data

Nice function. Where does pgffor get used, and could one do without it? –  Faheem Mitha Jan 7 '14 at 17:22
Looks like pgffor is not used. –  Faheem Mitha Jan 7 '14 at 19:45