5

I'm trying convert an argument to lowercase, strip out the spaces, and then search for the resulting string in a list. Thus far, I've had no luck.

Here's the code I would instinctively expect to work:

\newcommand{\mylist}{one,two,three}

\newcommand{\checkInList}[1]{ 
\StrDel{#1}{ }[\temp]
\IfSubStr{\mylist}{\lowercase{\temp}}
{yes}
{no}
}

\checkinlist{one}

I would expect that to print yes, but it does not.

I've found numerous questions also dealing with this topic, but so far none of the solutions have worked for me. The closest I came to a solution was in \uppercase does not work with \IfSubStr?, but trying to alter that solution to my code just resulted in both yes and no being printed. I also made some slight progress with Pass a macro (with arguments) as an argument for another macro, but ultimately failed in the end there as well.

If there's a better way to strip spaces, force case, and check in a list of things, I am entirely open to something completely different. I have no real reason to stick with any of the commands I'm currently using over any others.

  • Are you open to expl3 features? – user31729 Jan 6 '16 at 23:14
  • \lowercase is not expandable. You should lowercase the string in advance, then do the search. – egreg Jan 6 '16 at 23:14
  • @ChristianHupfer I don't know anything about expl3 to know why I'd be opposed, so I suppose not – SnoringFrog Jan 6 '16 at 23:33
  • @SnoringFrog: It's the 'new' LaTeX 'programming syntax -- quite powerful ;-) – user31729 Jan 6 '16 at 23:37
7

The primitive \lowercase is not expandable; you have to first lowercase the argument and then use the lowercased string:

\documentclass{article}
\usepackage{xstring}

\newcommand{\mylist}{one,two,three}

\newcommand{\checkinlist}[1]{%
  \lowercase{\def\temp{#1}}%
  \StrDel{\temp}{ }[\temp]%
  \IfSubStr{\mylist}{\temp}{yes}{no}%
}

\begin{document}

\checkinlist{one}

\checkinlist{One}

\checkinlist{o N e}

\checkinlist{o N f}

\end{document}

enter image description here

  • I tried something like this at one point, but was using \def\temp{\lowercase{#1}}. Putting lowercase on the outside never occurred to me. – SnoringFrog Jan 6 '16 at 23:29
5

Here's an expl3 version

\documentclass{article}

\usepackage{xparse}

\newcommand{\mylist}{one,two,three, egreg was faster}

\ExplSyntaxOn
\newcommand{\CheckInList}[2]{%
  \tl_set:Nx \l_tmpa_tl {\tl_lower_case:n {#1}} %transform the list lowercase
  \clist_set:Nx \l_tmpa_clist {\l_tmpa_tl} % make a real list
  \tl_set:Nx \l_tmpb_tl {\tl_lower_case:n {#2}} % lower case second arg
  \clist_if_in:NVTF \l_tmpa_clist {\l_tmpb_tl} {Yes!} {No!} % Check if #2 is in the list
}
\ExplSyntaxOff
\begin{document}

\CheckInList{\mylist}{One}

\CheckInList{\mylist}{two}

\CheckInList{\mylist}{Egreg was faster}

\CheckInList{\mylist}{Should egreg get the tick?}

\end{document}

enter image description here

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.