tags:

views:

99

answers:

2

Hi,

Consider the following straightforward implementation of a list in latex:

\newcommand{\add@to@list}[2]{%
  \ifx#2\@empty%
    \xdef#2{#1}%
  \else%
    \xdef#2{#2,#1}%
  \fi%
}%

I wonder if there is a simple way to implement a set (list with no repeated elements) ?

A: 

Try taking a look at the l3clist module in the expl3 bundle. It provides a basic programming interface to comma-separated lists.


Now that I'm back on a real machine, here's an example:

\documentclass{article}
\usepackage{expl3}
\begin{document}
\ExplSyntaxOn
\clist_new:N \l_my_clist
\clist_put_right:Nn \l_my_clist {hello}
\clist_put_right:Nn \l_my_clist {\unknown}
\clist_put_right:Nn \l_my_clist {hello}
\clist_remove_duplicates:N \l_my_clist
\clist_show:N \l_my_clist
\ExplSyntaxOff
\end{document}
Will Robertson
Yes there is a a clist in expl3. This package is still quite unstable, so I would prefer to have an implementation which does not depend on it. Also, I don't understand how l3clist works.
Helltone
No, it is not unstable: the team have said that the expl3 stuff on CTAN can be relied upon. It works exactly the same way any other TeX programming does, just with a lot of support stuff thought through very carefully and read "out of the box".
Joseph Wright
+1  A: 

This seems to work:

\newcommand{\add@to@set}[2]{%
   \ifx#2\@empty%
      \xdef#2{#1}%
   \else%
      \@expandtwoargs\@removeelement{#1}{#2}{#2}%
      \xdef#2{#2,#1}%
   \fi%
}%
Helltone
Add, the good old "\@expandtwoargs\@removeelement": a horrible syntax, with no apparent reason for needing three arguments (I can't find a place where it's used other than to remove duplicates from a list which is not re-named).
Joseph Wright
This is fine for your needs, I'm guessing, but will break with any fragile contents in your list. The implementation in l3clist is robust.
Will Robertson