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,

2

2 Answers 2

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

4
  • This is awesome, but the new functions returns an extra space -- any way to get rid of that?
    – mike
    Dec 9, 2012 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, 2012 at 1:20
  • Perfect -- really appreciate the response @percusse
    – mike
    Dec 9, 2012 at 1:28
  • @mike My pleaseure :)
    – percusse
    Dec 9, 2012 at 1:30
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

2
  • Thanks for the detailed response, but I am not actually making the table in Tex, so the answer below is preferred.
    – mike
    Dec 9, 2012 at 0: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...
    – user4686
    Dec 9, 2012 at 8:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .