4

In a previous post

I want to compare the last character of a string with other characters

I asked how to compare the last character of a string with another char, and the solutions offered have solved my problem. However, I have observed that, if I import the strings from a .csv file, they fail. I offer here a minimal example:

\documentclass{paper}

\usepackage{csvsimple-l3}

\begin{filecontents*}{test.csv}
    asdf
    qwer
    uioe
\end{filecontents*}

\ExplSyntaxOn
\NewDocumentCommand\IfLastEqualTF{mm}{
    \str_if_eq:eeTF {\str_item:nn{#1}{-1}}  {\str_item:nn{#2}{-1}}
}
\ExplSyntaxOff

\begin{document}

\IfLastEqualTF{asdf}{f}{yes}{no}
\IfLastEqualTF{qwer}{f}{yes}{no}
\IfLastEqualTF{uioe}{f}{yes}{no}

\csvreader[no head]{test.csv}{}
\csvreader[no head]{test.csv}{}
{
    \csvcoli~\IfLastEqualTF{\csvcoli}{f}{yes}{no}
}

\end{document}

When I execute it, I get

yes no no
asdf no qwer no uioe no

but I should get

yes no no asdf yes qwer no uioe no

Why does this happen? Because \csvreader reads correctly the file, but then the strings do not have the same behavior as when I type them inside the .tex file.

1 Answer 1

3

You need one more expansion step, namely to fully expand \csvcoli before the action of \str_if_eq:nnTF.

\documentclass{paper}
\usepackage{csvsimple-l3}

\begin{filecontents*}{\jobname.csv}
    asdf
    qwer
    uioe
\end{filecontents*}

\ExplSyntaxOn
\NewDocumentCommand\IfLastEqualTF{mm}
  {
    \user_iflastequal:en { #1 } { #2 }
  }

\cs_new:Nn \user_iflastequal:nn
  {
    \str_if_eq:eeTF {\str_item:nn{#1}{-1}}  {\str_item:nn{#2}{-1}}
  }
\cs_generate_variant:Nn \user_iflastequal:nn { e }
\ExplSyntaxOff

\begin{document}

\IfLastEqualTF{asdf}{f}{yes}{no}
\IfLastEqualTF{qwer}{f}{yes}{no}
\IfLastEqualTF{uioe}{f}{yes}{no}

\csvreader[no head]{\jobname.csv}{}{%
    \csvcoli~\IfLastEqualTF{\csvcoli}{f}{yes}{no}
}

\end{document}

enter image description here

If you need accented characters, full expansion is dangerous. For this case I suggest to use a *-variant when the argument has to be extracted from a control sequence such as \csvcoli.

\documentclass{paper}
\usepackage{csvsimple-l3}

\begin{filecontents*}[overwrite]{\jobname.csv}
    asdf
    qwer
    uioe
    áéíóú
\end{filecontents*}

\ExplSyntaxOn

\NewExpandableDocumentCommand{\IfLastEqualTF}{smmmm}
 {
  \IfBooleanTF{#1}
   {
    \user_iflastequal:Vnnn #2 { #3 } { #4 } { #5 }
   }
   {
    \user_iflastequal:nnnn { #2 } { #3 } { #4 } { #5 }
   }
 }

\cs_new:Nn \user_iflastequal:nnnn
 {
  \str_if_eq:eeTF { \__user_lastitem:n { #1 } } { #2 } { #3 } { #4 }
 }
\cs_generate_variant:Nn \user_iflastequal:nnnn { V }
 
\sys_if_engine_pdftex:TF
 {
  \cs_new:Nn \__user_lastitem:n
   {
    \tl_item:en { \text_map_function:nN { #1 } \__user_brace_items:n } { -1 }
   }
 }
 {
  \cs_new:Nn \__user_lastitem:n { \str_item:nn { #1 } { -1 } }
 }

\cs_generate_variant:Nn \tl_item:nn { e }
\cs_new:Nn \__user_brace_items:n { {#1} }

\ExplSyntaxOff

\begin{document}

\IfLastEqualTF{asdf}{f}{yes}{no}
\IfLastEqualTF{qwer}{f}{yes}{no}
\IfLastEqualTF{uioe}{f}{yes}{no}
\IfLastEqualTF{áéíóú}{ú}{yes}{no}
\IfLastEqualTF{áéíóú}{ó}{yes}{no}

\csvreader[no head]{\jobname.csv}{}{%
    \csvcoli~\IfLastEqualTF*{\csvcoli}{f}{yes}{no}
}

\csvreader[no head]{\jobname.csv}{}{%
    \csvcoli~\IfLastEqualTF*{\csvcoli}{ú}{yes}{no}
}

\end{document}

enter image description here

1
  • Thank you very very much! BTW, what does "to fully expand" mean? Is there any reference on this? Thank you again!
    – User
    Commented Jun 16 at 21:51

You must log in to answer this question.

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