Test whether a string is in a list with variable

Essentially a one liner with expl3:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\IfStringInList}{mmmm}
 {
  \clist_if_in:neTF {#2} {#1} {#3} {#4}
 }
\prg_generate_conditional_variant:Nnn \clist_if_in:nn {ne} {T,F,TF}
\ExplSyntaxOff

\begin{document}

\newcommand{\word}{Paul}
\newcommand{\butnot}{Joe}

\IfStringInList{\word}{George,John,Paul,Ringo}{Beat it}{Roll it}

\IfStringInList{\butnot}{George,John,Paul,Ringo}{Beat it}{Roll it}

\end{document}

With e we force full expansion of the first argument.

enter image description here

By the way, e expansion is a big achievement in TeX Live 2019, available with all TeX engines. Since you're talking of strings, I suppose that the material in the search string is fully expandable to characters.


Like this? You need to make sure that \word and/or \butnot get expanded first. This can also be achieved with some \expandafters but since I am never sure how many of those one needs I use a more mundane but IMHO also more accessible syntax. Of course there are many tools that help you doing the same (but at the very moment of writing this I am fighting a conflict between \tikzexternalize and xparse so I feel this solution might be useful for some who have similar clashes).

\documentclass{article}

\makeatletter
\newcommand*{\IfStringInList}[2]{%
  \in@{,#1,}{,#2,}%
  \ifin@
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
}
\makeatother
\newcommand{\IfStringInListExpandedDo}[4]{%
\edef\tmp{\noexpand\IfStringInList{#1}{#2}{\noexpand\edef\noexpand\tst{1}}{\noexpand\edef\noexpand\tst{0}}}%
\tmp%
\ifnum\tst=1\relax%
#3%
\else%
#4%
\fi}

\begin{document}
\newcommand{\word}{Paul}
\newcommand{\butnot}{Joe}
\IfStringInListExpandedDo{\word}{George,John,Paul,Ringo}{abc \textbf{Beat it}}{cde \textit{Roll it}}
\IfStringInListExpandedDo{\butnot}{George,John,Paul,Ringo}{abc\textbf{Beat it}}{cde \textit{Roll it}}
\end{document}

enter image description here