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. Jun 24, 2012 at 8:16
  • I've added two example rows to the initial post.
    – TeXter
    Jun 24, 2012 at 8:27
  • How do you want to hand pick the items? By row number? by first column entry? Jun 24, 2012 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, 2012 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)? Jun 24, 2012 at 8:33

1 Answer 1


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:


which yields the following table:

enter image description here


  • 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.



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 \\

    % #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
        \Label & \Cost & \Weight & \PropA & \PropB & \PropC & \Description

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

%\DTLdisplaydb{myDB}% Usefule for debugging.

  • 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, 2012 at 15:15
  • Nice function. Where does pgffor get used, and could one do without it? Jan 7, 2014 at 17:22
  • Looks like pgffor is not used. Jan 7, 2014 at 19:45
  • Hi Peter. I just ran into your note 1, which I didn't notice at the time I copied this code some years ago. I'd consider this is a bug, or at least an unreasonable restriction. However, there's an easy way around it. Namely use: \DTLforeach[\DTLisSubString{#2}{\RowID,}]{#1}{%. I.e. match \RowID, rather than just \RowID. Let me know if that seems correct to you, or if you are missing something. If you think it is correct, consider changing your answer. Jan 17, 2016 at 22:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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