TikZ 树中的不同节点大小发生冲突

TikZ 树中的不同节点大小发生冲突

对于 TikZ 中给定的树集,我遇到一个问题,即具有不同高度的节点(这里是一条线与两条线)与分离距离相冲突。以下是显示问题的图表:

单线节点和双线节点之间没有清晰距离的树

\tikzstyle{every node}=[draw=gray, anchor=west]
\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.5)},%
  edge from parent path={(\tikzparentnode.south)%
  |- (\tikzchildnode.west)}]
    \node {root}    
    child { 
        node {A} 
        child { 
            node {One line} 
        } 
        child { 
            node {One line} 
        } 
        child { 
            node[text width = 25em] {Two\\lines} 
        }
    };
\end{tikzpicture}

您有什么想法可以解决这个问题吗?

答案1

这很简单,使用包forest

\documentclass[margin=3mm]{standalone}
\usepackage[edges]{forest}

\begin{document}
    \begin{forest}
    for tree={grow'=0,folder,draw, align=left}
    [root
      [A
        [one line]
        [one line]
        [two\\ lines,text width=25ex]
        ]
      ]
    \end{forest}
\end{document}

它定义节点边缘之间的距离。上面的例子使用了默认值。

在此处输入图片描述

答案2

child如果在你的两行子命令之后没有后续命令,那么你可以放心的改变grow via three points那一个子命令的密钥:

\documentclass[tikz]{standalone}

\usetikzlibrary{trees}

\begin{document}
\tikzstyle{every node}=[draw=gray, anchor=west]
\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.5)},%
  edge from parent path={(\tikzparentnode.south)%
  |- (\tikzchildnode.west)}]
    \node {root}    
    child { 
        node {A} 
        child { 
            node {One line} 
        } 
        child { 
            node {One line} 
        } 
        child[grow via three points={one child at (0.5,-0.7) and two children at
          (0.5,-0.7) and (0.5,-1.6)}] { 
          node[text width = 25em] {Two\\lines} 
        }
    };
\end{tikzpicture}
\end{document}

相关内容