Comparing an argument to a string when argument is a result of a command with etoolbox
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:
\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”.
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
\long\edef\mymacro#1#2{\string&}
\ifdefstring{\mymacro}{&}{true}{false}
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
\documentclass[12pt]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{etoolbox}
\usepackage{verbatim}
\usepackage[dvipsnames*, x11names, svgnames, hyperref]{xcolor}
\obeylines
% Create some quick \TeX commands for color-info parsing
\def\dfltColorModel
\def\dfltColor
% Get the system's default color
\AtBeginDocument{\colorlet{defaultcolor}{.}}
\begin{document}
Here's the xcolor step of obtaining the system's default color scheme:
% extract the color into Model and Color
\begin{verbatim}
\extractcolorspecs{.}{\dfltColorModel}{\dfltColor}
\end{verbatim}
\extractcolorspecs{.}{\dfltColorModel}{\dfltColor}
Default Color Model: \dfltColorModel
Default Color: \dfltColor
\bigskip
\begin{testcolors}[rgb,cmyk,hsb,HTML,gray]
\testcolor{.}
\end{testcolors}
\bigskip
Here's the comparison using \verb!{\ifdefstring}!
\bigskip
\begin{verbatim}
\ifdefstring{\dfltColorModel}{gray}{\textcolor{blue}{true}}{false}
\end{verbatim}
\ifdefstring{\dfltColorModel}{gray}{\textcolor{blue}{true}}{false}
\bigskip
The trial and err-roar time it's taken to accomplish this one simple comparison is a story for another time.
\end{document}
Here's the output.