我在排列子节点时遇到了麻烦。如果使用 anchor=north 设置,它们似乎没有水平居中。它们看起来是根据节点的顶部边缘排列的,但我想让它们居中,这样子节点之间的连接就是平行的。我希望你明白我的意思!
\begin{tikzpicture}[->,>=stealth',
edge from parent/.style={thick,draw=black!70,-latex},
level 1/.style={sibling distance=4cm},
,level distance =4.5cm,auto,scale=0.75,transform shape]
\tikzstyle{and}=[rectangle, rounded corners,thick,draw=black!75,top color=blue!10, bottom color=white,minimum size=5mm]
\tikzstyle{or}=[rectangle,rounded corners,thick,draw=black!75,top color=red!10, bottom color=white,minimum size=5mm]
\tikzstyle{terminal}=[rectangle, rounded corners, thick,draw=black!75, top color=green!5, bottom color=white,minimum size=5mm]
\tikzstyle{relation}=[rectangle,rounded corners,inner sep=2pt, thick,draw=black!75, top color=gray!20, bottom color=white,minimum size=5mm, font =\scriptsize]
\node [or,right=6.75cm of obstacle] (obstacle1) {\begin{tabular}{l} Obstacle at \\ zebra crossing \end{tabular}}
child{ [sibling distance = 8cm] node [and,anchor=north] (ped_on_zebra) {\begin{tabular}{l} Pedestrian \\ on zebra crossing \end{tabular}}
child{ node [terminal,anchor=north] (ped1) {Pedestrian} }
child{ node [relation,anchor=north] (rel_11) {\begin{tabular}{l} dist., \\ vel. \\ orient.\end{tabular}} }
child{ node [terminal,anchor=north] (zebra_21) {\begin{tabular}{l} Zebra \\ crossing \end{tabular}} } }
;
\path
(rel_11) edge node [left] {} (ped1)
(rel_11) edge node [right] {} (zebra_21);
\end{tikzpicture}
答案1
如果您想使用anchor=north
节点terminal
并用水平线连接它们,则有两个选择:
1- 使所有terminal
和relation
节点具有相同的高度,并且minimum height
足够大。这样,它们将根据north
锚点对齐,同时也根据它们的对齐center
。
2- 不要触碰terminal
节点relation
的高度,而是在它们之间画一条水平线,这与它们中心之间的线不同。我认为这不太好,但可行:
\path
(rel_11.west|-ped1) edge node [left] {} (ped1)
(rel_11.east|-ped1) edge node [right] {} (zebra_21.west|-ped1);
红线在中心之间绘制,绿线根据之前的命令绘制。
下一个代码是你的修改版本。有什么不同?
[...]
1- 所有样式都在after内定义\begin{tikzpicture}
,并避免\tikzstyle
2- 除 之外,所有节点都共享相同的选项top color
,因此您可以使用mynode
一个参数声明一个通用样式( ),并使用它来定义terminal
、relation
、 ... 样式。
3-mynode
包括选项text width
和align
,允许避免使用tabular
节点内的文本。您仍然可以使用\\
换行符或让 LaTeX 为您完成此操作。
4- 我已经node [left] {}
从edge
路径中抑制了它们,因为它们在这个图中是多余的。
5-我已抑制,因为此示例中 ,right=6.75cm of obstacle
没有节点。obstacle
\documentclass[tikz]{standalone}
\usetikzlibrary{arrows,positioning}
\begin{document}
\begin{tikzpicture}[->,>=stealth',
edge from parent/.style={thick,draw=black!70,-latex},
level 1/.style = {sibling distance=4cm},
level distance = 4.5cm,auto,scale=0.75,transform shape,
mynode/.style = {rectangle, rounded corners, thick, minimum size = 5mm, text width = 3cm, align = center, draw=black!75, bottom color=white, top color=#1},
and/.style = {mynode=blue!10},
or/.style = {mynode=red!10},
terminal/.style = {mynode=green!10},
relation/.style = {mynode=gray!10, font=\scriptsize},
]
\node [or] (obstacle1) {Obstacle at \\ zebra crossing}
child{ [sibling distance = 8cm] node [and,anchor=north] (ped_on_zebra) {Pedestrian \\ on zebra crossing}
child{ node [terminal,anchor=north] (ped1) {Pedestrian} }
child{ node [relation,anchor=north] (rel_11) {dist., \\ vel. \\ orient.} }
child{ node [terminal,anchor=north] (zebra_21) {Zebra \\ crossing} } }
;
\path[red]
(rel_11) edge (ped1)
(rel_11) edge (zebra_21);
\path[green]
(rel_11.west|-ped1) edge (ped1)
(rel_11.east|-ped1) edge (zebra_21.west|-ped1);
\end{tikzpicture}
\end{document}