# Unexpected Results from \dolistloop

Given \def\CSVList{a,b,c}, using \dolistloop{\CSVList} does not seem to process the \CSVList as expected:

Perhaps I am not understanding how \dolistloop is supposed to work, but this example is pretty much straight from the documentation:

## Question:

• How can I use \dolistloop to get the desired results?

## Code:

\documentclass{article}
\usepackage{etoolbox}

\def\CSVList{a,b,c}%

\begin{document}
\par\noindent
\verb|\dolistloop| results:
\begin{itemize}
\renewcommand*{\do}[1]{\item #1.}%
\dolistloop{\CSVList}%
\end{itemize}

\par\noindent
Expected results:
\begin{itemize}
\item a.
\item b.
\item c.
\end{itemize}
\end{document}


I do not know anything about the second part of the question, but the first one can be answered by switching to the (unfortunately rather clumsy IMHO) procedure of building up the list that is described in the manual. I am not an expert on this, and do almost all my lists with \pgffor (which of course also has drawbacks), so I will be happy to delete my answer if a real answer by shows up. Anyway, this is the procedure that works.

\documentclass{article}
\usepackage{etoolbox}

\newcommand{\CSVList}{}

\begin{document}
\par\noindent
\verb|\dolistloop| results:
\begin{itemize}
\renewcommand*{\do}[1]{\item #1.}%
\dolistloop{\CSVList}%
\end{itemize}

\par\noindent
Expected results:
\begin{itemize}
\item a.
\item b.
\item c.
\end{itemize}
\end{document}


• The initialisation procedure could be shortened to \newcommand{\CSVList}{} \forcsvlist{\listadd\CSVList}{a,b,c}. Whether that is practical for the intended use case is another question... – moewe Sep 13 '18 at 6:37

etoolbox has two concepts of lists: (1) Internal lists built by \listadd and friends and iterated over with \dolistloop, \forlistloop and friends. And (2) CSV lists that have no building commands and are iterated over with \docsvlist and \forcsvlist. Usually when the etoolbox manual talks about lists, the internal lists are meant.

marmot's answer shows how to use internal lists (my preferred solution here), so this answer will show how to use CSV lists.

\docsvlist\CSVList does not work due to expansion issues, so you need \expandafter\docsvlist\expandafter{\CSVList}.

\documentclass{article}
\usepackage{etoolbox}

\def\CSVList{a,b,c}%

\begin{document}
\par\noindent
\verb|\docsvlist| results:
\begin{itemize}
\renewcommand*{\do}[1]{\item #1.}%
\expandafter\docsvlist\expandafter{\CSVList}%
\end{itemize}

\par\noindent
Expected results:
\begin{itemize}
\item a.
\item b.
\item c.
\end{itemize}
\end{document}


An etoolbox list is not a comma separated list. However, it's not difficult to input a comma separated list and transform it into an etoolbox list:

\documentclass{article}
\usepackage{etoolbox}

\newcommand{\defineetoolboxlist}[2]{%
\docsvlist{#2}%
}
\defineetoolboxlist\CSVList{a,b,c}

\begin{document}

\noindent
\verb|\dolistloop| results:
\begin{itemize}
\renewcommand*{\do}[1]{\item #1.}
\dolistloop{\CSVList}
\end{itemize}

\noindent
Expected results:
\begin{itemize}
\item a.
\item b.
\item c.
\end{itemize}

\end{document}


• Or \forcsvlist{\listadd\CSVList}{a,b,c} – moewe Sep 13 '18 at 9:50
• @moewe Thanks, but this heavily depends on the implementation of \forcsvlist. – egreg Sep 13 '18 at 9:54
• But doesn't everything at some point? ;-) This code is used in the etoolbox manual (p. 29), so I guess it should be fine and is intended to work. – moewe Sep 13 '18 at 9:55
• @moewe Perhaps. However, the method in tex.stackexchange.com/a/450642/4427 is much neater, in my opinion. For instance, \addtolist{CSVList}{a,b,c} and \addtolist{CSVList}{ a, b ,c} are completely equivalent, whereas etoolbox is sensitive to spaces. – egreg Sep 13 '18 at 10:00