0

The following is my MWE:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{etoolbox}

\newcommand{\test}[1]{%
\ifstrequal{#1}{bla}{{blu1}{blu2}}{{error}{schmerror}}
}

\newcommand{\switch}[2]{#2 #1}

\begin{document}

\test{bla} % this works

\switch12 % this works

\switch\test{bla} \\

\expandafter\switch\test{bla}

\expandafter\switch\expandafter{\test{bla}}

\switch{\test{bla}}

\end{document}

This is the output:

enter image description here

I'm trying to get the \switch macro to take the output of \test{bla} as argument, returning blu2 blu2. But not even \expendafter does the trick. Is there any way to get something like this to work?

The first two lines just make sure that both commands work as expected.

My first attempt, \switch\text{bla}, obviously swaps \test and {bla} resulting in bla errorschmerror since the argument of \test isn't bla. (I think it's the \par token, since the next attempt outputs on the same line if I leave out \\.)

My second attempt, \expandafter\switch\test{bla}, just returns bla. As far as I can tell, \test simply gets replaced by nothing on expansion, and \switch switches {bla} with something (no idea what though). At first I wondered whether this was due to \ifstrequal not being expandable, which might somehow result in \test expanding to , but then the next attempt should show the same behavior.

However, my third attempt obviously does replace \test{bla} with {blu1blu2} on expansion. This then gets switched with something by \switch. This isn't what I intend though, I want \switchto operate on two arguments, {blu1} and {blu2}, which should both be provided by \test.

The fourth attempt seems to do exactly the same thing.

  • ifstrequal doesn't work by expansion so you need to change the syntax of your calls, or use a different equality test – David Carlisle Jun 18 at 15:29
2

\expandafter\switch\test{bla}

is

\switch\ifstrequal{blah}{bla}{{blu1}{blu2}}{{error}{schmerror}}

which is

blah \ifstrequal {bla}{{blu1}{blu2}}{{error}{schmerror}}

which is not what you want.

You could change the call structure to

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{etoolbox}

\newcommand{\test}[2]{%
\ifstrequal{#2}{bla}{#1{blu1}{blu2}}{#1{error}{schmerror}}%
}

\newcommand{\switch}[2]{#2 #1}

\begin{document}

\test{}{bla}

\test\switch{bla} 

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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