@nameuse as conditional
You're almost there. Two problems only.
First, \@nameuse
contains an @
in the name, so you need to \makeatletter
before using it.
Second, if you \show\@nameuse
(after a \makeatletter
, of course) you see:
> \@nameuse=macro:
#1->\csname #1\endcsname
so one expansion of \@nameuse{foo}
yields \csname blafasel\endcsname
. The \csname
...\endcsname
requires another expansion to make \blafasel
, which is what you want. So now you know you need two expansions of \@nameuse
.
With
\expandafter\ifx\@nameuse{blafasel}\foo
you expand it once. To expand twice you need one more \expandafter
before each token that precedes \@nameuse
. That is, one \expandafter
for the "old" \expandafter
, and another for the \ifx
:
\expandafter\expandafter\expandafter\ifx\@nameuse{blafasel}\foo
Now the code:
\documentclass{standalone}
\makeatletter
\parindent\z@
\@namedef{blafasel}{foo}
\def\foo{foo}
\def\bar{bar}
\makeatother
\begin{document}
1:
\makeatletter
\expandafter\expandafter\expandafter\ifx\@nameuse{blafasel}\foo
\message{true}
\else
\message{false}
\fi
2:
\expandafter\expandafter\expandafter\ifx\@nameuse{blafasel}\bar
\message{true}
\else
\message{false}
\fi
\end{document}
prints true false
somewhere in the terminal.
You need to expand two levels, so \expandafter\expandafter\expandafter
, besides making @
a letter in order to avoid TeX compare the first two tokens in the expansion of \@
(precisely, \spacefactor
and \@m
).
A single \expandafter
is sufficient with \expandafter\ifx\csname blafasel\endcsname\foo
.
There are simpler ways to compare macros expanding to strings of characters, the simplest one being
\ifnum\pdfstrcmp{\@nameuse{blafadel}}{foo}=0
true
\else
false
\fi
that doesn't need to define \foo
. Since \pdfstrcmp
is available with different names in other engines, it's best to do
\usepackage{pdftexcmds}
and use \pdf@strcmp
.
Much more powerful string comparison functions are available with expl3
.