TikZ,树,一些没有箭头的子项

TikZ,树,一些没有箭头的子项

我想创建一个树形结构,现在我快完成了。请注意,这是我使用 TikZ 的第一个“项目”。我的问题:第一层和第二层的部分子元素没有用箭头连接。有人能帮我吗?与最后一层相比,我没有看到任何区别。

\begin{tikzpicture}[->,>=stealth',
edge from parent/.style={thick,draw=black!70,-latex},
level 1/.style={sibling distance=8.5cm},
,level distance =3.5cm,auto]

\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, fill=gray!20,minimum size=5mm, font =\scriptsize]

\tikzstyle{every node}=[font=\footnotesize]


\node[and]  {\begin{tabular}{l} 1 \\ 2 \\ 3\end{tabular}}
  child { [sibling distance = 2cm] node [and] (appr) {\begin{tabular}{l} 1 \\ 2 \\ 3\end{tabular}}
  child{ node [terminal] (car) {2} }
  child{ node [relation] (rel) {\begin{tabular}{l} 1 \\ 2 \\ 3\end{tabular}}  }
  child{ node [terminal] (zebra) {\begin{tabular}{l} 1 \\ 2 \\ 3\end{tabular}}  }  } 
  child { [sibling distance = 6cm] node [or] (obstacle)  {\begin{tabular}{l} 1 \\ 2 \\ 3\end{tabular}}
child{ [sibling distance = 2cm]  node [and] (ped_on_zebra) {\begin{tabular}{l} 1 \\ 2 \\ 3\end{tabular}}  
    child{ node [terminal] (ped) {1} }
    child{ node [relation] (rel_1) {\begin{tabular}{l} 1 \\ 2 \\ 3\end{tabular}}  }
    child{  node [terminal] (zebra_2) {\begin{tabular}{l} 1 \\ 2 \\ 3\end{tabular}}  }  }
child { [sibling distance = 2cm]  node [and] (dog_on_zebra)  {\begin{tabular}{l} 1 \\ 2 \\ 3\end{tabular}}   
    child{ node [terminal] (dog) {3} }
    child{ node [relation] (rel_2) {\begin{tabular}{l} 1 \\ 2 \\ 3\end{tabular}}  }
    child{ node [terminal] (zebra_1) {\begin{tabular}{l} 1 \\ 2 \\ 3\end{tabular}}  } }}
  ;

%Draw Relations
\path [dashed,-]
  (zebra) edge node [left] {} (rel)
  (rel) edge node [right] {} (car)

  (rel_1) edge node [left] {} (ped)
  (rel_1) edge node [right] {} (zebra_2)

  (rel_2) edge node [left] {} (dog)
  (rel_2) edge node [right] {} (zebra_1);

%create legend
\node [relation] (terminal)at (7,-2) {};
\node [right=0.2cm of terminal] {= Relation};
\node [terminal] (terminal)at (7,-1) {};
\node [right=0.2cm of terminal] {= Terminal-Node};
\node [and] (and) at (7,0) {};
\node [right=0.2cm of and] {= And-Node};
\node [or] (or) at (7,1) {} ;
\node [right=0.2cm of or] {= Or-Node};

\end{tikzpicture}

答案1

箭头在那里,但隐藏在节点后面。

造成这种情况的原因是语法输入错误。如果子节点中没有节点,TikZ 不会将节点视为子节点,而是将任何节点视为子节点(您可以将任意内容放入其中)。sibling distance在节点后面直接指定(它是您指定距离的兄弟节点的父节点)。

我擅自清理了一些代码,并改变了图例的过程,它使用了库chains(反过来positioning也使用了库)。

顺便说一句,树节点会自动命名,因此可能有更自动化的方式来连接最后的子节点,但那是另一个故事。

我没有将child anchor=north选项添加到第一级,因为对我来说它看起来有点奇怪,但你可以改变它。事实上,你可以直接添加child anchor=north到 TikZ 图片而不是级别样式。

代码

\documentclass[tikz,convert=false]{standalone}
\usetikzlibrary{arrows,chains}
\begin{document}
\begin{tikzpicture}[
  ->,
  >=stealth',
  edge from parent/.style={thick,draw=black!70,-latex},
  level 1/.style={sibling distance=8.5cm},
  level 2/.style={child anchor=north},
  level 3/.style={child anchor=north,sibling distance=2cm},
  level distance=3.5cm,
  auto,
  tree nodes/.style={
    shape=rectangle,
    rounded corners,
    thick,
    draw=black!75,
    minimum size=+5mm
  },
  and/.style     ={tree nodes, top color=blue!10, bottom color=white},
  or/.style      ={tree nodes, top color=red!10,  bottom color=white},
  terminal/.style={tree nodes, top color=green!5, bottom color=white},
  relation/.style={tree nodes, inner sep=2pt, fill=gray!20, font=\scriptsize},
  font=\footnotesize,
  every node/.append style={align=center}
]
\node              [and]                     {1 \\ 2 \\ 3}
  child {     node [and]      (appr)         {1 \\ 2 \\ 3}
    [sibling distance = 2cm]
    child {   node [terminal] (car)          {2}          }
    child {   node [relation] (rel)          {1 \\ 2 \\ 3}}
    child {   node [terminal] (zebra)        {1 \\ 2 \\ 3}}
  } 
  child {     node [or]       (obstacle)     {1 \\ 2 \\ 3}
    [sibling distance = 6cm]
    child {   node [and]      (ped_on_zebra) {1 \\ 2 \\ 3}
      child { node [terminal] (ped)          {1}          }
      child { node [relation] (rel_1)        {1 \\ 2 \\ 3}}
      child { node [terminal] (zebra_2)      {1 \\ 2 \\ 3}}
    }
    child   { node [and]      (dog_on_zebra) {1 \\ 2 \\ 3}
      child { node [terminal] (dog)          {3}          }
      child { node [relation] (rel_2)        {1 \\ 2 \\ 3}}
      child { node [terminal] (zebra_1)      {1 \\ 2 \\ 3}}
    }
  };

\path [dashed,-]
  (rel) edge (zebra)
        edge (car)

  (rel_1) edge (ped)
          edge (zebra_2)

  (rel_2) edge (dog)
          edge (zebra_1);

\begin{scope}[shift={(7,1)}, node distance=+1cm, on grid, label position=right, start chain=ch going above]
  \foreach \sStyle/\tText in {relation/Relation, terminal/Terminal-Node, and/And-Node, or/Or-Node} 
    \node[
     on chain=ch,
     \sStyle,
     label={= \tText}
    ] {};
\end{scope}
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述

答案2

箭头在那里,但隐藏在节点后面。缩短边缘或更改锚点,使其不位于中心,例如node [and,anchor=north east]

\node[and]  {\begin{tabular}{l} 1 \\ 2 \\ 3\end{tabular}}
  child { [sibling distance = 2cm] node [and,anchor=north east] (appr) 
    {\begin{tabular}{l} 1 \\ 2 \\ 3\end{tabular}}
  child{ node [terminal] (car) {2} }
  child{ node [relation] (rel) {\begin{tabular}{l} 1 \\ 2 \\ 3\end{tabular}}  }
  child{ node [terminal] (zebra) {\begin{tabular}{l} 1 \\ 2 \\ 3\end{tabular}}  }  } 
  child { [sibling distance = 6cm] node [or,anchor=north west] (obstacle)
    {\begin{tabular}{l} 1 \\ 2 \\ 3\end{tabular}}
...

在此处输入图片描述

相关内容