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

I've got an example like that:




        \ifstrequal{#1}{a}{"a" was given}{not a}, %
        \ifstrequal{#1}{b}{"b" was given}{not b}%




And the output of that code is:

not a, not b

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
up vote 25 down vote accepted

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

Compares two strings and executes ⟨true⟩ if they are equal, and ⟨false⟩ 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:

        \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:

        \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
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
Yes, with that additional macro, it'll be really very short. Thanks for help :) – Paulina R Aug 5 '11 at 20:33

Your Answer


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.