pgfplotstable: How to access column name?

I have used \pgfplotstableset to assign column names in the preamble. However, I don't know how to access them.

\documentclass{article}
\usepackage{amsmath}
\usepackage{pgfplots, pgfplotstable, filecontents}
\pgfplotsset{compat=1.6}

\begin{filecontents}{test.dat}
Time Distance
0 0
1 1
3 2
\end{filecontents}

\pgfplotstableset{
columns/Time/.style={
column name={$t_{\alpha}$},
},
columns/Distance/.style={
column name={$D_{\alpha}$},
},
}

\begin{document}

column name is ‘\col’; index is \pgfplotstablecol;\par
}
\end{document}


The column names are modified correctly in the table but when I try to access them using \col I don't get the modified names. What is the key for column names?

As pgfplots does not support mathmode expressions in the column names, I am planning to use \pgfplotstableset in the preamble to create a document wide list of column names and then use it for legends in the figures throughout the document.

Edit: I also tried

\foreach \y in {0,1}{
\colname
}


to get the column names. But with the same result. Also, \pgfplotstablegetcolumnnamebyindex is not mentioned in the manual for pgfplotstable v1.6. I got it from another post here.

Edit2: Further exploration of the issue. Consider the following code and the output of the latex file.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz,pgfplots,pgfplotstable,filecontents}
\pgfplotsset{compat=1.6}
\begin{filecontents}{test1.dat}
Time Distance
1 1
\end{filecontents}

\begin{filecontents}{test2.dat}
Time Velocity
2 3
\end{filecontents}

\begin{document}

\noindent \pgfplotstabletypeset{\tableone}\\
\pgfplotstabletypeset{\tabletwo}\newline\line(1,0){80}

\pgfplotstableset{
columns/Time/.style={
column name={$t_{\alpha}$},
}
}

\noindent \pgfplotstabletypeset{\tableone}\\
\pgfplotstabletypeset{\tabletwo}\newline\line(1,0){80}

\pgfkeyssetvalue{/pgfplots/table/column name}{$t_{\beta}$}

\noindent \pgfplotstabletypeset{\tableone}\\
\pgfplotstabletypeset{\tabletwo}\newline\line(1,0){80}

\pgfkeysvalueof{/pgfplots/table/column name}
\end{document}


Output is:

So here I show that \pgfplotstableset is indeed useful. Once an association has been made between a column header (as read from the input file) and the column name that one wants displayed it will be useful across multiple tables. As pointed out by @percusse and as mentioned in the \pgfplotstable manual, this function cannot be used as the scope of the definition is limited to \pgfplotstabletypeset.

I hope to use \pgfkeys instead. After going through pgfplotstable.code.tex to see the implementation of column name key, I tried to (unsuccessfully) use \pgfkeys. I am new to the \pgfkeys and hence I was not able to figure out how to access individual column names.

Waiting for new ideas.

-
Welcome to TeX.sx! A tip: you don't have to sign with your name since it automatically appears in the lower right corner of your post. –  Corentin Oct 26 '12 at 9:24
It does support mathmode expressions : use this row {$t_i$} {$D_i$} for your column names in the test.dat file and remove the \pgfplotstableset settings it will work. –  percusse Oct 26 '12 at 9:48
@percusse If {$t_i$} is changed to {$t_{\alpha}$} then it gives the error ! Missing \endcsname inserted. Do add \usepackage{amsmath} in the preamble. I have modified the question accordingly. –  devendra Oct 26 '12 at 11:25
I think this is not easy since, from the manual, column name sets the column name in the current context. That context is not available for the \foreach loop. It is set for the the typesetting but not for the low level reading of the table. –  percusse Oct 27 '12 at 11:19
Not the answer to your question, but I'd suggest to create another table with the column names in the first column (unuseful) and their aliases in the second and to access them via the \pgfplotstablegetelem command. –  Luigi Oct 27 '12 at 15:59

As @percusse stated correctly, the root challenge here is that these columns/<name>/.style things are only interpreted in the context of \pgfplotstabletypeset.

A solution which is also a direct answer to your question and solves the MWE means to query the information from the correct context. This is simpler than it sounds and I can give such an example below. It does not need ANY knowledge of internals of pgfplotstable. It only relies on pgfkeys and basic TeX expansion control.

Here is the solution:

\documentclass{article}
\usepackage{amsmath}
\usepackage{pgfplots, pgfplotstable, filecontents}
\pgfplotsset{compat=1.6}

\begin{filecontents}{test.dat}
Time Distance
0 0
1 1
3 2
\end{filecontents}

\pgfplotstableset{
columns/Time/.style={
column name={$t_{\alpha}$},
},
columns/Distance/.style={
column name={$D_{\alpha}$},
},
}

% defines \devendraretval to contain the display name for the column
% named '#1'
%
% #1: a column name
\def\getcolumndisplayname#1{%
% we WANT a group. Otherwise, any values set in these style would
% become global and they would be mixed up between different
% columns.
\begingroup
% Access the contents of column's display name programmatrically:
\pgfplotstableset{columns/#1/.try}%
\pgfkeysgetvalue{/pgfplots/table/column name}\temp
\ifx\temp\empty
% oh. We have no such value! Ok, use the column name as
% such.
\edef\temp{#1}%
\fi
% this here is one of the possible patterns to communicate a
% local variable outside of a local group:
\global\let\GLOBALTEMP=\temp
\endgroup
\let\devendraretval=\GLOBALTEMP
%
}%

\begin{document}
\thispagestyle{empty}

%
\getcolumndisplayname{\col}%
column name is ‘\col’; index is \pgfplotstablecol; display name is \devendraretval'\par
}

\getcolumndisplayname{Time}%
Value of Time is \devendraretval.
\end{document}


It defines a new command \getcolumndisplayname{}' which retrieves it.

-
Thanks. This is exactly what I was looking for. –  devendra Oct 28 '12 at 3:56

Alternative from my comment (and from Use first row of a table as legend entry in pgfplot graph?):

\documentclass[border=1pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.6}

time distance velocity something
0 0 1 0.2
1 1 1 0.3
1.999 1.999 1 0.4
2 2 0 0.4
3 2 0 0.5
}\mytable

name alias
time $t$
distance $d_\alpha$
velocity $\sin(2t)$
something nothing
}\tablenames

\pgfplotstablegetcolsof{\mytable}
\pgfmathtruncatemacro\numberofcols{\pgfplotsretval-1}

\begin{document}
\begin{tikzpicture}
\begin{axis}
\pgfplotsinvokeforeach{1,...,\numberofcols}{

This is interesting. But doesn't solve the problem. \tablenames table will have to be created for every figure. An easier option is just to add legend entries manually instead. However, if some sort of search functionality is available whereby a specific element from the alias column can be picked for a given element from the column name then this is useful. Then \tablenames table can be created once for the whole document and can be reused throughout the document. I am sure that this must be possible. –  devendra Oct 27 '12 at 17:31