1

This time I have a function that takes strings and shall test whether the color given after the last dash is in a given set of colors or not. While I am able to design a regular expression for it, it seems I am too stupid to access the results, that I expected to be stored in l_IconAndColor_seq.

\documentclass{standalone}

\ExplSyntaxOn

\NewDocumentCommand{\ColorNameChopper}{m}
{
    \my_colornamechopper:n { #1 }
}

\seq_new:N \l_IconAndColor_seq

\cs_new:Nn \my_colornamechopper:n
{
    #1: & \regex_extract_once:nnNTF 
    {\A(.*)-(Black|White)\Z}
     {#1}
     \l_IconAndColor_seq
     {TRUE & \seq_item:Nn \l_IconAndColor_seq {2} & \seq_item:Nn 
     \l_IconAndColor_seq {3} & 
     \seq_count:N \l_IconAndColor_seq}
     {FALSE}\\
}

\ExplSyntaxOff

\begin{document}
    \begin{tabular}{lllll}
    String & Found & First & Second & Size\\
    \ColorNameChopper{Test-Black}
    \ColorNameChopper{Test-White}
    \ColorNameChopper{Test-Orange}
    \end{tabular}
\end{document}

I am probably struggling of something simple, but I can't see it and so long the sequence is empty:

enter image description here

6
  • 1
    please follow the guide line for expl3 names. Use \l_tobibs_IconAndColor_seq and \tobibs_colornamechopper:n. Jun 9, 2022 at 17:15
  • Little nitpick: according to the rule spelled out in interface3.pdf (§8.1.2, Characters in regular expressions), the dash should be matched with \- in the regular expression: “non-alphanumeric printable ASCII characters can (and should) always be escaped: many of them have special meaning (e.g., use \(, \), \?, \., \^)”.
    – frougon
    Jun 9, 2022 at 17:39
  • @UlrikeFischer thank you for pointing that out, I am still not used to the overall syntax of LaTeX3 and should probably once read through the whole "The LATEX3 Interfaces" guide. Shame on me.
    – TobiBS
    Jun 9, 2022 at 19:24
  • @frougon thanks for pointing that out, it is a pity that there are so many dialects of RegExps out there, but i will improve my LaTeX3 RegExp skills.
    – TobiBS
    Jun 9, 2022 at 19:25
  • 1
    @TobiBS Regarding the naming conventions that Ulrike pointed out, you only need to read expl3.pdf which is 16 pages long, including bibliography and index. :-) interface3.pdf is a reference manual, I'd say “no one” reads it all in one go.
    – frougon
    Jun 9, 2022 at 20:09

1 Answer 1

2

You're setting the sequence in a table cell and try to use it in another one.

Besides, you want to use the last item in the sequence.

The first problem can be solved by expanding the contents prior to using it. The other one is solved by using item –1

\documentclass{article}

\ExplSyntaxOn

\NewDocumentCommand{\ColorNameChopper}{m}
  {
    \tobibs_colornamechopper:n { #1 }
  }

\seq_new:N \l__tobibs_colornamechopper_seq

\cs_new_protected:Nn \tobibs_colornamechopper:n
  {
    #1: &
     \regex_extract_once:nnNTF {(.*)-(Black|White)} {#1} \l__tobibs_colornamechopper_seq
       {
         \use:e
           {
             TRUE &
             \seq_item:Nn \l__tobibs_colornamechopper_seq {-2} &
             \seq_item:Nn \l__tobibs_colornamechopper_seq {-1} &
             \seq_count:N \l__tobibs_colornamechopper_seq
           }
       }
       {FALSE}
    \\
  }

\ExplSyntaxOff

\begin{document}

\begin{tabular}{lllll}
String & Found & First & Second & Size\\
\ColorNameChopper{Test-Black}
\ColorNameChopper{Test-White}
\ColorNameChopper{Test-Orange}
\ColorNameChopper{Test-again-Black}
\end{tabular}

\end{document}

enter image description here

How and why does it work? First of all, your command \regex_extract_once:nnNTF sets the sequence in a table cell, so as soon as TeX processes & (when typesetting), the value would be lost.

However, if we build the whole table row before processing it, the value would not be lost (expansion takes place at a different level than typesetting, so TeX will not “see” the &, as it's just expanding macros and still not building the table. Yes, tables are quite hard to manage.

11
  • I believe the idea behind “First” and “Second” is, for the OP, to show the contents of the two capturing groups (items 2 and 3 of \l__tobibs_colornamechopper_seq). Also, maybe you could say that \use:e does the intended job here because \seq_item:Nn and \seq_count:N are expandable.
    – frougon
    Jun 9, 2022 at 17:25
  • @frougon Perhaps. But look at the original code…
    – egreg
    Jun 9, 2022 at 17:30
  • I did: I was preparing an answer but you posted before I finished. :-) I'm saying this because I think the “expandable” condition is not obvious for the OP. Anyway, it is now spelled out here.
    – frougon
    Jun 9, 2022 at 17:32
  • 1
    @TobiBS I fixed the code so the first part is also shown; I also added some comments.
    – egreg
    Jun 9, 2022 at 19:40
  • 1
    @TobiBS This would be way too long for a comment, but the “annex” here should put you on track. Expandable macros are marked with a star in interface3.pdf.
    – frougon
    Jun 9, 2022 at 20:06

You must log in to answer this question.

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