9

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

% Read in Table


  \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 head row/.style={before row=\hline,after row=\hline},
    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,

mike
  • 505

2 Answers2

6

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

\pgfplotstableread[col sep=comma]{scientists.csv}\mytable
\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 head row/.style={before row=\hline,after row=\hline},
    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}

enter image description here

percusse
  • 157,807
3

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

% Read in Table

\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|}},
    every head row/.style={before row=\hline,after row=\hline},
    every last row/.style={after row=\hline},
    ]{scientists.csv}

\bigskip
Albert Einstein is \ref{albert} years old and Marie Curie is \ref{marie} years
old. But Thomas Edison is still older, he is \ref{thomas} years old.
\end{document}

outcome of the code

  • Thanks for the detailed response, but I am not actually making the table in Tex, so the answer below is preferred. – mike Dec 09 '12 at 00:23
  • @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... –  Dec 09 '12 at 08:53