The following defines a fully expandable macro that checks whether the key is in the list. It uses expkv
to loop over the list of key=value pairs and pdftexcmds
to make \pdf@strcmp
available in all engines. The key names are not expanded in any way in this process. The macro needs exactly two steps of expansion to do its job.
\documentclass{article}
\usepackage{expkv}
\usepackage{pdftexcmds}
\makeatletter
\newcommand\ifkeyinlistTF[1]
{\romannumeral\expandafter\ifkeyinlistTF@\detokenize{#1}\relax}
\long\def\ifkeyinlistTF@#1\relax#2%
{%
\ekvparse{\ifkeyinlistTF@k{#1}}{\ifkeyinlistTF@v{#1}}{#2}%
\ifkeyinlistTF@false
}
\newcommand\ifkeyinlistTF@k[2]
{\if0\pdf@strcmp{#1}{\unexpanded{#2}}\ifkeyinlistTF@true\fi}
\newcommand\ifkeyinlistTF@v[3]
{\if0\pdf@strcmp{#1}{\unexpanded{#2}}\ifkeyinlistTF@true\fi}
\newcommand\ifkeyinlistTF@false[2]{\z@#2}
\long\def\ifkeyinlistTF@true\fi#1\ifkeyinlistTF@false#2#3{\fi\z@#2}
\makeatother
\begin{document}
\edef\foo
{%
\ifkeyinlistTF{key}{a,b=c,d,e=f,g,h=i}{.}{a}%
\ifkeyinlistTF{key}{a,b=c,d,e=f,g,h=i,key}{i}{.}%
\ifkeyinlistTF{key}{a,b=c,d,e=f,g,h=i,keya}{.}{q}%
\ifkeyinlistTF{key}{a,b=c,d,e=f,g,h=i,key=a}{u}{.}%
\ifkeyinlistTF{key}{a,b=c,d,e=f,g,h=i,key=}{i}{.}%
\ifkeyinlistTF{key}{a,b=c,d,e=f,g,h=i, key=}{t}{.}%
\ifkeyinlistTF{\key}{a,b=c,d,e=f,g,h=i, \key =}{a}{.}%
}
\texttt{\meaning\foo}
\end{document}
Output:
