乳胶中的树:4 个节点的深度

乳胶中的树:4 个节点的深度

我正在关注当前教程(MWE):关联

但是,我在绘制 4 个节点的树时遇到了困难。项目是重叠的,即使我添加一个也 child [missing] {} 无法解决问题。

下面是一个未正确显示的代码示例:

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\tikzstyle{every node}=[draw=black,thick,anchor=west]
\tikzstyle{selected}=[draw=red,fill=red!30]
\tikzstyle{optional}=[dashed,fill=gray!50]
\begin{tikzpicture}[%
  grow via three points={one child at (0.5,-0.7) and
  two children at (0.5,-0.7) and (0.5,-1.4)},
  edge from parent path={(\tikzparentnode.south) |- (\tikzchildnode.west)}]
\node {texmf}
    child { node {doc}}     
    child { node {fonts}}
    child { node {source}}
    child { node [selected] {tex}
      child { node {generic}}
      child { 
         node [optional] {latex}
         child { node {latex21} }
         child { node {latex22} }
         child { node {latex23} }
      }
      child [missing] {}              
      child [missing] {}              
      child [missing] {}              
      child { 
         node {plain}
         child { node {plain21} }
         child { node {plain22} }
         child { node {plain23} }
         }
      child [missing] {}              
      child [missing] {}              
      child [missing] {}              
    }
    child [missing] {}              
    child [missing] {}              
    child [missing] {}              
    child [missing] {}              
    child [missing] {}              
    child [missing] {}              
    child { node {texdoc}};  
\end{tikzpicture}
\end{document}

显示问题,textdoc 应该在下面

其实我不明白在何时何地使用child [missing] {}。提前感谢您的帮助。

答案1

简短回答:

child[missing]如果您希望texdoc节点在水平方向上“独立”,则应该再添加 3 。

长答案:

Tikz实际上试图生长平衡树,因此要绘制这样的目录,需要一点“魔法”:节点missing。要进一步了解missing节点的工作原理,您可以查看 pgf/TikZ 手册。基本上,TikZ 绘制树时不考虑节点大小,因此使用此代码需要“跳过”一些节点(将它们标记为missing)以获得正确的位置。对于两个节点Ab在第 2 级,你应该missing在中间插入与后代相同的节点A(子孙等等)。这是为了让树“跳过”尽可能多的节点,以便b在其正确的位置。在您的例子中,有 6 个叶节点(级别 4)和 3 个中间节点(级别 3),因此您需要“跳过”9 个节点。以下代码:

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\tikzstyle{every node}=[draw=black,thick,anchor=west]
\tikzstyle{selected}=[draw=red,fill=red!30]
\tikzstyle{optional}=[dashed,fill=gray!50]
\begin{tikzpicture}[%
  grow via three points={one child at (0.5,-0.7) and
  two children at (0.5,-0.7) and (0.5,-1.4)},
  edge from parent path={(\tikzparentnode.south) |- (\tikzchildnode.west)}]
\node {texmf}
    child { node {doc}}     
    child { node {fonts}}
    child { node {source}}
    child { node [selected] {tex}
      child { node {generic}}
      child { 
         node [optional] {latex}
         child { node {latex21} }
         child { node {latex22} }
         child { node {latex23} }
      }
      child [missing] {}              
      child [missing] {}              
      child [missing] {}              
      child { 
         node {plain}
         child { node {plain21} }
         child { node {plain22} }
         child { node {plain23} }
         }
      child [missing] {}              
      child [missing] {}              
      child [missing] {}              
    }
    child [missing] {}              
    child [missing] {}              
    child [missing] {}              
    child [missing] {}              
    child [missing] {}              
    child [missing] {}
    child [missing] {}              
    child [missing] {}              
    child [missing] {}                            
    child { node {texdoc}};  
\end{tikzpicture}
\end{document}

得到以下图像,希望这就是你想要的:

OP 的树具有正确的节点间距

相关内容