以下是 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}
不同节点之间的连接线是斜线。如何用水平 - 垂直线代替这些线?
答案1
另一个选项是使用forest
。在这种情况下,您可以省去节点phantom
,只需使用与tier
对齐等。我还更新了您的代码以使用库提供的当前箭头语法,因为现在推荐使用这种语法。我喜欢它,因为指定树涉及的输入要少得多!P7
P4
P5
arrows.meta
forest
更新的解决方案(Forest 版本 2+)
\documentclass[tikz, border=5pt]{standalone}
\usepackage[edges]{forest}
\usetikzlibrary{arrows.meta}
\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},
}
% Forest version 2.1
\begin{forest}
for tree={
block,
edge+={thick, -{Stealth[]}}
},
forked edges
[P1
[P2
[P3
[P4, tier=terminal]
[P5]
]
]
[P6, calign with current
[P7, tier=terminal]
]
[P8
[P9
[P10]
]
]
]
;
\end{forest}
\end{document}
原始解决方案(森林版本 1)
\documentclass[tikz, border=5pt]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta}
\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},
}
\begin{forest}
for tree={
parent anchor=south,
child anchor=north,
block,
edge path={
\noexpand\path [-{Stealth[]}, \forestoption{edge}, thick]
(!u.parent anchor) -- +(0,-5pt) -| (.child anchor)\forestoption{edge label};
},
}
[P1
[P2
[P3
[P4, tier=terminal]
[P5]
]
]
[P6, calign with current
[P7, tier=terminal]
]
[P8
[P9
[P10]
]
]
]
;
\end{forest}
\end{document}
结果
基本上,无论您使用哪种解决方案,输出都是相同的。
答案2
当您说水平/垂直时,您是指这个吗?
以下是代码:
\documentclass[tikz,border=10]{standalone}
\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 = {},
edge from parent/.style={draw,red,thick},
edge from parent path={(\tikzparentnode.south)-- ++(0,-3mm) -| (\tikzchildnode.north)}
}
\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}
编辑:我刚刚发现我们可以使用库[edge from parent fork down]
中的样式trees
来产生相同的效果。
答案3
PSTricks 解决方案:
\documentclass{article}
\usepackage{pstricks}
\usepackage{xfp}
\psset{
linecolor = red,
arrows = ->
}
\def\Label(#1,#2)#3{%
\psframe[
framearc = 0.3,
linecolor = black,
fillstyle = solid,
fillcolor = black
](\fpeval{#1-0.5*\widthBox},\fpeval{#2-0.5*\heightBox})%
(\fpeval{#1+0.5*\widthBox},\fpeval{#2+0.5*\heightBox})
\rput(#1,#2){\textcolor{white}{#3}}}
\begin{document}
% parameters
\def\widthBox{1.4}
\def\heightBox{0.4}
\def\horiDist{4.5}
\def\vertDist{1.6}
% drawing
\begin{pspicture}(\fpeval{2*\horiDist+1.75*\widthBox},\fpeval{4*\heightBox+3*\vertDist})
% left
\Label(\fpeval{1.25*\widthBox},\fpeval{2.5*\heightBox+2*\vertDist}){P2}
\psline(\fpeval{1.25*\widthBox},\fpeval{2*\heightBox+2*\vertDist})%
(\fpeval{1.25*\widthBox},\fpeval{2*\heightBox+\vertDist})
\Label(\fpeval{1.25*\widthBox},\fpeval{1.5*\heightBox+\vertDist}){P3}
\psline(\fpeval{1.25*\widthBox},\fpeval{\heightBox+\vertDist})%
(\fpeval{1.25*\widthBox},\fpeval{\heightBox+0.5*\vertDist})%
(\fpeval{0.5*\widthBox},\fpeval{\heightBox+0.5*\vertDist})%
(\fpeval{0.5*\widthBox},\heightBox)
\psline(\fpeval{1.25*\widthBox},\fpeval{\heightBox+0.5*\vertDist})%
(\fpeval{2*\widthBox},\fpeval{\heightBox+0.5*\vertDist})%
(\fpeval{2*\widthBox},\heightBox)
\Label(\fpeval{0.5*\widthBox},\fpeval{0.5*\heightBox}){P4}
\Label(\fpeval{2*\widthBox},\fpeval{0.5*\heightBox}){P5}
% middle
\Label(\fpeval{\horiDist+1.25*\widthBox},\fpeval{3.5*\heightBox+3*\vertDist}){P1}
\psline(\fpeval{\horiDist+1.25*\widthBox},\fpeval{3*\heightBox+3*\vertDist})%
(\fpeval{\horiDist+1.25*\widthBox},\fpeval{3*\heightBox+2*\vertDist})
\Label(\fpeval{\horiDist+1.25*\widthBox},\fpeval{2.5*\heightBox+2*\vertDist}){P6}
\psline(\fpeval{\horiDist+1.25*\widthBox},\fpeval{2*\heightBox+2*\vertDist})%
(\fpeval{\horiDist+1.25*\widthBox},\heightBox)
\Label(\fpeval{\horiDist+1.25*\widthBox},\fpeval{0.5*\heightBox}){P7}
\psline(\fpeval{\horiDist+1.25*\widthBox},\fpeval{1.5*\heightBox+\vertDist-0.1})%
(\fpeval{\horiDist+1.25*\widthBox},\heightBox)
% right
\Label(\fpeval{2*\horiDist+1.25*\widthBox},\fpeval{2.5*\heightBox+2*\vertDist}){P8}
\psline(\fpeval{2*\horiDist+1.25*\widthBox},\fpeval{2*\heightBox+2*\vertDist})%
(\fpeval{2*\horiDist+1.25*\widthBox},\fpeval{2*\heightBox+\vertDist})
\Label(\fpeval{2*\horiDist+1.25*\widthBox},\fpeval{1.5*\heightBox+\vertDist}){P9}
\psline(\fpeval{2*\horiDist+1.25*\widthBox},\fpeval{\heightBox+\vertDist})%
(\fpeval{2*\horiDist+1.25*\widthBox},\heightBox)
\Label(\fpeval{2*\horiDist+1.25*\widthBox},\fpeval{0.5*\heightBox}){P10}
% connection
\psline(\fpeval{\horiDist+1.25*\widthBox},\fpeval{3*\heightBox+2.5*\vertDist})%
(\fpeval{1.25*\widthBox},\fpeval{3*\heightBox+2.5*\vertDist})%
(\fpeval{1.25*\widthBox},\fpeval{3*\heightBox+2*\vertDist})
\psline(\fpeval{\horiDist+1.25*\widthBox},\fpeval{3*\heightBox+2.5*\vertDist})%
(\fpeval{2*\horiDist+1.25*\widthBox},\fpeval{3*\heightBox+2.5*\vertDist})%
(\fpeval{2*\horiDist+1.25*\widthBox},\fpeval{3*\heightBox+2*\vertDist})
\end{pspicture}
\end{document}
使用此代码,您只需选择框的宽度 ( \widthBox
) 和高度 ( ),以及它们之间的水平 ( ) 和垂直 ( ) 距离。然后绘图将自动进行相应调整。\heightBox
\horiDist
\vertDist