TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 5 down vote accepted

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}
share|improve this answer

\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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.