5

In the following tree, some nodes have multiple parents (which technically violates the tree definition).

Tree with multiple parent nodes

Therefore, to create the tree in the forest package, I manually added the edges from children to (additional) parents. However, the nodes are now unevenly spaced due to forest thinking that each node belongs to one parent at most.

My question is now how to evenly horizontally distribute the nodes over each level so that, for example, the bottom node {a,b,c,d} is centered.

Here is my code so far:

\documentclass[12pt]{article}
\usepackage{forest}

\begin{document}
\begin{forest}
  for tree={
    ellipse,
    draw,
    inner sep=1.5pt,
    l*=1.75
  }
  [$\O$
    [$\{a\}$,name=a
      [$\{a{,}b\}$,name=ab
        [$\{a{,}b{,}c\}$,name=abc
          [$\{a{,}b{,}c{,}d\}$,name=abcd ]
        ]
        [$\{a{,}b{,}d\}$,name=abd ]
      ]
      [$\{a{,}c\}$,name=ac
        [$\{a{,}c{,}d\}$,name=acd ]
      ] 
      [$\{a{,}d\}$,name=ad ]
    ]
    [$\{b\}$,name=b
      [$\{b{,}c\}$,name=bc
        [$\{b{,}c{,}d\}$,name=bcd ]      
      ]
      [$\{b{,}d\}$,name=bd ]
    ]
    [$\{c\}$,name=c
      [$\{c{,}d\}$,name=cd ]
    ]
    [$\{d\}$,name=d
    ]
  ]
  \draw (b) edge (ab);
  \draw (c) edge (ac);
  \draw (c) edge (bc);
  \draw (d) edge (ad);
  \draw (d) edge (bd);
  \draw (d) edge (cd);
  \draw (ac) edge (abc);
  \draw (bc) edge (abc);
  \draw (ad) edge (abd);
  \draw (bd) edge (abd);
  \draw (ad) edge (acd);
  \draw (cd) edge (acd);
  \draw (bd) edge (bcd);
  \draw (cd) edge (bcd);
  \draw (abd) edge (abcd);
  \draw (acd) edge (abcd);
  \draw (bcd) edge (abcd);
\end{forest}
\end{document}

Thanks in advance for your help!

3
  • 3
    Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem.
    – jlab
    Commented Apr 2 at 11:51
  • 3
    It looks like a Hasse diagram, is it really needed the forest environment? If not, it can be done with node placing and edges (this question, or this question can be helpful) or with the tikz-cd package, if you need something fast, but without ellipses (check quiver for this) Commented Apr 2 at 12:50
  • 1
    You can do this with forest, but it may be overkill. Forest tries hard to avoid edges crossing. (It sometimes loses track, but that's the aim.) That seems beside the point here? A matrix of math nodes might make more sense, even.
    – cfr
    Commented Apr 2 at 16:16

2 Answers 2

5

As Lorenzo and cfr pointed out, using the forest package does not seem to be the easiest approach here. Instead, I solved the problem by manually creating the Hasse diagram (thanks again to Lorenzo) similar to this question using TikZ:

Tree with evenly spaced nodes

Here is my code:

\documentclass{article}
\usepackage{tikz}

\usetikzlibrary{shapes}

\begin{document}
\begin{tikzpicture}[scale=0.7,inner sep=2]
 \node[ellipse,draw=black](0) at (-3,10) {$\O$};

 \node[ellipse,draw=black](a) at (-7.5,8) {$\{a\}$};
 \node[ellipse,draw=black](b) at (-4.5,8) {$\{b\}$};
 \node[ellipse,draw=black](c) at (-1.5,8) {$\{c\}$};
 \node[ellipse,draw=black](d) at (1.5,8) {$\{d\}$};

 \node[ellipse,draw=black](ab) at (-10.5,6) {$\{a{,}b\}$};
 \node[ellipse,draw=black](ac) at (-7.5,6) {$\{a{,}c\}$};
 \node[ellipse,draw=black](ad) at (-4.5,6) {$\{a{,}d\}$};
 \node[ellipse,draw=black](bc) at (-1.5,6) {$\{b{,}c\}$};
 \node[ellipse,draw=black](bd) at (1.5,6) {$\{b{,}d\}$};
 \node[ellipse,draw=black](cd) at (4.5,6) {$\{c{,}d\}$};

 \node[ellipse,draw=black](abc) at (-9.75,4) {$\{a{,}b{,}c\}$};
 \node[ellipse,draw=black](abd) at (-5.25,4) {$\{a{,}b{,}d\}$};
 \node[ellipse,draw=black](acd) at (-0.75,4) {$\{a{,}c{,}d\}$};
 \node[ellipse,draw=black](bcd) at (3.75,4) {$\{b{,}c{,}d\}$};

 \node[ellipse,draw=black](abcd) at (-3,2) {$\{a{,}b{,}c{,}d\}$};

 \draw (0) -- (a);
 \draw (0) -- (b);
 \draw (0) -- (c);
 \draw (0) -- (d);
 
 \draw (a) -- (ab);
 \draw (a) -- (ac);
 \draw (a) -- (ad);
 \draw (b) -- (ab);
 \draw (b) -- (bc);
 \draw (b) -- (bd);
 \draw (c) -- (ac);
 \draw (c) -- (bc);
 \draw (c) -- (cd);
 \draw (d) -- (ad);
 \draw (d) -- (bd);
 \draw (d) -- (cd);
 
 \draw (ab) -- (abc);
 \draw (ab) -- (abd);
 \draw (ac) -- (abc);
 \draw (ac) -- (acd);
 \draw (ad) -- (abd);
 \draw (ad) -- (acd);
 \draw (bc) -- (abc);
 \draw (bc) -- (bcd);
 \draw (bd) -- (abd);
 \draw (bd) -- (bcd);
 \draw (cd) -- (acd);
 \draw (cd) -- (bcd);
 
 \draw (abc) -- (abcd);
 \draw (abd) -- (abcd);
 \draw (acd) -- (abcd);
 \draw (bcd) -- (abcd);

\end{tikzpicture}
\end{document}
1
  • 2
    Suggestion: You can (and should) refactor the node style, which doesn't change. I.e. move it to the beginning, give the style a name and simplify the node options.
    – MS-SPO
    Commented Apr 2 at 17:04
5

I think a matrix of math nodes would be easier (and faster!) than forest, which tries to avoid crossing edges. For example,

\documentclass[tikz,border=10pt]{standalone}
% ateb: https://tex.stackexchange.com/a/714619/
\usetikzlibrary{matrix,shapes.geometric}
\begin{document}
\begin{tikzpicture}
  \matrix (h) [matrix of math nodes,row sep=10mm,column sep=-10pt,nodes={text width=15mm,align=center,inner xsep=0pt,ellipse,draw}]
  {
    &&&&& \O  \\
    & \{a\} && \{b\} &&&& \{c\} && \{d\}  \\
    \{a,b\} && \{a,c\} && \{a,d\} && \{b,c\} && \{b,d\} && \{c,d\} \\
    & \{a,b,c\} && \{a,b,d\} &&&& \{a,c,d\} && \{b,c,d\} \\
    &&&&& \{a,b,c,d\}\\
  };
  \foreach \j in {2,4,8,10}
  {
    \draw  (h-1-6.south) -- (h-2-\j.north);
    \draw  (h-4-\j.south) -- (h-5-6.north);
  }
  \foreach \j in {1,3,5} \draw (h-2-2.south) -- (h-3-\j.north);
  \foreach \j in {5,9,11} \draw (h-2-10.south) -- (h-3-\j.north);
  \foreach \j in {1,7,9} \draw (h-2-4.south) -- (h-3-\j.north);
  \foreach \j in {3,7,11} \draw (h-2-8.south) -- (h-3-\j.north);
  \foreach \j in {1,3,7} \draw (h-4-2.north) -- (h-3-\j.south);
  \foreach \j in {1,5,9} \draw (h-4-4.north) -- (h-3-\j.south);
  \foreach \j in {3,5,11} \draw (h-4-8.north) -- (h-3-\j.south);
  \foreach \j in {11,9,7} \draw (h-4-10.north) -- (h-3-\j.south);
\end{tikzpicture}
\end{document}

[Image omitted due to Okular bug.]

From community:

result

[Note that I forgot the ellipse and 'community' was too quick, so the image doesn't draw the ellipse, even though the code does!]

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .