In the following MWE both tests are true on pdflatex and inputenc but the second test fails on fontspec and luatex/xelatex. Any chance to get this engine agnostic (both true)?

\documentclass{article}
%\usepackage[T1]{fontenc}\usepackage[utf8]{inputenc}
\usepackage{fontspec}
%\usepackage[logonly]{trace}\traceon
\usepackage{xstring}


\begin{document}

ß in Maß: \IfSubStr{Maß}{ß}{TRUE}{FALSE}%
\newline%
\ss{} in Maß: \IfSubStr{Maß}{\ss}{TRUE}{FALSE}%

\end{document}
  • 1
    ß is not an active char with luatex/xetex. You could change this, but why do you want to do it? – Ulrike Fischer May 14 at 9:57
  • The idea was to restrict package code to us-ascii. – Archäopath May 14 at 10:11
up vote 5 down vote accepted

The fact that both tests with pdflatex return true is essentially a lucky coincidence.

The package xstring performs full expansion to the tokens in (some of) its arguments (see the package documentation); in the case of \IfSubStr the first and second arguments are fully expanded.

What happens to ß in pdflatex is:

  1. ß is actually two bytes in UTF-8;
  2. the first byte, 0xC3 makes TeX examine the next one, 0x9F and transform both according to an internal table to \ss (in utf8enc.dfu you find \DeclareUnicodeCharacter{00DF}{\ss});
  3. according to the current output encoding (here T1), \ss is transformed into \T1\ss (a quite strange token) which is internally defined through \chardef so it is unexpandable.

Thus you see that the same happens when you type \ss, just jumping over some of the above steps.

When lualatex is used, ß undergoes no expansion at all, because lualatex directly reads UTF-8 and internally transforms it into (abstract) Unicode. On the other hand, \ss becomes \TU\ss (similar to the above), which is essentially equivalent to type \char`ß (so it's not the same as typing ß).

Try \IfSubStr{Ș}{Ș}{TRUE}{FALSE} with pdflatex and you'll get a low level error

! Illegal parameter number in definition of \@xs@arg@ii.
<to be read again> 
                   \crcr 
l.15 \IfSubStr{Ș}{Ș}
                      {TRUE}{FALSE}

No error with lualatex.

There's no guarantee of success when \IfSubStr is used for non ASCII characters.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.