# Reference table value in text

I want to be able to reference a table value in my text (this is because I often update my tables, and then list the specific values in the text). Here is an example table I would use:

% Example Table
\documentclass{minimal}
\begin{filecontents*}{scientists.csv}
name,surname,age
Albert,Einstein,133
Marie,Curie,145
Thomas,Edison,165
\end{filecontents*}

\documentclass{article}
\usepackage{pgfplotstable}
\begin{document}
\pgfplotstabletypeset[
col sep=comma,
string type,
columns/name/.style={column name=Name, column type={|l}},
columns/surname/.style={column name=Surname, column type={|l}},
columns/age/.style={column name=Age, column type={|c|}},
every last row/.style={after row=\hline},
]{scientists.csv}
\end{document}


I may want to be able to reference a given scientists age in the text by a reference of his/her name (ie,:)

Albert Einstein is \ref{albert} years old.


Ideally, this would still be using pgfplotstable because it is how I currently read in many tables. Thanks,

-
\firstname is not defined. Do you intend it to be \surename? – hpesoj626 Dec 8 '12 at 7:20
Thanks -- had copied in wrong example from here:texblog.org/tag/pgfplotstable – mike Dec 8 '12 at 7:31

Another alternative without modifying the data is via \pgfplotstablegetelem...\pgfplotsretval pair. Note that the row index starts from 0 instead of 1.

\documentclass{article}
\usepackage{pgfplotstable}
\begin{filecontents*}{scientists.csv}
name,surname,age
Albert,Einstein,133
Marie,Curie,145
Thomas,Edison,165
\end{filecontents*}

\def\getcell#1#2#3{
\pgfplotstablegetelem{#1}{#2}\of{#3}\pgfplotsretval%
}
\begin{document}
\pgfplotstabletypeset[
string type,
columns/name/.style={column name=Name, column type={|l}},
columns/surname/.style={column name=Surname, column type={|l}},
columns/age/.style={column name=Age, column type={|c|}},
every last row/.style={after row=\hline},
]\mytable

\bigskip

\getcell{0}{name}{\mytable} \getcell{0}{surname}{\mytable} is \getcell{0}{age}{\mytable}
years old. \getcell{1}{name}{\mytable} \getcell{1}{surname}{\mytable} is
\getcell{1}{age}{\mytable} years old. But \getcell{2}{name}{\mytable}
\getcell{2}{surname}{\mytable} is still older, he is \getcell{2}{age}{\mytable} years old.

\end{document}


-
This is awesome, but the new functions returns an extra space -- any way to get rid of that? – mike Dec 9 '12 at 1:11
@mike Yes sorry I'm missing a percent sign in the definition of getcell, replace that line with \def\getcell#1#2#3{% – percusse Dec 9 '12 at 1:20
Perfect -- really appreciate the response @percusse – mike Dec 9 '12 at 1:28
@mike My pleaseure :) – percusse Dec 9 '12 at 1:30

If it is an option to have your csv files suitably prepared, try this (here I bundled the filecontents thing and the table in one latex source)

\documentclass{article}

\makeatletter
\def\printandsetlabel#1#2#3{#2\setcounter{#1}{#2}%
\protected@edef\@currentlabel
{\csname p@#1\endcsname\csname the#1\endcsname}%
\label{#3}}
\makeatother

\newcounter{age}
\newcommand*{\age}[2]{\printandsetlabel{age}{#1}{#2}}

\begin{filecontents*}{scientists.csv}
name,surname,age
Albert,Einstein,\age{133}{albert}
Marie,Curie,\age{145}{marie}
Thomas,Edison,\age{165}{thomas}
\end{filecontents*}

\usepackage{pgfplotstable}
\pgfplotsset{compat=1.7}

\begin{document}\thispagestyle{empty}\hsize8cm
\pgfplotstabletypeset[
col sep=comma,
string type,
columns/name/.style={column name=Name, column type={|l}},
columns/surname/.style={column name=Surname, column type={|l}},
columns/age/.style={column name=Age, column type={|c|}},

@mike: thanks for looking at my proposal. Preparing the table (I think you mean the .csv file) does not need TeX, but of course if you need to use the same table with some other software, then you will not want to have the added \age things... – jfbu Dec 9 '12 at 8:53