# pgfplots axis descriptions from file

I was wondering if there is any way to make pgfplots read the xlabel and ylabel from a file. This is similar to this question Using Labels from a File in PGFplot but I'd like to read the column headings and use them as axis descriptions (not as labels for individual ticks).

To stick with the example from the other post, for a file foo.dat like this

# foo.dat
# X-Position Height
1          15
2          20
3          12
4          24

I would like to have an axis environment equivalent to

\begin{axis}[xlabel=X-Position, ylabel=Height]
\end{axis}

but without having to specify xlabel=X-Position, ylabel=Height explicitly.

The reason for this strange question is that I'm using a tex file in a script to generate plot images. Without axis descriptions I can generate a data file, copy both the data file and the prepared tex file into a temporary folder an compile everything. If what I asked is not possible, I would have to change the tex file. I can deal with this by generating the tex file on the fly in this case, but I was just wondering if there was a better solution.

-

Yes, that is possible using the macros of pgfplotstable (which is always shipped together with pgfplots).

pgfplotstable allows to read the table into some in-memory structure, and it supports \pgfplotstableforeachcolumn<\tablename>\as<\loopvariable>{<loop body>}. Inside of <loop body>, the macro <\loopvariable> expands to the column name and \pgfplotstablecol expands to the column index.

Thus, we can iterate over all columns and have access to name and index. We only need to combine this with a TeX expansion trick and a mapping from column indices to axis directions (0=>x, 1=>y) and have a running prototype:

\documentclass[a4paper]{article}

\usepackage{filecontents}

\begin{filecontents}{foo.dat}
# foo.dat
X-Position Height
1          15
2          20
3          12
4          24
\end{filecontents}

\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\usepackage{pgfplotstable}

\begin{document}

\begin{tikzpicture}
% this here allows to set labels by INDEX rather than 'x', 'y', or 'z':
\pgfplotsset{
label no 0/.style={xlabel={#1}},
label no 1/.style={ylabel={#1}},
label no 2/.style={zlabel={#1}},
}

% this iterates over every column name, where the current column name is stored in
% '\col':
% this here defines '\temp' to be
%   '\pgfplotsset{label no 0={X-Position}}'
%   '\pgfplotsset{label no 0={\col}}'
% purpose: '\col' must be expanded (\'e'def) immediately
% before it is overwritten in the next loop iteration.
\edef\temp{\noexpand\pgfplotsset{label no \pgfplotstablecol={\col}}}
% this here calls '\temp':
\temp
}%

% the axis always uses the currently set options - in our case
% 'xlabel' and 'ylabel':
\begin{axis}