使用 tikz 制作树结构

使用 tikz 制作树结构

我想通过 创建一个树形结构tikz。我并不是专业的 Latex 用户,我只是尝试结合示例使其工作,但首先我希望边缘有方向,然后T 3重叠T 4,然后我希望能够使每层节点之间的空间更灵活地改变。这是我的代码:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{graphdrawing}
\usetikzlibrary{shapes,arrows,calc}
\usetikzlibrary{graphs}
\tikzstyle{line} = [draw, -latex']
\begin{document}
\begin{tikzpicture}[sibling distance=10em,
  every node/.style = {shape=rectangle, rounded corners,
    draw, align=center,
    top color=white, bottom color=white!20}]]
  \node {P}
    child { node (ch1){char1} 
        child { node {T 1}
        child { node {comment} } }
        child { node {T 2}
        child { node {comment} } }
        child { node {T 3}
        child { node {comment} } } node [left=2cm of char 1] (s1){script 1}}
    child { node (ch2){char 2} 
      child { node {T 2}
        child { node {comment} } }
        child { node {T 4}
        child { node {comment} } }
        child { node {T 5}
        child { node {comment} } } node [right=2cm of char 2] (s2){script 2}};
   %Draw edges
   \path [line] (s2) -- node {}(ch2);
   \path [line] (s1) -- node {}(ch1);
\end{tikzpicture}

\end{document}

char 1和之间的边缘上有一个圆圈script 1。我不明白为什么?任何建议都将不胜感激!!

答案1

  1. 出现圆圈是因为你node{}\path [line] (s2) -- node {}(ch2);
  2. 您可以通过说 来添加箭头edge from parent/.style={draw,-latex'}
  3. 您正在加载,graphdrawing但我无法察觉您正在使用它。
  4. left=2cm of char 1应该是left=2cm of ch1
  5. 您可以使用例如来更改任何级别的尺寸/距离level 1/.style={sibling distance=50mm},level 2/.style={sibling distance=15mm},level 3/.style={text width=1cm,font=\tiny}
  6. 为了调试,您可以临时设置opacity=0.5以更好地了解某些节点去了哪里。
  7. 您的其他陈述我不明白。您能否考虑将它们说得更清楚一些?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,calc,positioning}
\usetikzlibrary{graphs}
\begin{document}
\begin{tikzpicture}[sibling distance=10em,%opacity=0.5,
  every node/.style = {shape=rectangle, rounded corners,
    draw, align=center,
    top color=white, bottom color=white!20},
    line/.style={draw, -latex'},
    edge from parent/.style={draw,-latex'},
    level 1/.style={sibling distance=50mm}, 
    level 2/.style={sibling distance=15mm},
    level 3/.style={text width=1cm,font=\tiny}]
  \node {P}
    child { node (ch1){char1} 
        child { node {T 1}
        child { node {comment} } }
        child { node {T 2}
        child { node {comment} } }
        child { node {T 3}
        child { node {comment} } } node [left=2cm of ch1] (s1){script 1}}
    child { node (ch2){char 2} 
      child { node {T 2}
        child { node {comment} } }
        child { node {T 4}
        child { node {comment} } }
        child { node {T 5}
        child { node {comment} } } node [right=2cm of ch2] (s2){script 2}};
   %Draw edges
   \path [line] (s2) -- (ch2);
   \path [line] (s1) -- (ch1);
\end{tikzpicture}
\end{document}

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{graphdrawing}
\usetikzlibrary{shapes,arrows,calc}
\usetikzlibrary{graphs}
\tikzstyle{line} = [draw, -latex']
\begin{document}
\begin{tikzpicture}[sibling distance=10em,
  every node/.style = {shape=rectangle, rounded corners,
    draw, align=center,
    top color=white, bottom color=white!20}]]
  \node {P}
    child { node (ch1){char1} 
        child { node {T 1}
        child { node {comment} } }
        child { node {T 2}
        child { node {comment} } }
        child { node {T 3}
        child { node {comment} } } node [left=2cm of char 1] (s1){script 1}}
    child { node (ch2){char 2} 
      child { node {T 2}
        child { node {comment} } }
        child { node {T 4}
        child { node {comment} } }
        child { node {T 5}
        child { node {comment} } } node [right=2cm of char 2] (s2){script 2}};
   %Draw edges
   \path [line] (s2) -- node {}(ch2);
   \path [line] (s1) -- node {}(ch1);
\end{tikzpicture}

\end{document}

在此处输入图片描述

你的评论让我很困惑。像这样吗?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,calc,positioning}
\usetikzlibrary{graphs}
\begin{document}
\begin{tikzpicture}[sibling distance=10em,%opacity=0.5,
  every node/.style = {shape=rectangle, rounded corners,
    draw, align=center,
    top color=white, bottom color=white!20},
    line/.style={draw, -latex'},
    edge from parent/.style={draw,-latex'},
    level 1/.style={sibling distance=30mm}, 
    level 2/.style={sibling distance=15mm},
    level 3/.style={text width=1cm,font=\tiny}]
  \node {P}
    child { node (ch1){char1} 
        child { node {T 1}
        child { node {comment} } }
        child { node {T 3}
        child { node {comment} } }
        child { node {T 2}
        child { node {comment} } } node [left=2cm of ch1] (s1){script 1}}
    child { node (ch2){char 2} 
        child { node {T 2}
        child { node {comment} } }
        child { node {T 4}
        child { node {comment} } }
        child { node {T 5}
        child { node {comment} } } node [right=2cm of ch2] (s2){script 2}};
   %Draw edges
   \path [line] (s2) -- (ch2);
   \path [line] (s1) -- (ch1);
\end{tikzpicture}
\end{document}

在此处输入图片描述

请注意,这只是为了澄清您的请求,而不是真正这样做的建议。(这不是一棵树。)

相关内容