# ifthenelse equal string comparison fails

I am trying to fix some problems with the accessibility.sty package from www.babs.gmxhome.de

There is one string comparison which always results to false, i.e. the language code definition will not be executed.

\ifthenelse{\equal{#1}{\string german}}{\gdef\LanguageCode{/Lang(DE)}}{}%


Now if I try

\message{#1}
\message{\string german}


The output when compiling a document will be:

german german

I am really lost, why #1 and the string german do not compare to true here... What could be the reason?

• Why have you got \string here? That gives a catcode-12 ('other') g and then catocde-11 ('letter') erman. I have a feeling you are imagining that the two arguments are the same just because the letters look identical when you typeset them. – Joseph Wright Aug 8 '14 at 15:56
• I am not sure, if I understand your comment. Afaik \string takes a token and returns a string of characters, no?! – ndbd Aug 8 '14 at 16:27
• @nbd: Better use xstring package for example – user31729 Aug 8 '14 at 16:41
• I tried, i.e. message{#1}, \IfStrEq{#1}{\string german}{message{moo}}{} which results in output "german". I just don't get this – ndbd Aug 8 '14 at 16:56
• Can you show how you use this? It's important to know what argument you're passing to \equal as #1. – egreg Aug 8 '14 at 17:21

The \ifthenelse test does a token-based comparison. Thus when you do

\documentclass{article}
\usepackage{ifthen}
\newcommand{\foo}{%
\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{%
\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}{%
\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{%
\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}{%
\ifthenelse{\equal{#1}{german}}
{TRUE}
{FALSE}%
}
\begin{document}

\foo{german}

\end{document}

• Thanks very much for this insightful and detailed explanation. Indeed, just leaving out \string is sufficient, since the input is already (or can assumed to be) sensible. I really was under the impression that "german" would be considered as one token. Again, thanks! – ndbd Aug 8 '14 at 20:29
• @ndbd Are you perhaps a programmer is some other language (C?): TeX's idea of a 'token' tends to confuse such people, as it's not really the same concept. – Joseph Wright Aug 8 '14 at 21:56
• Plenty of experience in OCaml, C, some python... Yes. TeX is certainly different :-) – ndbd Aug 9 '14 at 20:48

I would like to add another simple way of making a string-based choice in plain TeX without the need for additional packages. It makes use of the TeX macro expansion and uses \csname ... \endcsname to build a macro name.

\documentclass[11pt,a4paper]{article}

\makeatletter
% define macros that are executed for every given string
\def\setlang@english{\gdef\LanguageCode{/Lang(EN)}}
\def\setlang@german{\gdef\LanguageCode{/Lang(DE)}}
\def\setlang@dutch{\gdef\LanguageCode{/Lang(NL)}}
\newcommand{\setlang}{
% call the macro corresponding to argument #1
\csname setlang@#1\endcsname
}
\makeatother

% test the macro
\begin{document}
\setlang{english}
\LanguageCode
\setlang{dutch}
\LanguageCode
\end{document}
{"ft_lang_label":"__label__en","ft_lang_prob":0.6413355,"math_prob":0.5279162,"size":2571,"snap":"2020-24-2020-29","text_gpt3_token_len":801,"char_repetition_ratio":0.119205296,"word_repetition_ratio":0.03821656,"special_character_ratio":0.2598211,"punctuation_ratio":0.06326034,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9520971,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-07-07T00:33:03Z\",\"WARC-Record-ID\":\"<urn:uuid:74a2943b-f7a5-42ed-bf5a-1f77100e1208>\",\"Content-Length\":\"165874\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:307be7c2-cf41-4e86-b7e7-af0c09a3d2a1>\",\"WARC-Concurrent-To\":\"<urn:uuid:2297877b-06b9-4b9e-b3fb-dba856f30601>\",\"WARC-IP-Address\":\"151.101.129.69\",\"WARC-Target-URI\":\"https://tex.stackexchange.com/questions/195491/ifthenelse-equal-string-comparison-fails/195498\",\"WARC-Payload-Digest\":\"sha1:M647H4U65KRQYJWD3K5YLFMHSE6CPR3A\",\"WARC-Block-Digest\":\"sha1:IW2RTX63RBOFZ7LKSTN3OTGCWXCQDJJE\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-29/CC-MAIN-2020-29_segments_1593655890566.2_warc_CC-MAIN-20200706222442-20200707012442-00517.warc.gz\"}"}