[OS X TeX] using variables in text via references or labels

Alan Munn amunn at msu.edu
Mon Mar 30 17:50:59 EDT 2009


At 11:35 PM +0200 3/30/09, Gert van Oss wrote:
>Thank you both! It took me some minutes to find out how to install 
>some missing parts of Latex: datatool, xfor.
>
>And then I got the example to work, this is exactly what works for 
>me! I didn't know how to look for this solution as I didn't know 
>something like this existed.
>
>I'm using very large tables in my documents but I'll just create a 
>summary-table with the specific numbers that I'll want to mention in 
>my text so efficiency is not a problem. It will anyhow always be 
>better then scanning my screen for a number I might have forgotten 
>to append.
>
>Thanks again,

You're welcome.  Just for completeness, there was a redundancy in the 
getrow command: it doesn't need to assign a command to the Index, and 
it certainly doesn't need to assign it twice as in my original 
example.  So a better command would be:

\newcommand{\getrow}[2]{%
\DTLforeach*[\DTLiseq{\myindex}{#2}]{#1}{%
\tableA=A,\tableB=B,\tableC=C,\tableD=D}}

  (Of course if having a command for the Index is useful, it could be assigned.)

Alan


>Gert
>
>On 30 mrt 2009, at 23:08, Alan Munn wrote:
>>
>>
>>---cut here---
>>
>>% Proof of concept for arbitrary reference to table cells using datatool.
>>% This assumes that your table's first column contains row numbers (used
>>% for indexing the rows).  If tables are very large, this could get quite
>>% inefficient, since the \getrow command rereads the database each time.
>>% For the current example, the database t1.csv consists of the following:
>>% Cut and past this and save as t1.csv (without the % and leading spaces).
>>% ---t1.csv---
>>% Index,A,B,C,D
>>% 1,10,20,40,80
>>% 2,8,16,32,64
>>% 3,1,2,4,8
>>%---t1.csv
>>
>>\documentclass{article}
>>\usepackage{datatool}
>>
>>% Define a command which takes a database name and a rownumber as arguments
>>% and returns the elements of that row as separate commands. This is to be
>>% used for reference to particular cells.
>>%
>>\newcommand{\getrow}[2]{%
>>\DTLforeach*[\DTLiseq{\myindex}{#2}]{#1}{%
>>\myindex=Index,\myindex=Index,\tableA=A,\tableB=B,\tableC=C,\tableD=D}}
>>
>>
>>\begin{document}
>>
>>\begin{table}
>>\DTLloaddb{t1}{t1.csv}
>>\centering
>>\begin{tabular}{llll}
>>\DTLforeach{t1}{%
>>\myindex=Index,\tableA=A,\tableB=B,\tableC=C,\tableD=D}{%
>>\\
>>\tableA & \tableB & \tableC & \tableD}
>>
>>\end{tabular}
>>\caption{Some numbers}
>>\end{table}
>>As we can see from the table the element in row 1 column 4 is 
>>\getrow{t1}{1}\tableD, is a small number.
>>\end{document}

-- 
Alan Munn						amunn at msu.edu
Department of Linguistics
and Germanic, Slavic, Asian and African Languages		Tel. 
	517-355-7491
Michigan State University, East Lansing MI 48824 USA	Fax	517-432-2736



More information about the MacOSX-TeX mailing list