[Tex/LaTex] pgfplotstable: How to access column name

legendpgfkeyspgfplotspgfplotstable

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}
  \pgfplotstableread{test.dat}\loadedtable

  \pgfplotstabletypeset{\loadedtable}

  \pgfplotstableforeachcolumn\loadedtable\as\col{%
     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}{
  \pgfplotstablegetcolumnnamebyindex{\y}\of{\loadedtable}\to{\colname}
  \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}
\pgfplotstableread{test1.dat}\tableone \pgfplotstableread{test2.dat}\tabletwo

\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:
enter image description here

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.

Best Answer

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}
  \pgfplotstableread{test.dat}\loadedtable

  \pgfplotstabletypeset{\loadedtable}

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

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

enter image description here

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

Please refer to http://pgfplots.sourceforge.net/TeX-programming-notes.pdf for details about the basic instructions.

Related Question