6

I am comparing two strings, one of which is the expansion of a command. Neither of the three attempts below yields the correct result:

\documentclass{scrartcl}

\usepackage{etoolbox}
\begin{document}

    \newcommand{\sometext}{abc}
    \expandafter\ifstrequal{\sometext}{abc}{True!}{False!}

\end{document}

This yiels ''False!'', so \expandafter doesn't seem to do anything here. The way I see \expandafter work generally is without curly braces, so I tried these two:

\documentclass{scrartcl}

\usepackage{etoolbox}
\begin{document}

    \newcommand{\sometext}{abc}
    \expandafter\ifstrequal\sometext{abc}{True!}{False!}

\end{document}

and

\documentclass{scrartcl}

\usepackage{etoolbox}
\begin{document}

    \newcommand{\sometext}{abc}
    \expandafter\ifstrequal\sometext{}{abc}{True!}{False!}

\end{document}

Both of these yield abcTrue!False!

Is there a way to do what I want?

1
  • \expandafter\ifstrequal\expandafter{\sometext}{abc}{True!}{False!} ALTERNATELY, since \sometext is not a string, but a pointer to a string: \newcommand{\sometext}{abc} \newcommand{\mastertext}{abc} \ifdefequal{\sometext}{\mastertext}{True!}{False!} Dec 27, 2016 at 17:49

1 Answer 1

9

You need the 2nd \expandafter, otherwise you are only expanding the {.

\documentclass{scrartcl}

\usepackage{etoolbox}
\begin{document}

    \newcommand{\sometext}{abc}
    \expandafter\ifstrequal\expandafter{\sometext}{abc}{True!}{False!}

\end{document}

To elaborate more, based on follow up comments

\expandafter\ifstrequal{\sometext}{abc}{True!}{False!}

fails because the \expandafter acts upon the { for which there is no substitution text; thus, the \expandafter is moot in this case, and the test fails because \sometext is not literally equivalent to abc.

For the other attempt of

\expandafter\ifstrequal\sometext{abc}{True!}{False!}

it becomes equivalent, following expansion, to

\ifstrequal abc{abc}{True!}{False!}

Thus, here, a is compared with b. If True, c is printed; if false, abc is printed. Then {True!}{False!} are just left-over input stream that are acted upon outside of the string test.

Finally, for the case of

\expandafter\ifstrequal\sometext{}{abc}{True!}{False!}

the expansion becomes

\ifstrequal abc{}{abc}{True!}{False!}

As before, a is compared with b. Likewise, c becomes the TRUE text, and {} becomes the FALSE text. Then, {abc}{True!}{False!} are the left-over residuals.

6
  • Thank you! Could you (or someone) explain to me what is the precise interaction between \expandafter and {? Is { a token that Tex tries to expand, or that will be skipped by extendafter? Also, why doesn't my second attempt (without any curly braces around the first argument) work? I've seen expandafter used with no curly braces around the argument that's being expanded (after), so why doesn't it work here?
    – sgf
    Dec 27, 2016 at 17:57
  • 1
    @thisismynamenow \expandafter tries to expand the second token after it; if it is { nothing at all happens, except that \expandafter disappears after having done its job. In the second and third example, you're basically a and b, with c as true text and {} as false text; then abc, True! and False! are printed.
    – egreg
    Dec 27, 2016 at 18:01
  • @thisismynamenow TeX will try to expand what it is told to. In the case of { there is no further expansion possible, so it remains a {, making the \expandafter moot. In the 2nd case, without the curly braces, it ends up comparing a to b. Dec 27, 2016 at 18:02
  • I see. In my actualy example, the first two letters (or was it only the first?) went missing; that was the most irritating thing about it, but this explains it. Thanks for the help!
    – sgf
    Dec 27, 2016 at 18:06
  • @thisismynamenow I have updated my answer to try to capture the follow-on conversation with you, egreg, and myself. Dec 27, 2016 at 18:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .