I'm trying to typeset an OCaml like list and would like to define a command mylist
for it. The expected result is something like below:
\mylist{} --> []
\mylist{aa} --> [aa]
\mylist{aa,bb} --> [aa;bb]
\mylist{aa,bb,cc} --> [aa;bb;cc]
I expect that ,
is always the typed separator (in LaTeX) but is displayed as another sign (here it is ;
) in the pdf document (it's acceptable to hardcode the displayed one in mylist
definition).
After searching for a while I found this answer and changed it a bit.
\newcounter{listcount}\newcounter{totalcount}%
\newcommand{\mylist}[1]{%
\setcounter{totalcount}{0}% Reset total count
\renewcommand*{\do}[1]{\stepcounter{totalcount}}% Reconfigure count
\docsvlist{#1}% Count number of items
\setcounter{listcount}{0}% Reset current item count
\renewcommand*{\do}[1]{% Reconfigure item \do
\stepcounter{listcount}% Next item
##1;\ifnum\value{listcount}<\value{totalcount}\,\fi% Print item
}
\left[\docsvlist{#1}\right]% Process list
}
The problem is that when the list contains >1 elements, there is a trailing ;
.
\mylist{aa,bb} --> [aa;bb;]
Intuitively there is a need to test whether the element is the last one. However I seem lost in the macro definition w.r.t. listcount
and the number of commas.
I also found a use of forcsvlist
but the problem is similar. There seems a solution by using other packages, but a bit overkill so I tend not to use that.
\listadd
etc. in earlier times and switched to\seq
lists fromexpl3
-- it's much easier and not really overkill ;-)expl3
. Can you elaborate that?