13

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?

  • 1
    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
  • 1
    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
21

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}
  • 1
    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

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.