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.
\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}