
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}
나는 팬텀 노드를 사용하여 P7을 P4, P5 및 P10과 동일한 수준으로 얻습니다.
그러나 팬텀 노드 대신에 약간의 빈 공간이 생기고 P6의 커넥터가 거기에서 끝나고 새 커넥터가 해당 빈 공간에서 P7로 시작됩니다.
P6에서 P7까지 직선 커넥터를 얻으려면 어떻게 해야 합니까? 일반적으로 다양한 노드의 '레벨'을 어떻게 조정합니까?
답변1
이는 권력의 힘이 발휘된 사례다.forest
도움이 될 수 있습니다. 이 tier
키를 사용하면 아무런 노력 없이 원하는 정렬을 얻을 수 있습니다(더 짧은 코드도 확인하세요).
\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}
노드에 대한 현재 레이블 지정 스키마도 패키지에 남겨둘 수 있으므로 코드는 훨씬 더 짧아질 수 있습니다.
답변2
이것이 가능한 해결책입니다. coordinate
대신 공간 사용을 제거하려면 P6에서 P7까지 선을 그리지 않고 (P6)-(P7) 선을 수동으로 그리는 데 사용됩니다 node
.
edge from parent/.style={draw=none}
암호
\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}