The following was motivated by the answer by David Carlisle. Note that \sets{{a,{b,c},{{c,d},e}}}
does not work with this (accepted) answer (neither would \setsb
, \setsc
, nor setsd
).
CAVEAT: any input mistake (non matching braces) makes the TeX run end in an error!
CAVEAT2: only accepted elements here are letters (lowercase and uppercase) and \emptyset
.
Please note how {}
are converted into \emptyset
(and how spaces in the input are ignored, as is expected anyhow in math mode).
The macro has to be used in math mode because of its use of \emptyset
.
Edit1: improvement to allow spaces in the input ... (particularly, a space just before a closing brace was a disaster before the edit).
Nota Bene : this code is a bit to play with TeX
's mechanism of grouping and local scope and recursive macros. It could be made more general. In a way, it's like telling TeX
to show us it understands sets!
Edit2: one last update, just to fix the problem of { }
giving {\emptyset}
rather than just \emptyset
.
\documentclass{article}
\def\printset #1{\ifx\relax#1\relax\emptyset\else
\screensp #1\relax\emptyset
{\{\setaux #1\relax,\}}\fi}
\makeatletter
\def\screensp #1\relax{\ifx#1\@sptoken\relax\expandafter\@firstoftwo\else
\expandafter\@secondoftwo\fi}
\makeatother
\def\setaux #1#2,#3{\ifx\relax#1\relax\emptyset\else
\ifx#1\emptyset\emptyset\else
\ifcat#1a#1\else
{\printset {#1}}\fi\fi\fi
\ifx\relax#3\relax,\expandafter\setaux\else
\ifx#3\}\else,\expandafter\expandafter\expandafter
\setaux\fi\fi {#3}}
\begin{document}\pagestyle{empty}
\[\printset{}\]
\[\printset{{}}\]
\[\printset{{},{{}}}\]
\[\printset{{},{{}},{{},{{}}}}\]
\[\printset{{a,{b,c},\emptyset},{{c,d},e}}\]
\[\printset{{{a,b},{\emptyset,U,{}, {V,W}, {}},{{}},{d,{e,{U,{V,W}},\emptyset,f}}}}\]
\[\printset{a , b , { c , { d , { u , { v , w } } }} , { e } }\]
\[\printset{ }\]
\[\printset{ { } }\]
\end{document}
\
and{}
then you will be incompatible with virtually everything. the\
in\{
is no different to the\
in\section
or\langle
It is just how LaTeX is.\verb
which also changes catcodes, in particular will not work in the argument of another command. what you could do is have a command say\sets
that braces every argument in a list so\sets{a,b,c}
makes\{a\},\{b\},\{c\}
\sets{a,{b,c}}
makes\{a\},\{b,c\}
That requires no catcode tricks