# \uppercase does not work with \IfSubStr?

I'm using \IfSubStr macro of xstring package to decide if user given options contains some letter and if they do I take some actions.

I want case independent test on whether user options contain that letter so I thought I will just use \uppercase macro to implement case independent comparison. But it does not work as I expect.

Here is minimal demonstration:

\usepackage{xstring}
\newcommand{\test}[1]{%
\IfSubStr{\uppercase{#1}}{K}{%then
In then%
}{%else
In else%
}
}
\begin{document}
\test{k}
\end{document}


I would expect this to print In then, but instead it prints In else. Why is that and how do I fix it so that it prints In then as I expect?

-

You have to do \uppercase first:

\documentclass{article}
\usepackage{xstring}
\newcommand{\test}[1]{%
\uppercase{\IfSubStr{#1}}{K}{%then
In then%
}{%else
In else%
}
}
\begin{document}
\test{k}
\end{document}


The primitive \uppercase works in a rather peculiar way: it examines the list of tokens given as its argument and changes all character tokens into their uppercase equivalent, then puts back the processed token list in the input stream.

With your test you're comparing \uppercase{k} with K, which are distinct (and enabling expansion will not help, because \uppercase is unexpandable. By doing it first, what TeX finally sees is the required

\IfSubStr{K}{K}

-

without using \uppercase

\documentclass{article}
\usepackage{xstring}
\newcommand{\test}[1]{%
\IfSubStr{#1}{K}{In then }{\IfSubStr{#1}{k}{In then }{ In else }}}
\begin{document}
\test{k} \test{aa} \test{AAKk} \test{aaA}
\end{document}


-