josephwright / etoolbox

Tool-box for LaTeX programmers using e-TeX
LaTeX Project Public License v1.3c
41 stars 7 forks source link

Delete items from lists #10

Closed josephwright closed 7 years ago

josephwright commented 7 years ago

From an e-mail:

I would appreciate it very much if for internal lists the possibility exists, not only attach elements, but also to delete them again. I tried to develop a macro \listdel which does the job as intended, see the mwe below. Is there the chance that you would supplement this functionality to etoolbox?

The example:

documentclass{minimal}
\usepackage{etoolbox}

\makeatletter
\def\etb@catcodes{\do\&\do\|\do\:\do\-\do\=\do\<\do\>}
\def\do#1{\catcode\number`#1=\the\catcode`#1\relax}
\edef\etb@catcodes{\etb@catcodes}
\let\do\noexpand
\catcode`\&=3
\catcode`\|=3
\@makeother\:
\@makeother\-
\@makeother\=
\@makeother\<
\@makeother\>

\newrobustcmd{\listdel}[2]{%
  \ifinlist{#2}{#1}{%
    \begingroup
    \def\etb@tempa##1|#2|##2&{\endgroup
      \expandafter\def\expandafter#1\expandafter{\@gobble##1|##2}%
    }%
    \expandafter\etb@tempa\expandafter|#1&%
  }{}
}
\etb@catcodes\undef\etb@catcodes

\makeatother

\begin{document}
\def\foo{}
\listadd\foo{aaa}
\listadd\foo{bbb}
\listadd\foo{ccc}

\meaning\foo

\begingroup
\listdel\foo{aaa}
\meaning\foo
\endgroup

\begingroup
\listdel\foo{bbb}
\meaning\foo
\endgroup

\begingroup
\listdel\foo{ccc}
\meaning\foo
\endgroup

\begingroup
\listdel\foo{xxx}
\meaning\foo
\endgroup

\end{document}
mrpiggi commented 4 years ago

Would it be possible to provide commands