Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I've got an example like that:

\documentclass{article}

\usepackage{etoolbox}

\newcommand{\ab}{a}

\newcommand{\aORb}[1]{%
        \ifstrequal{#1}{a}{"a" was given}{not a}, %
        \ifstrequal{#1}{b}{"b" was given}{not b}%
}

\begin{document}
 begin

     \aORb{\ab{}}

 end
\end{document}

And the output of that code is:

begin
not a, not b
end

However, if I put simply "a" or "b" as an argument to \aORb instead of \ab{} then it works fine.

Is there another command in etoolbox package that will allow me to do such a comparison correctly?

share|improve this question

1 Answer 1

up vote 13 down vote accepted

The etoolbox manual states for \ifstrequal on page 17: (emphasis mine)

Compares two strings and executes htruei if they are equal, and hfalsei otherwise. The strings are not expanded in the test and the comparison is category code agnostic.

So you always compare the string "\ab{}" with "a" and "b". If you want to allow macros as input you need to expand them manual:

\newcommand{\aORb}[1]{%
        \expandafter\ifstrequal\expandafter{#1}{a}{"a" was given}{not a}, %
        \expandafter\ifstrequal\expandafter{#1}{b}{"b" was given}{not b}%
}

This expands the first token of #1 once before \ifstrequal is processed.

If you want to expand the input completely use:

\newcommand{\aORb}[1]{%
        \edef\mytemp{{#1}}%
        \expandafter\ifstrequal\mytemp{a}{"a" was given}{not a}, %
        \expandafter\ifstrequal\mytemp{b}{"b" was given}{not b}%
}

This expands the input completely using \edef and wraps it in braces, so only one \expandafter is enough.

Then you need to use \aORb{\ab} not \aORb{\ab{}}, because \ab doesn't take an argument the {} would stay and you would compare here "a{}" with "a" and "b".

share|improve this answer
    
Thank you for reply. Is there a shorter way to do this? –  Paulina R Aug 4 '11 at 21:39
1  
It's already quite short IMHO, but you can write an own macro like \newcommand\myifstrequal{\expandafter\ifstrequal\expandafter} and then use it instead of \ifstrequal. –  Martin Scharrer Aug 4 '11 at 23:48
1  
Yes, with that additional macro, it'll be really very short. Thanks for help :) –  Paulina R Aug 5 '11 at 20:33

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.