# How to check if comma list is empty?

How do I check if \myList is empty? Following (is my attempt) prints "NOT empty" when, in fact, the list is empty.

\documentclass{standalone}
\usepackage{expl3}
\begin{document}
\ExplSyntaxOn
\clist_new:N\myList
\clist_set:Nn\myList{}
\cs_if_eq:NNTF\myList{}{EMPTY}{NOT empty}
\ExplSyntaxOff
\end{document}


EDIT: I found a way to do so (below) but would like you to share another approach (it's interesting to see how else it can be done):

  \clist_new:N\myList
\clist_new:N\emptyList
\clist_set:Nn\myList{} % assign "emptiness" to the list (although it had already been empty to begin with)
\cs_if_eq:NNTF\myList\emptyList{EMPTY}{NOT empty}

• \clist_if_empty:NTF. – Ulrike Fischer May 14 at 19:53
• What Ulrike said, plus the “proper” way to make a clist empty is \clist_clear:N \myList. (Deep down is basically the same, but exist for semantics). – Phelype Oleinik May 14 at 19:56

\documentclass{standalone}
\usepackage{expl3}
\begin{document}
\ExplSyntaxOn
\clist_new:N\myList
\clist_set:Nn\myList{}
\clist_if_empty:NTF \myList{ EMPTY }{ NOT EMPTY }
\ExplSyntaxOff
\end{document}


run texdoc l3clist

• How can I use AND (&&) during this \clist_if_empty check to also perform check on additional list (or on something else) in one directive? – bp2017 May 14 at 20:36
• See \bool_if:nTF or (better) e.g. \bool_lazy_or:nnTF, all in interface3. – Joseph Wright May 14 at 20:39
• @bp2017 You can always use the c-type variants, e..g \clist_new:c { myList2 }. Note though that there is no escaping the catcode and token basis of TeX. Also note that standard expl3 style would be \l_bp_mylist_clist or similar. – Joseph Wright May 14 at 20:53
• @bp2017 You want the predicates: \clist_if_empty_p:N, and so on – Joseph Wright May 14 at 20:53

The test you want is \clist_if_empty:N(TF): each data type has an appropriate test for emptiness or similar. These are defined such that they are 'aware' of the internal nature of data types (for example an empty prop is not entirely empty at the macro level).

Your \cs_if_empty:NN(TF) test is wrong as the second argument you have given is no N type (a single token), but is rather n-type (in this case, an empty token list). You could in this case do a low-level test, for example

\cs_if_eq:NN(TF) \myList \c_empty_clist { <true> } { <false> }


or by expanding the list to a 'raw' token list

\tl_if_blank:V(TF) \c_empty_clist { <true> } { <false> }

• There is a style in tikzset named myStyle, I tried to pass #1 of that style (which contains all the arguments passed to the style, supplied through a node or a path directive which calls that style with or without arguments) inside /utils/exec as \clist_set:Nn\myList{#1}. But for some reason when the style is used with no arguments (like \path node[myStyle];), the list is NOT empty (it's empty only when \path node[myStyle={}];). I'm trying to figure out why I get such result (and trying to look what #1 contains in this case, but having difficulties so far). – bp2017 May 14 at 20:15
• @bp2017 There is some 'back end' in pgf that may be responsible, but I'd be tempted just to \showtokens the input #1. – Joseph Wright May 14 at 20:18
• @bp2017: myStyle is internally replaced with mystyle=\pgfkeysnovalue. You can provide a different default value, however. See section 87.3.2 of the pgf manual. – schtandard May 15 at 7:10