[Tex/LaTex] Delete an element from a comma delimited list

loopsprogramming

LaTeX's low-level programming is poorly documented and the section on what is called control commands is even more so.

LaTeX provides the \@for macro. This works by repeatedly assigning list items to a temporary variable:

I want to define a list that will include all the greek math letters. The list is defined as follows:

\newcommand{\mathList}{\alpha,\beta,\gamma,
               \delta,\epsilon,\zeta,\theta}

To iterate over the list I have used the @for macro. For example the following explodes the list
and removes the comma.

   \@for\i:=\mathList\do{%
      \ensuremath \i \space 
    }

How can I define a macro to be able to delete the nth element of the list? I have figured out appending to the list but have not posted it for brevity. I would prefer a TeX or LaTeX solution, although I would also be curious to see how it is done in LaTeX3, so all solutions are welcome.

Minimal example for convenience below:

\documentclass[11pt]{article}
\begin{document}
\makeatletter
\let\dotlessi\i
\newcommand*{\mathList}{\alpha,\beta,\gamma,
               \delta,\epsilon,\zeta,\theta, }
\@for\i:=\mathList\do{%
  \ensuremath \i \space 
 }
\let\i\dotlessi
\makeatother
\end{document}

Best Answer

Here is one way (deleting item number 3 from the list):

\count@=0
\toks@{}
\@for\i:=\mathList\do{%
  \advance\count@ 1
  \ifnum\count@=3 \else
    \edef\0{\the\toks@\expandafter\noexpand\i,}%
    \toks@\expandafter{\0}%
  \fi
 }%
\edef\mathList{\the\toks@}
Related Question