Just when I think I starting to understand \expandafter I run into this problem where I am trying to test if \ConditionG, \ConditionH, etc are defined in a loop. I tried all the combinations I could think of and am not able to get this to work:
\documentclass{article}
\usepackage{pgffor}
\usepackage{etoolbox}
\begin{document}
%\def\ConditionG{}% This should show up as undefined
\def\ConditionH{}
\foreach \i in {G, H}{
\expandafter\ifdefined\csname Condition\i\endcsname% Incorrect Results: everything defined
\textbackslash Condition\i\ is defined\par
\else
\textbackslash Condition\i\ is not defined\par
\fi
}
\hrule
\foreach \i in {G, H}{% Using etoolbox
\ifdef{\expandafter\csname Condition\i\endcsname}{% Incorrect Results: everything defined
\textbackslash Condition\i\ is defined\par
}{
\textbackslash Condition\i\ is not defined\par
}
}
\end{document}
Joseph's detailed explanation regarding When to use \edef, \noexpand, and \expandafter? makes sense to me, but can't seem to apply that to my problem.
\expandafter\ifx\csname foo\endcsname\relax...\fito test a macro. In LaTeX2e, this is what\@ifundefineddo. – Leo Liu Jun 21 '11 at 05:55\@ifundefinedworks expandably but fills up the hash table, whereas using a group avoids the hash table issue at the cost of not being expandable. – Joseph Wright Jun 21 '11 at 07:19\newcount\i \def\test{\advance\i by 1{\csname foo\the\i\endcsname}\test}\testfills the hash table (and apparently, the control sequence is put in the hash table even when scanned within a\def(or any token list). – Bruno Le Floch Jun 23 '11 at 23:08