How to clear an etoolbox list created using listadd?

by Nasser   Last Updated May 19, 2016 07:51 AM

This must be trivial, but after 30 minutes reading the manual I can't figure it out (I am a newbie in Latex programming). I see no API for it.

I simply want to clear a list created using etoolbox so I can reuse it again. Now when reusing it, it appends to it. here is MWE

\documentclass[10pt]{book}
\usepackage{etoolbox}    
\begin{document}
\forcsvlist{\listadd\mylist}{ 1,2,3 } %make up a list
\begin{enumerate}
  \renewcommand*{\do}[1]{\item[] #1}
  \dolistloop{\mylist}
\end{enumerate}

% what to do now to clear it? If I do the following, it appends!

\forcsvlist{\listadd\mylist}{ 5,6,7 }
\begin{enumerate}
  \renewcommand*{\do}[1]{\item[] #1}
  \dolistloop{\mylist}
\end{enumerate}
\end{document}

Mathematica graphics

I think I need to reset a counter or such, but I have no idea which counter or how to tell etoolbox to clear mylist. In Matlab, I would do A=[] for example to clear an array, anything similar in etoolbox?

Tags : etoolbox


Answers 2


You just need

\renewcommand\mylist{}

Or (perhaps more common) do each processing loop in a group or environment then the list will automatically be reset at the end of each group as you are using local definitions.

David Carlisle
David Carlisle
May 19, 2016 08:17 AM

Lists are just macros, so you can simply redefine the macro to be empty, as told by David.

You may enjoy a more flexible list management set of functions:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\listset}{mm}
 {
  \clist_clear_new:c { l_nasser_list_#1_clist }
  \clist_set:cn { l_nasser_list_#1_clist } { #2 }
 }
\NewDocumentCommand{\listapp}{mm}
 {% the list must already exist!
  \clist_put_right:cn { l_nasser_list_#1_clist } { #2 }
 }
\NewDocumentCommand{\listprocess}{mm}
 {
  \clist_map_inline:cn { l_nasser_list_#1_clist } { #2 }
 }
\ExplSyntaxOff

\begin{document}

Make up a list:
\listset{mylist}{ 1,2,3 } %make up a list
\begin{enumerate}
\listprocess{mylist}{\item[] #1}
\end{enumerate}

Append to the previous list:
\listapp{mylist}{ 5,6,7 }
\begin{enumerate}
\listprocess{mylist}{\item[] #1}
\end{enumerate}

Set anew the list:
\listset{mylist}{a, b, c}
\begin{enumerate}
\listprocess{mylist}{\item[] #1}
\end{enumerate}

\end{document}

enter image description here

egreg
egreg
May 19, 2016 12:18 PM

Related Questions


REVTex-like affiliation& author command

Updated March 20, 2016 08:09 AM

Why won't my boolean work correctly?

Updated July 08, 2016 08:01 AM

Less-than string comparison like \ifstrequal

Updated June 30, 2017 15:23 PM

Using \docsvlist on a generated list with newcommand

Updated August 25, 2017 16:23 PM

question of \ifstrequal in etoolbox

Updated September 16, 2017 21:23 PM