# TikZ-tree: edge-alignment in horizontal org-chart

I'm working on some sort of org-chart/hierarchy-tree and ran into issues with the edge-alignment. As a guideline I used the family-tree-TeXample. See the TeX.sx-dicussion here.

However, the text in my nodes is a lot longer than the short names in the example. Therefore, starting the edges from the south-anchor of the criteria-nodes produces quite a bit of whitespace.

As a solution to this, I'd like to start the edges at the west-anchor of the criteria-nodes. However, edge from parent path is set to |- and thus does not produce a horizontal section before going vertical.

I tried the below of=<node>, but that didn't help either. The option I'm trying to avoid is setting a coordinate for each edge, in case I'd have to adjust something later. Spreading the tree out horizontally is unfortunately not an option either, because the attribute-count is too high.

I googled a bit to find some pictures of trees that have this edge-style:

The first tree uses an edge style like -|-.

The second one is also nice, but this anchor does not exist in TikZ-nodes.

The third is better than my current solution, but doesn't look very clean.

Here's the MWE that produces a tree as shown in the picture at the top:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{trees}
\tikzstyle{every node}=[draw=black,thick,anchor=west, minimum height=2.5em]

\begin{document}

\begin{figure}[!htb]
\resizebox{\linewidth}{!}{

\begin{tikzpicture}[
criteria/.style={text centered, text width=2cm, fill=gray!50},
attribute/.style={%
grow=down, xshift=-1cm,
text centered, text width=2cm,
edge from parent path={(\tikzparentnode.south west) |- (\tikzchildnode.west)}},
first/.style    ={level distance=8ex},
second/.style   ={level distance=16ex},
third/.style    ={level distance=24ex},
fourth/.style   ={level distance=32ex},
fifth/.style    ={level distance=40ex},
level 1/.style={sibling distance=10em}]
% Main Goal
\node[anchor=south]{SuperLongTitleWithNoMeaning}
[edge from parent fork down]

% Criteria and Attributes
child{node (crit1) [criteria] {Criteria1}
child[attribute,first]  {node {Attribute\\Number1}}
child[attribute,second] {node {Attribute2}}
child[attribute,third]  {node {Attribute3}}
child[attribute,fourth] {node {Attribute4}}
child[attribute,fifth]  {node {Another\\Attribute}}}
%
child{node [criteria] {Criteria2}
child[attribute,first]  {node {Attribute1}}
child[attribute,second] {node {Attribute2}}
child[attribute,third]  {node {Third\\Criteria}}
child[attribute,fourth] {node {Longtext-\\criteria}}}
%
child{node [criteria] {Criteria3}
child[attribute,first]  {node {Attribute\\two lines}}
child[attribute,second] {node {Attribute2}}
child[attribute,third]  {node {Attribute3}}}
%
child{node [criteria] {Criteria4}
child[attribute,first]  {node {Attribute1}}
child[attribute,second] {node {Attribute2}}}
%
child{node [criteria] {Criteria5}
child[attribute,first]  {node {First\\Attribute}}
child[attribute,second] {node {Attribute2}}
child[attribute,third]  {node {Third\\Criteria}}
child[attribute,fourth] {node {Longtext-\\criteria}}};
\end{tikzpicture}}
\caption{This is a nice tree.}
\end{figure}

\end{document}


Bonus question:

The top-node centers itself according to the total number of nodes in all levels. Therefore, it's a bit shifted toward the left. Can I tell TikZ to ignore the attribute-level for the centering? It would look nicer, if the heading would be right above the middle criteria.

-

Here's something like the second option (the node does exist).

There are only two changes to your MWE, in attribute/,style:

2. The anchor point you need can be expressed as an angle

Sorry: I pass on the bonus.

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{trees}
\tikzstyle{every node}=[draw=black,thick,anchor=west, minimum height=2.5em]

\begin{document}

\begin{figure}[!htb]
\resizebox{\linewidth}{!}{

\begin{tikzpicture}[
criteria/.style={text centered, text width=2cm, fill=gray!50},
attribute/.style={%
grow=down, xshift=0cm,
text centered, text width=2cm,
edge from parent path={(\tikzparentnode.225) |- (\tikzchildnode.west)}},
first/.style    ={level distance=8ex},
second/.style   ={level distance=16ex},
third/.style    ={level distance=24ex},
fourth/.style   ={level distance=32ex},
fifth/.style    ={level distance=40ex},
level 1/.style={sibling distance=10em}]
% Main Goal
\node[anchor=south]{SuperLongTitleWithNoMeaning}
[edge from parent fork down]

% Criteria and Attributes
child{node (crit1) [criteria] {Criteria1}
child[attribute,first]  {node {Attribute\\Number1}}
child[attribute,second] {node {Attribute2}}
child[attribute,third]  {node {Attribute3}}
child[attribute,fourth] {node {Attribute4}}
child[attribute,fifth]  {node {Another\\Attribute}}}
%
child{node [criteria] {Criteria2}
child[attribute,first]  {node {Attribute1}}
child[attribute,second] {node {Attribute2}}
child[attribute,third]  {node {Third\\Criteria}}
child[attribute,fourth] {node {Longtext-\\criteria}}}
%
child{node [criteria] {Criteria3}
child[attribute,first]  {node {Attribute\\two lines}}
child[attribute,second] {node {Attribute2}}
child[attribute,third]  {node {Attribute3}}}
%
child{node [criteria] {Criteria4}
child[attribute,first]  {node {Attribute1}}
child[attribute,second] {node {Attribute2}}}
%
child{node [criteria] {Criteria5}
child[attribute,first]  {node {First\\Attribute}}
child[attribute,second] {node {Attribute2}}
child[attribute,third]  {node {Third\\Criteria}}
child[attribute,fourth] {node {Longtext-\\criteria}}};
\end{tikzpicture}}
\caption{This is a nice tree.}
\end{figure}

\end{document}

-
Thanks, Brent! I read about the angle-setting yesterday, but never actually tried to just write a number. I like the tree a lot more now. Do you happen to have an idea about the centering of the top-node without rearranging the branches? –  dhst Mar 2 '12 at 10:04
@Daniel: No, sorry, I had a couple of private monkey-fingerpoken tries at it, but to no avail. –  Brent.Longborough Mar 2 '12 at 10:17
Too bad. Seems like xshift has no effect on the top-node. –  dhst Mar 2 '12 at 11:05
@Daniel: First thing I tried... Welcome to fingerpokenwelt. –  Brent.Longborough Mar 2 '12 at 11:07
Fingerpoken-magic: xshift-ing all the criteria-nodes by a bit less than half the sibling-distance (10em in the MWE) centers the top-node. From there on it comes down to playing around with the third decimal place. With my current settings (different than the MWE) xshift=-4.775em looks flawless. –  dhst Mar 2 '12 at 11:34