5

Take the following code as an example:

\documentclass{standalone}
\def\myvar{foobar}

\begin{document}
\makeatletter
\in@{foo}{foobar}\ifin@ Yes\else No\fi,
\in@{foo}{\myvar}\ifin@ Yes\else No\fi
\makeatother
\end{document}

This typesets Yes, No while I would expect Yes, Yes. I'm guessing this is because \myvar is not being expanded before \in@ does its work. How do I make it work?

Note: I'm not very good at expansion, so it may be that the above (or the title) doesn't make much sense.

Keelan
  • 5,425

2 Answers2

5

Several ways, one way to do it with two \expandafter is

\documentclass{standalone}
\def\myvar{foobar}

\begin{document}
\makeatletter
\in@{foo}{foobar}\ifin@ Yes\else No\fi,
\def\zz{\in@{foo}}
\expandafter\zz\expandafter{\myvar}\ifin@ Yes\else No\fi
\makeatother
\end{document}
David Carlisle
  • 757,742
5

\in@ doesn't expand its arguments; you can easily define a new macro \ein@ that expands (once) its argument:

\documentclass{article}
\def\myvar{foobar}

\makeatletter
\def\ein@#1#2{%
  \expandafter\ein@@\expandafter{#2}{#1}%
}
\def\ein@@#1#2{\in@{#2}{#1}}
\makeatother

\begin{document}

\makeatletter
\in@{foo}{foobar}\ifin@ Yes\else No\fi,
\ein@{foo}{\myvar}\ifin@ Yes\else No\fi
\makeatother

\end{document}

You can do it more easily with xparse:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\IfInTF}{smmmm}
 {
  \IfBooleanTF{#1}
    {
     \tl_if_in:onTF { #3 } { #2 }  { #4 } { #5 }
    }
    {
     \tl_if_in:nnTF { #3 } { #2 }  { #4 } { #5 }
    }
 }
\ExplSyntaxOff

\newcommand\myvar{foobar}


\begin{document}

\IfInTF{foo}{foobar}{Yes}{No},
\IfInTF*{foo}{\myvar}{Yes}{No}

\end{document}
egreg
  • 1,121,712