6

I am trying to draw a simple vertical tree to represent a hierarchical partition of a graph in communities. I have written some code to draw it in tikz, but I am not very satisfied of the result. My code is:

\begin{tikzpicture}[shorten >=1pt, auto, node distance=3cm, ultra thick,
        node_style/.style={font=\sffamily\Large\bfseries,minimum size=0.7cm},
        edge_style/.style={draw=blue, ultra thick},
        community_label_style/.style=    {font=\sffamily\Large\bfseries,minimum size=0.7cm,text height=1.5ex,text depth=.25ex,}]
\node[community_label_style] (v15) at (0,-4) {\emph{a}};
\node[community_label_style] (v16) at (1,-4) {b};
\node[community_label_style] (v17) at (3,-4) {c};
\node[community_label_style] (v18) at (4,-4) {d};
\node[community_label_style] (v19) at (8,-4) {e};
\node[community_label_style] (v20) at (9,-4) {f};
\node[community_label_style] (v21) at (11,-4) {g};
\node[community_label_style] (v22) at (12,-4) {h};
\node (v1) at (6,1) {};
\node[community_label_style] (v13) at (1,-2) {$C2$};
\node[community_label_style] (v7) at (9,-2) {$C6$};
\node (v2) at (6,0) {};
\draw  (v1) edge (v2);
\node[community_label_style] (v3) at (2,0) {$C1_2$};
\node[community_label_style] (v4) at (10,0) {$C2_2$};
\node[community_label_style] (v11) at (4,-2) {$C4$};
\draw  (v2) edge (v3);
\draw  (v2) edge (v4);
\node (v5) at (2,-2) {};
\node[community_label_style] (v12) at (3,-2) {$C3$};
\node[community_label_style] (v14) at (0,-2) {$C1$};
\node (v6) at (10,-2) {};
\node[community_label_style] (v9) at (11,-2) {$C7$};
\node[community_label_style] (v10) at (12,-2) {$C8$};
\node[community_label_style] (v8) at (8,-2) {$C5$};
\draw  (v3) edge (v5);
\draw  (v4) edge (v6);
\draw  (v6) edge (v7);
\draw  (v7) edge (v8);
\draw  (v6) edge (v9);
\draw  (v9) edge (v10);
\draw  (v11) edge (v12);
\draw  (v12) edge (v5);
\draw  (v5) edge (v13);
\draw  (v13) edge (v14);
\draw  (v15) edge (v14);
\draw  (v16) edge (v13);
\draw  (v17) edge (v12);
\draw  (v18) edge (v11);
\draw  (v19) edge (v8);
\draw  (v20) edge (v7);
\draw  (v21) edge (v9);
\draw  (v22) edge (v10);
\draw  (v13) edge (v12);
\draw  (v7) edge (v9);
\draw  (v4) edge (v3);
\end{tikzpicture}

The result is:

enter image description here

I don't like that lines are separed by white dots. I would like to draw a more elegant representation. Is it possible? How could I improve my drawing?

Thank you for your help.

1
  • When you say that the lines are separated by white dots, do you mean they are not completely connected? If yes, then simply add [shorten >=-4pt] to the edges you would like to be longer, for example \draw[shorten >=-4pt] (v3) edge (v5);. Also, how exactly would you like to improve your drawing, specifically? Apr 20, 2014 at 0:26

4 Answers 4

7

May I suggest you the powerful forest package? It is built upon PGF/TikZ, so you can still use the power of TikZ if required, and offers a lot of interesting features to build trees. Compare the economy in the code:

\documentclass{article}
\usepackage{forest}

\begin{document}

\begin{forest} 
where n children=0{font=\sffamily}{},
for tree={
  edge path={
    \noexpand\path[\forestoption{edge}]
      (!u.parent anchor) -- +(0,-13pt) -|   
      (.child anchor)\forestoption{edge label};
  },
  l sep=10pt,
} 
[\mbox{}
  [$C1_2$
    [$C1$ [a]]
    [$C2$ [b]]
    [$C3$ [c]]
    [$C4$ [d]]
  ]
  [$C2_2$
    [$C5$ [e]]
    [$C6$ [f]]
    [$C7$ [g]]
    [$C8$ [h]]
  ]
]
\end{forest}

\end{document}

enter image description here

5
  • Could you possibly explain the specification of edge path? I don't understand how the explanation on page 33 of the manual relates to the code/output you have. Note: if this is too large or annoying of a question, just say so. I seem to have a hard time with forest's documentation for some reason so I'm not sure whether I'm just asking something too big/idiotic to be worth answering.
    – cfr
    Apr 20, 2014 at 2:27
  • @cfr No problem, but perhaps a comment's space is too short for an explanation; the specification I gave is basically \noexpand\path[\forestoption{edge}] (!u.parent anchor) -- +(0,-13pt) -| (.child anchor)\forestoption{edge label}; this uses the my specification uses the lower level commands \forestove which sets the options for the current node. In fact, I am considering that it would be "more natural" to use the code with no low level commands? Apr 20, 2014 at 3:03
  • Maybe I should ask it as a follow up question?
    – cfr
    Apr 20, 2014 at 3:31
  • @cfr Sounds like a good idea. Apr 20, 2014 at 3:32
  • Done! When you have a minute, perhaps you could look at tex.stackexchange.com/q/172498/39222? Thanks for your patience...
    – cfr
    Apr 20, 2014 at 13:50
5

Another alternative -- Use of tikz tree, achieving the same effect.

enter image description here

Code

\documentclass[12pt]{article}
\usepackage{graphicx,xcolor}
\usepackage[margin=0.5cm]{geometry}
%\usepackage[landscape,paper=ansibpaper]{geometry}
\usepackage{lscape} %for landscape 
\usepackage{tikz}
\usetikzlibrary{shapes,calc,positioning,arrows,mindmap,matrix}

\begin{document}
\tikzset{
    node_style/.style={font=\sffamily\Large\bfseries,minimum size=0.7cm},
    edge_style/.style={draw=blue, ultra thick},
    community_label_style/.style= {font=\sffamily\Large\bfseries,minimum size=0.7cm,text height=1.5ex,text depth=.25ex,},
    edge from parent fork down,
    level/.style={sibling distance = 6cm, level distance =2cm},
    level 2/.style={sibling distance=1.5cm},
}
\begin{tikzpicture}[
-, auto, node distance=3cm, ultra thick,
]
\node[]{}
 child[] {node[community_label_style] (v3)  {$C1_2$}
     child[]{node[community_label_style] (v14) {$C1$}
            child []{node[community_label_style] (v15)  {\emph{a}}}}
     child[]{node[community_label_style] (v13) {$C2$}
            child []{node[community_label_style] (v15)  {b}}}
     child[]{node[community_label_style] (v12) {$C3$}
            child []{node[community_label_style] (v15)  {c}}}
     child[]{node[community_label_style] (v11) {$C4$}
            child []{node[community_label_style] (v15)  {d}}}
     }
 child[] {node[community_label_style] (v4)  {$C2_2$}
     child[]{node[community_label_style] (v14) {$C5$}
            child []{node[community_label_style] (v15)  {e}}}
     child[]{node[community_label_style] (v13) {$C6$}
            child []{node[community_label_style] (v15)  {f}}}
     child[]{node[community_label_style] (v12) {$C7$}
            child []{node[community_label_style] (v15)  {g}}}
     child[]{node[community_label_style] (v11) {$C8$}
            child []{node[community_label_style] (v15)  {h}}}
};
\end{tikzpicture}

\end{document}
3

Since I cannot get to grips with forest, I would probably use tikz-qtree instead:

\documentclass{standalone}
\usepackage{tikz-qtree}

\begin{document}

\begin{tikzpicture}
  \tikzset{edge from parent/.style={draw, edge from parent path={(\tikzparentnode.south) -- +(0,-12pt) -| (\tikzchildnode)}}}
  \tikzset{every leaf node/.style={font=\sffamily}}
  \Tree
    [.{}
      [.$C1$
        [.$C1$ a
        ]
        [.$C2$ b
        ]
        [.$C3$ c
        ]
        [.$C4$ d
        ]
      ]
      [.$C2$
        [.$C5$ e
        ]
        [.$C6$ f
        ]
        [.$C7$ g
        ]
        [.$C8$ h
        ]
      ]
    ]
\end{tikzpicture}

\end{document}

Forking tree with <code>tikz-qtree</code>

3

Here is another alternative using the genealogytree package:

enter image description here

\documentclass{article}
\usepackage[all]{genealogytree}

\begin{document}

\begin{genealogypicture}[
  processing=tcbox*,           % draw nodes with tcolorbox
  node size from=6mm to 5cm,   % width of nodes
  level size=6mm,              % height of nodes
  level distance=8mm,          % generation distance
  child distance=4mm,further distance=4mm,%  horizontal distances
  box={blanker,before upper=\strut,fontupper=\sffamily\Large\bfseries},
  edges={foreground={black,ultra thick},no background},
]
child{ g{}
  child{ g{$C1_2$}
    child{ g{$C1$} c{a} }
    child{ g{$C2$} c{b} }
    child{ g{$C3$} c{d} }
    child{ g{$C4$} c{d} }
  }
  child{ g{$C2_2$}
    child{ g{$C5$} c{e} }
    child{ g{$C6$} c{f} }
    child{ g{$C7$} c{g} }
    child{ g{$C8$} c{h} }
  }
}
\end{genealogypicture}

\end{document}

You must log in to answer this question.

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