3

Consider the MWE:

\documentclass[landscape]{article}

\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\begin{document}

\tikzset{
  treenode/.style = {align=center, inner sep=2pt, rounded corners = 2pt, minimum width = 2cm, text centered, font=\sffamily},
  block/.style = {treenode, rectangle, white, font=\sffamily\bfseries, draw=black, fill=black},
  phantom/.style = {}
}

\begin{tikzpicture}[->,>=stealth',level/.style={sibling distance = 3in/#1, level distance = 1.5cm}] 
  \node [block] {P1}
    child {node [block] {P2}
      child {node [block] {P3}
        child {node [block] {P4}}
        child {node [block] {P5}}
      }
    }
    child {node [block] {P6}
      child {node [phantom] {}
        child {node [block] {P7}}
      }
    }
    child {node [block] {P8}
      child {node [block] {P9}
        child {node [block] {P10}}
      }
    }
  ;
\end{tikzpicture}

\end{document}

I am using the phantom node to get P7 at the same level as P4, P5 and P10.

But in place of the phantom node, I get some blank space, the connector from P6 terminates theres and a new connector starts from that blank space to P7.

How do I get a straight connector from P6 to P7? Generally, how do we adjust 'level' of various nodes?

5

This is a case in which the power of the forest can be helpful; the tier key let's you get the desired alignment with no effort (notice also the shorter code):

\documentclass{article}
\usepackage{forest}

\tikzset{
treenode/.style={
  align=center, 
  inner sep=2pt, 
  rounded corners=2pt,
  minimum width = 2cm,
  font=\sffamily
  },
block/.style={
  treenode, 
  rectangle, 
  white, 
  font=\sffamily\bfseries, 
  draw=black, 
  fill=black
  },
  phantom/.style = {}
}

\begin{document}


\begin{forest}
for tree={
  block,
  edge={->,>=latex},
  where level={0}{s sep=1.5cm}{}
}
[P1
  [P2
    [P3
      [P4]
      [P5,tier=last]
    ]
  ]
  [P6,before computing xy={s=(s("!p")+s("!n"))}
    [P7,tier=last]
  ]
  [P8
    [P9]
    [P10,tier=last]
  ]
]
\end{forest}

\end{document}

enter image description here

The code can be even shorter since the current labelling schema for the nodes can also be left to the package.

  • wish, I could upvote twice :) Very elegant! – deshmukh Dec 18 '14 at 13:14
  • 1
    I did not quite understand the xy={s=(s("!p")+s("!n") magic. Could you please elaborate? – deshmukh Dec 19 '14 at 4:06
4

This is a possible solution. To remove the space use coordinate instead of node, Here edge from parent/.style={draw=none} is used to not draw the line from P6 to P7, then draw the (P6)--(P7) line manually.

enter image description here

Code

\documentclass[border=1cm]{standalone}
%\documentclass[landscape]{article}

\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\begin{document}

\tikzset{
  treenode/.style = {align=center, inner sep=2pt, rounded corners = 2pt, minimum width = 2cm, text centered, font=\sffamily},
  block/.style = {treenode, rectangle, white, font=\sffamily\bfseries, draw=black, fill=black},
  phantom/.style = {},
}

\begin{tikzpicture}[->,>=stealth',
level/.style={sibling distance = 3in/#1, level distance = 1.5cm}
] 
  \node [block] {P1}
    child {node [block] {P2}
      child {node [block] {P3}
        child {node [block] {P4}}
        child {node [block] {P5}}
      }
    }
    child {node [block](a) {P6}
      child {coordinate [phantom,edge from parent/.style={draw=none}] {}
        child {node [block](b) {P7}}
      }
    }
    child {node [block] {P8}
      child {node [block] {P9}
        child {node [block] {P10}}
      }
    }
  ;
\draw[->] (a)--(b);
\end{tikzpicture}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.