Using \docsvlist on a generated list with newcommand

Good evening,

I tried to adapt an example code in etoolbox and could not make it work. An example is most probably easier to understand :

while

\begin{itemize}
\renewcommand*{\do}[1]{\item #1}
\docsvlist{item1, item2, item3}
\end{itemize}


given in the documentation works just fine, I cannot understand why this

\newcommand{\test}{item1, item2, item3}

\begin{itemize}
\renewcommand*{\do}[1]{\item #1}
\docsvlist{\test}
\end{itemize}


will not process the list as 3 items but just one.

I need it in a more complex situation but my basic problem is here. (The \test is defined somewhere else in my case but I do not think it is relevant).

Sorry if my English and/or LaTeX is a bit shaky, I am not a native in either...

• Welcome to TeX.SX! You need to expand test first. – TeXnician Aug 25 '17 at 16:04

\test is not expanded, i.e. it is not seen as a csv - list with three items but as a single entity.

Solution: Use \expandafter\docsvlist\expandafter{\test}.

The first \expandafter tells TeX to look after \docsvlist, there must be another \expandafter in order to look after the { and then expand \test.

\documentclass{article}

\usepackage{etoolbox}

\newcommand{\test}{item1,item2,item3}

\begin{document}
\begin{itemize}
\renewcommand*{\do}[1]{\item #1}
\expandafter\docsvlist\expandafter{\test}
\end{itemize}
\end{document}


This uses expl3 features by storing the list into a \clist variable and 'preventing' expansion issues:

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn

\clist_new:N \l_sandrine_item_clist
\NewDocumentCommand{\SetMyList}{+m}{
\clist_set:Nn \l_sandrine_item_clist {#1}
}

\NewDocumentCommand{\listmapper}{}{%
\clist_map_inline:Nn \l_sandrine_item_clist {\item ##1}
}

\ExplSyntaxOff
\begin{document}
\SetMyList{item 1,item 2,item 3}

\begin{itemize}
\listmapper
\end{itemize}

\end{document}