Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

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}
share|improve this answer
add comment

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}

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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