Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
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

1 Answer 1

up vote 7 down vote accepted

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:

enter image description here

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
\DTLloaddb[noheader,keys={RowID,Label,Cost,Weight,PropA,PropB,PropC,Description}]{myDB}{foo.dat}

%\DTLdisplaydb{myDB}% Usefule for debugging.

\PrintDTLTable{myDB}{Hammer001,Hammer003,Longsword003}
\end{document}
share|improve this answer
    
datatool seems to be what I am looking for. I will probably turn the master table into an external file, which datatool supports, too. Bonus points for introducing the three-handed sword! –  TeXter Jun 24 '12 at 15:15
    
Nice function. Where does pgffor get used, and could one do without it? –  Faheem Mitha Jan 7 at 17:22
    
Looks like pgffor is not used. –  Faheem Mitha Jan 7 at 19:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.