[Tex/LaTex] The way to list subsets of the set {1, 2, 3, 4, 5} with forest or tree package

foresttikz-treestrees

I want to list the number of subsets of the set {1, 2, 3, 4, 5}. I tried with forest package.

    \documentclass[border=3mm]{standalone}
    \usepackage{amsmath}
    \usepackage{forest}
    \begin{document}
    \begin{forest}
    for tree={edge={->}}
    [\{1;\,2;\,3;\,4;\,5\}[1[2[3[4[5]][5]][4[5]][5]][3[4[5]][5]][4[5]][5]][2[3[4[5]][5]][4[5]][5]][3[4[5]][5]][4[5]][5]]
    \end{forest}
\end{document}

enter image description here

In this diagram, have not the empty set. What is the better way to list?

Best Answer

Here's another method, which I eventually figured out. Instead of using pure TikZ, it combines Forest with expl3 functions to construct the contents of all nodes bar the root.

subsets initiates the style. n elements=<integer> should be used to specify the number of elements in the root subset. The style will then construct the root subset, add the descendants and populate them appropriately. That is,

\begin{forest}
  subsets,
  [, n elements=3]
\end{forest}

will produce the tree for the root set containing 3 elements

3 element set

and

\begin{forest}
  subsets,
  [, n elements=5]
\end{forest}

that for the set containing 5.

5 element set

Complete code:

\documentclass[border=10pt]{standalone}
\usepackage{xparse,forest}
\ExplSyntaxOn
\clist_new:N \l_subsets_subset_clist
\cs_new_protected_nopar:Nn \subsets_construct_subset:nn
{
  \clist_set:Nn \l_subsets_subset_clist { #2 }
  \clist_clear:N \l_tmpa_clist
  \clist_map_inline:nn { #1 }
  {
    \int_zero:N \l_tmpa_int
    \clist_set_eq:NN \l_tmpa_clist \l_subsets_subset_clist
    \clist_clear:N \l_subsets_subset_clist
    \clist_map_inline:Nn \l_tmpa_clist
    {
      \int_incr:N \l_tmpa_int
      \int_compare:nF { ##1 = \l_tmpa_int }
      {
        \clist_put_right:Nn \l_subsets_subset_clist { ####1 }
      }
    }
  }
  \clist_if_empty:NTF \l_subsets_subset_clist
  {
    \emptyset
  }{
    \{ \clist_use:Nn \l_subsets_subset_clist { , } \}
  }
}
\NewDocumentCommand \constructsubset { m m }
{
  \group_begin:
  \subsets_construct_subset:nn { #1 } { #2 }
  \group_end:
}
\ExplSyntaxOff
\forestset{
  declare toks={subset chooser}{},
  declare toks register={root set},
  root set={},
  n elements/.style={
    tempcounta'=0,
    if={>nn={0}{#1}}{content=\emptyset}{
      until={
        >Rn={tempcounta}{#1}%
      }{
        tempcounta'+=1,
        if tempcounta=1{root set=1}{root set+={,}, root set+/.register=tempcounta},
      },
      delay={
        tempcounta'=0,
        while={>Rn<{tempcounta}{#1}}{
          where n children=0{
            tempcountb'=#1,
            tempcountb-/.register=tempcounta,
            repeat/.process={Rw{tempcountb}{##1}{append={[]}}},
          }{},
          tempcounta'+=1,
          do dynamics,
        },
      },
    },
  },
  subsets/.style={
    before typesetting nodes={
      where level=0{content=\{,content+/.register=root set,content+=\}}{
        temptoksa/.option=n,
        for nodewalk={until={>On={level}{0}}{parent}}{+temptoksa={,},+temptoksa/.option=n,},
        subset chooser/.register=temptoksa,
        TeX/.process={ORw2{subset chooser}{root set}{\xdef\tempa{\constructsubset{##1}{##2}}}},
        content/.expanded=\tempa,
      },
      for tree={
        math content,
        edge+=<-,
      },
    },
  },
}
\begin{document}

\begin{forest}
  subsets,
  [, n elements=3]
\end{forest}
\begin{forest}
  subsets,
  [, n elements=5]
\end{forest}
\end{document}
Related Question