ifthenelse equal string comparison fails
The \ifthenelse
test does a token-based comparison. Thus when you do
\documentclass{article}
\usepackage{ifthen}
\newcommand{\foo}[1]{%
\ifthenelse{\equal{#1}{\string german}}
{TRUE}
{FALSE}%
}
\begin{document}
\foo{german}
\end{document}
what happens is that \string
is applied to the first token it sees, in this case a g
. Comparing the two results, they are not the same: one has one non-letter then five letters, the second has six letters. Typesetting those two cases is different: g
with category code 12 ('other') typesets the same glyph as g
with category code 11 ('letter'), so the two look the same.
There are various approaches to doing true 'string' comparisons in TeX. With a modern TeX engine, by far the easiest is to use \pdfstrcmp
or equivalent:
\documentclass{article}
\usepackage{pdftexcmds}
\makeatletter
\newcommand\foo[1]{%
\ifnum\pdf@strcmp{\unexpanded{#1}}{german}=0 %
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{TRUE}
{FALSE}%
}
\makeatother
\begin{document}
\foo{german}
\end{document}
This does do a string comparison, ignoring category codes. If you want to stick with \ifthenelse
but can assume e-TeX then
\documentclass{article}
\usepackage{ifthen}
\newcommand{\foo}[1]{%
\ifthenelse{\equal{\detokenize{#1}}{\detokenize{german}}}
{TRUE}
{FALSE}%
}
\begin{document}
\foo{german}
\end{document}
will work as \detokenize
makes its entire argument into a string.
Another approach without needing anything other than classical TeX primitives is to use something like
\documentclass{article}
\usepackage{pdftexcmds}
\makeatletter
\newcommand\foo[1]{%
\begingroup
\def\@tempa{#1}%
\@onelevel@sanitize\@tempa
\def\@tempb{german}%
\@onelevel@sanitize\@tempb
\ifx\@tempa\@tempb
\aftergroup\@firstoftwo
\else
\aftergroup\@secondoftwo
\fi
\endgroup
{TRUE}
{FALSE}%
}
\makeatother
\begin{document}
\foo{german}
\end{document}
using the fact that \@onelevel@sanitize
also converts things to strings.
Of course, if you know that the input will be something sensible, there's no real need to use any detokenization at all
\documentclass{article}
\usepackage{ifthen}
\newcommand{\foo}[1]{%
\ifthenelse{\equal{#1}{german}}
{TRUE}
{FALSE}%
}
\begin{document}
\foo{german}
\end{document}