
Considere o 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}
Estou usando o nó fantasma para colocar P7 no mesmo nível de P4, P5 e P10.
Mas no lugar do nó fantasma, recebo algum espaço em branco, o conector de P6 termina aí e um novo conector começa desse espaço em branco para P7.
Como obtenho um conector reto de P6 a P7? Geralmente, como ajustamos o 'nível' de vários nós?
Responder1
Este é um caso em que o poder doforest
pode ser útil; a tier
chave permite obter o alinhamento desejado sem esforço (observe também o código mais curto):
\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}
O código pode ser ainda mais curto, pois o esquema de rotulagem atual dos nós também pode ser deixado para o pacote.
Responder2
Esta é uma solução possível. Para remover o uso de espaço coordinate
em vez de node
, aqui
edge from parent/.style={draw=none}
é usado para não desenhar a linha de P6 a P7 e, em seguida, desenhar a linha (P6)--(P7) manualmente.
Código
\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}