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?

2 Answers 2


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”.

  • Thank you for reply. Is there a shorter way to do this?
    – Paulina R
    Aug 4, 2011 at 21:39
  • 4
    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. Aug 4, 2011 at 23:48
  • 1
    Yes, with that additional macro, it'll be really very short. Thanks for help :)
    – Paulina R
    Aug 5, 2011 at 20:33

2018 Update.

See "The etoolbox Package," v2.5e (19 Aug 2018)

The etoolbox Package (v2.5e)

On p.19:

\ifdefstring{(command)} {(string)} {(true)} {(false)}

Compares the replacement text of a (command) to a (string) and executes (true) if they are equal, and (false) otherwise. Neither the (command) nor the (string) is expanded in the test[,] and the comparison is category code agnostic. Control sequence tokens in the (string) argument will be detokenized and treated as strings. This command is robust. Note that it will only consider the replacement text of the (command). For example, this test



would yield (true). The prefix and the parameters of \mymacro as well as the category codes in the replacement text are ignored.

Here's a short code example.

% TEX encoding = UTF-8 Unicode
% TeXShop (ver. 2.47) (Mac OSX 10.5.8)
% compliation method LaTeX pdfTeX



\usepackage[dvipsnames*, x11names, svgnames, hyperref]{xcolor}


% Create some quick \TeX commands for color-info parsing

% Get the system's default color


Here's the xcolor step of obtaining the system's default color scheme:

% extract the color into Model and Color


Default Color Model: \dfltColorModel

Default Color: \dfltColor




Here's the comparison using \verb!{\ifdefstring}!





The trial and err-roar time it's taken to accomplish this one simple comparison is a story for another time.


Here's the output.

enter image description here


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.