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

Alan Munn amunn at msu.edu
Mon Mar 30 17:08:53 EDT 2009


At 8:31 PM +0200 3/30/09, Gert van Oss wrote:
>dear all,
>
>I've had some problem making up a clear subject line and stil am in 
>doubt whether I've succeded. What I'm trying to achieve is the 
>following.
>
>My documents have often various tables and charts embedded via the
>
>    \input{table_file}
>
>command.
>
>While explaining my tables/charts I'm mentioning actual numbers (as 
>listed in the tables) in my text. Is it somehow possible to create a 
>list of numbers in my latex document and have them called via a 
>reference in my text? This would enable me to run a somewhat related 
>scenario in my numbers and udpate both my tables with numbers and 
>have all numbers changed in the text as well.
>
>I was thinking this should be possible while using \labels, \ref but 
>for the moment can't stretch my mind to find a solution.

As Ross mentioned, there's no good way to refer to the elements of a 
table in LaTeX format. For tabular data from external sources I use 
datatool.sty and keep the data in Excel or OpenOffice csv files.  The 
following is a small proof of concept that would allow you to make 
reference to individual cells in a table stored in a csv file and 
created using datatool.

For figures, I don't see a solution, but if the figures have an 
associated table, the same solution could be used (even if the table 
for the figure isn't displayed in the document.)

Alan

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