6

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?

2 Answers 2

6

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.

2
  • Thanks. I do not think I'll ever be good at Latex programming because I would never have thought one needs to use a \renewcommand to clear a list.
    – Nasser
    May 19, 2016 at 8:28
  • @Nasser a list is just a macro like \newcommand\mylist{a,b,c} (but using a token less likely than , to use as the separator so you don't have to worry about the separator appearing in an item) so an empty list is just an empty macro. May 19, 2016 at 8:48
1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .