4

My idea is to use xstring package for dealing with some string tests. In my case, I used the \strLeft function for extracting the 3 first letters of a string. Then, I use this result in \IfStrEqCase function.

\newcommand\textttarg[1]{\expandarg #1\,\,  \IfStrEqCase{#1}{{BOV}{bbov}{BOH}{bboh}}[ff]}
\def\nnnnn{\expandarg\StrLeft{BOVComp}{3}}
\nnnnn\\
\textttarg{\nnnnn}

The result is very strange because the word is BOVComp. Extract the 3 first letters: BOV. The test \IfStrEqCase don't see the 3 letters correctly and returns the default result ff.

Here is my MWE:

\documentclass[]{article}
\usepackage{tikz}
\usepackage{xstring}

\begin{document}
\newcommand\textttarg[1]{\expandarg #1\,\,  \IfStrEqCase{#1}{{BOV}{bbov}{BOH}{bboh}}[ff]}
\def\nnnnn{\expandarg\StrLeft{BOVComp}{3}}

\nnnnn\\
\textttarg{\nnnnn}

\end{document}
3
  • \IfStrEqCase sees \expandarg\StrLeft{BOVComp}{3}, not BOV.
    – egreg
    Jan 19, 2015 at 14:44
  • @egreg Are you sure? #1 returns BOV (see overleaf.com/2122963tfzqky). If yes, do you have any idea for expanding #1 because the test?
    – Guuk
    Jan 19, 2015 at 14:47
  • \StrLeft is not (fully) expandable. It's the set of instructions for producing the first n tokens from its argument, but \expandarg doesn't do the business.
    – egreg
    Jan 19, 2015 at 14:49

1 Answer 1

6

With \expandarg, the first token in the arguments of \IfStrEqCase is expanded once.

Since your argument is \nnnnn, this results in doing

 \IfStrEqCase{\expandarg\StrLeft{BOVComp}{3}}{{BOV}{bbov}{BOH}{bboh}}[ff]

so the ‘string’ which is compared against BOV or BOH is \expandarg\StrLeft{BOVComp}{3} and the tests are obviously unsuccessful. Note that when the manual of xstring talks about ‘strings’ it actually refers to tokens, because that's what TeX understands.

Using \fullexpandarg wouldn't help either, because \StrLeft is not fully expandable, since it performs assignments.

Of course, using \nnnnn alone prints BOV, because in this case TeX goes all the way, performing the necessary assignments.

You probably want

\newcommand{\nnnnn}{BOVComp\StrLeft{BOVComp}{3}[\temp]}

\newcommand{\textttarg}[1]{%
  #1
  \begingroup\expandarg % localize the effect of \expandarg
  \IfStrEqCase{\temp}{{BOV}{bbov}{BOH}{bboh}}[ff]%
  \endgroup
}

Full example:

\documentclass{article}
\usepackage{xstring}

\newcommand{\nnnnn}{BOVComp}

\newcommand{\textttarg}[1]{%
  #1
  \begingroup\expandarg % localize the effect of \expandarg
  \StrLeft{#1}{3}[\temp]%
  \IfStrEqCase{\temp}{{BOV}{bbov}{BOH}{bboh}}[ff]%
  \endgroup
}

\begin{document}

\textttarg{\nnnnn}

\textttarg{BOHComp}

\end{document}

enter image description here


Here's how I'd do it with expl3:

\documentclass{article}
\usepackage{xparse}


\ExplSyntaxOn
\NewDocumentCommand{\textttarg}{m}
 {
  #1~
  \str_case_x:nnF { \__guuk_get_three:o { #1 } }
   {
    {BOV}{bbov}
    {BOH}{bboh}
   }
   {ff}
}

\cs_new:Npn \__guuk_get_three:n #1
 {
  \__guuk_get_three:w #1 . . . \q_stop
 }
\cs_generate_variant:Nn \__guuk_get_three:n { o }

\cs_new:Npn \__guuk_get_three:w #1 #2 #3 #4 \q_stop
 {
  #1#2#3
 }
\ExplSyntaxOff
\newcommand{\nnnnn}{BOVComp}

\begin{document}

\textttarg{\nnnnn}

\textttarg{BOHComp}

\textttarg{Uh}

\end{document}

enter image description here

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .