2

This is a follow-up question to this one: Making Tikz automatic node placement work

Especially to the post of Mark Wibrow (second one).

His solution works with 16 nodes, but I'm looking for a solution that does the same but with 32 nodes, like this:

\foreach \bytes [count=\y from 0] in {
   {null},
   {a,b,c,d,e}, 
   {ab,ac,ad,ae,bc,bd,be,cd,ce,de}, 
   {abc,abd,abe,acd,ace,ade,bcd,bce,bde,cde}, 
   {abcd,abce,abde,acde,bcde},
   {abcde}}

Can you help me editing the following:

  1. the

    at ({\x+abs(2-\y)+(mod(\y, 4)==0)/2}, -\y)
    

    that all 32 nodes are symmetrical centered.

    The solution above only works for 16 nodes!

  2. that my nodes a -> ab, a -> ac, a -> ad and so on are connected.

    The solution

    \foreach \x in {0,...,15}{
        \foreach \y in {\x,...,15}{
            \ifnum\x=\y
            \else
                \process{\x}{\y}
                \ifnum\pgfmathresult=1
                    \draw [<->] (\x) -- (\y);
                \fi
            \fi     
        }
    }
    

    doesn't work.

I wan't to draw graph to look like this (taken from [1], p. 8)

enter image description here

mrbela
  • 1,129
  • Can't anybody solve my questions? – mrbela Nov 19 '13 at 22:24
  • Your first problem is easily solved as the numbers of nodes in each row are the binomial coefficients 1, 5, 10, 10 and 5, and 1 which can be derived using n!/(k!*(n-k)!). So the coordinates of the nodes are ({-((5!/\y!/(5-\y)!-1)/2+\x},-\y), or something like that. – Mark Wibrow Nov 20 '13 at 06:49

1 Answers1

5

The position problem can be solved as mentioned in the comment above by calculating the binomial coefficients, like this:

\documentclass[border=0.125cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\begin{document}

\begin{tikzpicture}[x=2cm,y=2cm]

\foreach \l [count=\y from 0] in {{null},
   {a,b,c,d,e}, 
   {ab,ac,ad,ae,bc,bd,be,cd,ce,de}, 
   {abc,abd,abe,acd,ace,ade,bcd,bce,bde,cde}, 
   {abcd,abce,abde,acde,bcde},
   {abcde}}
   \foreach \m [count=\x from 0] in \l
     \node [ellipse, draw, anchor=base, minimum width=1cm, minimum height=0.75cm] 
       (\m) at ({-(5!/\y!/(5-\y)!)/2+\x},-\y) {\m};

\end{tikzpicture}

\end{document}

enter image description here

And this will do the connections.

\documentclass[border=0.125cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\newcount\incount
\def\in#1#2{\incount=0\relax\edef\marshal{\noexpand\In#1@@;#2@@;}\marshal}

\def\In#1#2;#3#4;{%
  \ifx#1@%   
    \ifx#3@%
      \incount=\ifnum0>\incount-\fi\incount%
      \let\next=\relax%
    \else%
      \advance\incount by-1%
      \def\next{\In#1#2;#4;}%
    \fi%
  \else%
    \def\test##1#1##2##3?{\ifx##2*\else\advance\incount by1\fi}%
    \test#3#4;#1**?%
    \def\next{\In#2;#3#4;}%
  \fi%
  \next%      
}
\begin{document}

\begin{tikzpicture}[declare function={nchoosek(\n,\k)=\n!/(\k!*(\n-\k)!);}, x=2cm,y=3cm]

\foreach \R [count=\y from 0, evaluate={\s=nchoosek(5,\y);}, remember=\R as \r] in {{null},
   {a,b,c,d,e}, 
   {ab,ac,ad,ae,bc,bd,be,cd,ce,de}, 
   {abc,abd,abe,acd,ace,ade,bcd,bce,bde,cde}, 
   {abcd,abce,abde,acde,bcde},
   {abcde}}
   \foreach \C [count=\x from 0] in \R {   
     \node [ellipse, draw, anchor=base, minimum width=1cm, minimum height=0.75cm] 
       (\C) at (-\s/2+\x,-\y+1) {\C};       
      \foreach \c in \r {
        \in{\c}{\C}
        \ifnum\incount=1
          \draw (\c.south) -- (\C.north);
        \fi
      }
  }

\end{tikzpicture}

\end{document}

enter image description here

Mark Wibrow
  • 70,437
  • Hey Mark! First of all: Thank you very much for your help! If I want to insert your code in my file I get the error: "! Missing $ inserted". The line is the end of the \ifnum-code. Do you understand the error or is it in my document?! – mrbela Nov 20 '13 at 11:02
  • @user2438518 there was a missing \fi and an extraneous \ifnum which I've now corrected. – Mark Wibrow Nov 20 '13 at 11:11
  • Now it works, thanks! For the others: Mark defined \in and \In. That made problems with my \in (element of) in math-mode. So I renamed them.. – mrbela Nov 20 '13 at 11:55
  • One last stupid question: Now (and thats the last what I want to do ;) ) I want to color each node. A subset of them in green color and the other in red.. How can I define anything like "is the actual node in the set one, then draw him green. if not, draw him red". THANKS!! – mrbela Nov 20 '13 at 11:56
  • @user2438518 add \C/.try to the node options. Then you can define styles, for example abc/.style={fill=blue!20}. – Mark Wibrow Nov 20 '13 at 12:23
  • yeah, I saw this solution in one of your prior posts. But how can I define one style for a set of nodes and not for each node an own style. – mrbela Nov 20 '13 at 12:41
  • @user2438518 So use a .code key. For example, add set={\C} to the node options and define the key beforehand set/.code={...} which then takes the current node text as its argument. Then replace ... with the test you want to do and set the keys inside using \tikzset, for example \tikzset{set/.code={\in{#1}{abc}\ifnum\incount=1\tikzset{fill=blue!20}\fi}}. – Mark Wibrow Nov 20 '13 at 13:23