Identification of an empty macro from pgfplots using \ifx

Consider following MWE and the output thereafter. I have defined an empty macro and used \ifx to identify it as such. However, when an empty macro is received from \pgfkeysgetvalue, \ifx doesn't work.

\documentclass{article}
\usepackage{pgfplots, filecontents}
\pgfplotsset{compat=1.6}
\begin{filecontents}{test.dat}
Time Distance
0 0
1 1
\end{filecontents}

\pgfplotstableset{
columns/Distance/.style={
column name={$D_{\alpha}$},
}
}
\begin{document}
%
\def\tmp{}
\ifx\tmp\empty
\noindent Tmp is \tmp.\\
\fi
%
\pgfplotstableset{columns/\col/.try}%
\pgfkeysgetvalue{/pgfplots/table/column name}\temp%
\ifx\temp\empty%
\noindent Temp is empty.\\%
\else%
\noindent Macro col is \col. Macro temp is \temp.\\%
\fi%
}%
\end{document}


I am not sure whether macro \temp returned by \pgfkeysgetvalue is \empty. But it doesn't print anything. I have looked at these related posts:

How to check if a macro value is empty or will not create text with plain TeX conditionals?

Why doesn't \ifx work inside a node command in TikZ?

What is the difference between \pgfutil@empty and \empty in an \ifx environment

-
I've taken the liberty to reenter the links such that they are slightly more informative and also parsed automatically. Please revert back if you don't feel that it's proper. – percusse Nov 2 '12 at 21:20
This is better. Thank you. – devendra Nov 2 '12 at 21:34

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.

-
Wonderful! Did the trick. – devendra Nov 2 '12 at 21:32
It only does for the MWE... but that does not load pgfplotstable - in which case /pgfplots/table/column name is undefined (=\relax). If you load pgfplotstable, it has a different value. – Christian Feuersänger Nov 3 '12 at 11:14
@ChristianFeuersänger thanks for the clarification, I know nothing I just scattered some \show in the MWE:-) – David Carlisle Nov 3 '12 at 11:46
@DavidCarlisle sure - I would have done the some. Thanks for the answer! – Christian Feuersänger Nov 3 '12 at 11:57

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.

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
%
\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
}%


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.