
Betrachten Sie das 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}
Ich verwende den Phantomknoten, um P7 auf das gleiche Niveau wie P4, P5 und P10 zu bringen.
Anstelle des Phantomknotens erhalte ich jedoch ein leeres Feld, der Konnektor von P6 endet dort und ein neuer Konnektor beginnt an diesem leeren Feld und führt zu P7.
Wie erhalte ich eine gerade Verbindung von P6 zu P7? Wie passen wir im Allgemeinen die „Ebene“ verschiedener Knoten an?
Antwort1
Dies ist ein Fall, in dem die Macht derforest
kann hilfreich sein; mit der tier
Taste können Sie mühelos die gewünschte Ausrichtung erzielen (beachten Sie auch den kürzeren 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}
Der Code kann sogar noch kürzer sein, da das aktuelle Beschriftungsschema für die Knoten auch dem Paket überlassen werden kann.
Antwort2
Dies ist eine mögliche Lösung. Um das Leerzeichen zu entfernen, verwenden Sie coordinate
anstelle von node
. Hier
edge from parent/.style={draw=none}
wird verwendet, um die Linie von P6 nach P7 nicht zu zeichnen. Zeichnen Sie dann die Linie (P6)--(P7) manuell.
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}