Identification of an empty macro from pgfplots using \ifx
If you modify your code to look like
\pgfkeysgetvalue{/pgfplots/table/column name}\temp%
\show\temp
\ifx\temp\empty%
\noindent Temp is empty.\\%
You will see \temp
is \relax
in the no value case, so test for
\ifx\temp\relax
Don't forget to take the \show
out of the production code.
Your MWE is incomplete: it does not load pgfplotstable
. However, column name
is only available within pgfplotstable
.
What beats me is that you did not get an error although you used an undefined key. Ah - it is the /.try
handler. Apparently, it swallows the nested error message. I was unaware of that behavior ...
Anyway, I checked what happens if you load pgfplotstable
(as you did for sure in your larger production code). And: column name
is not empty initially. It only expands to an empty value (more precisely: to \pgfkeysnovalue
). Apparently, this has some special support which is (alas) not documented:
- if you say column name={}
, the column's display name is the empty string.
- if you say column name=\pgfkeysnovalue
, the column's display name defaults to the column's name. This is the default.
I will adjust the documentation.
Here is a suitable solution:
\documentclass{article}
\usepackage{pgfplots, filecontents}
\usepackage{pgfplotstable}% ---------- CF
\pgfplotsset{compat=1.6}
\begin{filecontents}{test.dat}
Time Distance
0 0
1 1
\end{filecontents}
\pgfplotstableset{
columns/Distance/.style={
column name={$D_{\alpha}$},
}
}
% --------- CF
\def\emptycolname{\pgfkeysnovalue}
\begin{document}
%
\def\tmp{}
\ifx\tmp\empty
\noindent Tmp is \tmp.\\
\fi
%
\pgfplotstableread{test.dat}{\loadedtable}%
\pgfplotstableforeachcolumn\loadedtable\as\col{%
\begingroup% ---- CF
\pgfplotstableset{columns/\col/.try}%
\pgfkeysgetvalue{/pgfplots/table/column name}\temp%
\ifx\temp\emptycolname% ----- CF
\noindent Temp is empty.\\%
\else%
\noindent Macro col is \col. Macro temp is \temp.\\%
\fi%
\endgroup% ---- CF
}%
\pgfplotstabletypeset\loadedtable
\end{document}
I introduced four changes: first, I loaded pgfplotstable
. Second, I defined \emptycolname
to contain the value that pgfplotstable
assumes if you never assigned a column name: \pgfkeysnovalue
. Third, a used that value in the \ifx
comparison. Last (but not least), I introduced \begingroup ... \endgroup
inside of your loop. Otherwise, you will inherit the column name
of the previous loop iteration. I also added \pgfplotstabletypeset
to see that it works.
See http://pgfplots.sourceforge.net/TeX-programming-notes.pdf for how to transport local variables outside of a TeX group (if you need it).