10

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

Output of MWE

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

David Carlisle
  • 757,742
devendra
  • 2,818
  • 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 02 '12 at 21:20

2 Answers2

7

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.

David Carlisle
  • 757,742
3

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).

David Carlisle
  • 757,742