1

I am learning LaTeX3 syntax and have created a minimal example to understand how clists work. I realized that when an element is inserted using \clist_push:Nn, the \clist_if_in:NnTF condition doesn't work for it. For example, suppose we have unique elements to begin with and decided to insert another unique element 9, then checking if 9 exists in the list yields false. I would appreciate an explanation to this as well as any suggestions to improving the code's standard.

Output

\documentclass{article}
\ExplSyntaxOn
\clist_new:N \l_aljumaily_myCList_clist
\tl_new:N \l_aljumaily_input_tl
\NewDocumentCommand{\useCList}{}{
  \clist_use:Nnnn \l_aljumaily_myCList_clist{~and~}{,~}{,~and~}
}
\NewDocumentCommand{\setCList}{m}{
  \clist_set:Nn \l_aljumaily_myCList_clist{#1}
}
\NewDocumentCommand{\printLength}{}{
  \clist_count:N \l_aljumaily_myCList_clist
}
\NewDocumentCommand{\getItem}{m}{
  \clist_item:Nn \l_aljumaily_myCList_clist{#1}
}
\NewDocumentCommand{\insertToLeft}{m}{
  \tl_set:Nn \l_aljumaily_input_tl {#1}
  \clist_push:Nn\l_aljumaily_myCList_clist {\l_aljumaily_input_tl}
}
\NewDocumentCommand{\isInList}{m}{
  \str_set:Nn \l_aljumaily_input_str {#1}
  \clist_if_in:NnTF \l_aljumaily_myCList_clist {#1}{Yes}{No}
}
\ExplSyntaxOff

\begin{document}
\setCList{7, 3, 9, 1, 2}
Printing list: \useCList\par
Length is: \printLength\par
Item at index 1: \getItem{1}\par
Printing list: \useCList\par
Inserting 4 to left\insertToLeft{4}\par
Item at index 1: \getItem{1}\par
Printing list: \useCList\par
Is item 4 in: \isInList{4}.\par
Is item 3 in: \isInList{3}.\par
Is item 9 in: \isInList{9}.\par
Is item 1 in: \isInList{1}.\par
Is item 2 in: \isInList{2}.\par
Is item 7 in: \isInList{7}.
\end{document}

1 Answer 1

3

You have misunderstood the nature of the second argument to \clist_push:Nn. It is an n-type token list, i.e. exactly what you pass to it. So

  \tl_set:Nn \l_aljumaily_input_tl {#1}
  \clist_push:Nn\l_aljumaily_myCList_clist {\l_aljumaily_input_tl}

adds \l_aljumaily_input_tl to the clist. (You can check that with \clist_show:N \l_aljumaily_myCList_clist.)

What you want to do is pass the value of the tl:

\clist_push:NV\l_aljumaily_myCList_clist \l_aljumaily_input_tl
3
  • Thank you for the response! I now understand where you are coming from. I kindly wonder why it worked for the other values but not the one inserted? Mar 21 at 18:18
  • 2
    @M.AlJumaily In your \setCList macro, you add the values directly; there is no intervention by another macro. But in your \insertToLeft macro you are adding your token list macro. So an alternative to Joseph's solution is to just use ` \clist_push:Nn\l_aljumaily_myCList_clist {#1}` in the \insertToLeft macro.
    – Alan Munn
    Mar 21 at 18:23
  • @AlanMunn and JosephWright: thank you for your time and explanation! I know understand the flaw in the code. Mar 21 at 18:31

You must log in to answer this question.

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