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:
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 onpgfkeys
and basic TeX expansion control.Here is the solution:
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.