2

Is it a bug?

Following code got BUG STR and PASS TL

\documentclass{article}
\ExplSyntaxOn
\str_set:Nn \l_tmpa_str { b }
\clist_if_in:nVTF { a, b } \l_tmpa_str
  { \tl_show:n { PASS~STR } }
  { \tl_show:n { BUG~STR } }
\tl_set:Nn \l_tmpa_tl { b }
\clist_if_in:nVTF { a, b } \l_tmpa_tl
  { \tl_show:n { PASS~TL } }
  { \tl_show:n { BUG~TL } }
\ExplSyntaxOff

\begin{document}

\end{document}
4
  • 5
    well a string b has catcode 12 and so is different to the letter b with catcode 10. Jan 12, 2022 at 19:41
  • @UlrikeFischer Thanks! I forget the catcode. But it is counterintuitive sometimes. (:
    – ZhiyuanLck
    Jan 12, 2022 at 19:45
  • For debugging \tl_analysis_show (or other pretty printer) helps, although... okay I need something to pretty print clist too. // Another case of stored "other" catcode being confusing...
    – user202729
    Jan 13, 2022 at 0:14
  • expl3's str is the de-tokenized token list, which is different to the string type in most other programming languages. What you need here is most likely a token list, for example \l_tmpa_tl. Also see the beginning paragraphs in texdoc interafce3, chap. 16 "The l3str package: Strings". Jan 13, 2022 at 6:17

1 Answer 1

3

I find it useful to think to str variables as being a different data type from anything else in TeX, so they can only be compared with each other, but not with other data types unless they are “stringified”.

This is of course not the “truth”. Actually any character token in TeX has a category code assigned at the moment it enters TeX's mouth (in more technical terms, after tokenization). In a str variable all character tokens (except space tokens, which retain their category code 10) are assigned category code 12.

The a inside the clist variable you want to check the presence of, however, under normal conditions, has category code 11.

You might define a (probably slow) routine to check whether the clist contains something whose “stringification” is the same as what you want to test.

\ExplSyntaxOn

\bool_new:N \l__zhiyuan_clist_str_bool
\prg_new_protected_conditional:Nnn \zhiyuan_clist_str_if_in:nn { T, F, TF }
 {
  \bool_set_false:N \l__zhiyuan_clist_str_bool
  \__zhiyuan_clist_str_check:ne { #1 } { \tl_to_str:n { #2 } }
  \bool_if:NTF \l__zhiyuan_clist_str_bool
   { \prg_return_true: }
   { \prg_return_false: }
 }

\cs_new_protected:Nn \__zhiyuan_clist_str_check:nn
 {
  \clist_map_inline:nn { #1 }
   {
    \str_if_eq:eeT { \tl_to_str:n { ##1 } } { #2 }
     {
      \clist_map_break:n { \bool_set_true:N \l__zhiyuan_clist_str_bool }
     }
   }
 }
\cs_generate_variant:Nn \__zhiyuan_clist_str_check:nn { ne }

\prg_generate_conditional_variant:Nnn \zhiyuan_clist_str_if_in:nn { nV, VV } { T, F, TF }

\str_set:Nn \l_tmpa_str { b }

\zhiyuan_clist_str_if_in:nVTF { a,b } \l_tmpa_str
 { \iow_term:n { YES } }
 { \iow_term:n { NO } }

\zhiyuan_clist_str_if_in:nVTF { a,c } \l_tmpa_str
 { \iow_term:n { YES } }
 { \iow_term:n { NO } }

\ExplSyntaxOff

\stop

This will print on the console

YES
NO
6
  • It may be a little faster to only run \tl_to_str once on #2 instead of every iteration.
    – user202729
    Jan 13, 2022 at 0:18
  • @user202729 I was even tempted to remove it, assuming the second argument is a string. Yes, it might be marginally faster. Maybe later I'll modify it.
    – egreg
    Jan 13, 2022 at 0:23
  • Well, it is a solution. But I wonder if there are some user cases that the catcode of item of an n-type comma list need to be different with value of a string when used in \clist_if_in:nV. Maybe it is more user-friendly and straightforward to write \clist_if_in:nVTF { abc } \l_tmpa_str? And for some corner cases, we can provide another version just like \str_case_e:nn.
    – ZhiyuanLck
    Jan 13, 2022 at 3:23
  • @ZhiyuanLck I'm not aware of such use cases. Do you have any?
    – egreg
    Jan 13, 2022 at 6:57
  • @egreg No. So is it better if we change the internal behavior of \clist_if_in:nV when it meets a string variable?
    – ZhiyuanLck
    Jan 13, 2022 at 11:12

You must log in to answer this question.

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