防止 tikz 树节点重叠

防止 tikz 树节点重叠

我正在用 tikz 绘制句子的树结构,其中节点包含单词,有时可能更长。如果发生这种情况,它们可能会与相邻节点重叠。我如何告诉 tikz 保持相邻节点之间的最小间距?我的 MWE:

\documentclass{standalone}

\usepackage{tikz}
\tikzset{
  treenode/.style = {align=center, inner sep=3pt, text centered,
    font=\sffamily},
  arn_n/.style = {treenode, rectangle, text width=3em},
  arn_x/.style = {treenode},
  gray-arrow/.style = {draw=gray}
}
\usetikzlibrary{arrows}

\begin{document}
\begin{tikzpicture}[->,>=stealth',level/.style={sibling distance = 10cm/#1,
  level distance = 1cm}] 
\node [arn_n]{NP}
child { node [arn_n] {N'}
    child { node [arn_n] {N'}
        child { node [arn_n] {aa}
            child { node [arn_x] {der}
            }
        }
        child { node [arn_n] {N'}
            child { node [arn_n] {AP}
                child { node [arn_x] {geduldigsten}
                }
            }
            child { node [arn_n] {N'}
                child { node [arn_n] {N}
                    child { node [arn_x] {Konservativen}
                    }
                }
            }
        }
    }
    child { node [arn_n] {N'}
        child { node [arn_n] {aa}
            child { node [arn_x] {unserer}
            }
        }
        child { node [arn_n] {N'}
            child { node [arn_n] {N}
                child { node [arn_x] {Partei}
                }
            }
        }
    }
}
;
\end{tikzpicture}
\end{document}

渲染的 MWE

在这种情况下,包含“unserer”和“N'”的节点在第五层重叠。有什么聪明的方法可以防止这种情况发生?

答案1

forest为您做:

\documentclass{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta}

\begin{document}
\begin{forest}
for tree={
  treenode/.style = {align=center, inner sep=3pt, 
        text centered, font=\sffamily},
  arn_n/.style = {treenode, rectangle, text width=3em},
  arn_x/.style = {treenode},
  gray-arrow/.style = {draw=gray},
  edge=-{Stealth}}
[NP, arn_n
    [N', arn_n
        [N', arn_n
            [aa, arn_n
                [der, arn_x]]
            [N', arn_n
                [AP, arn_n
                    [geduldigsten, arn_x]]
                [N', arn_n
                    [N, arn_n
                        [Konservativen, arn_x]]]]]
        [N', arn_n
            [aa, arn_n
                [unserer, arn_x]]
            [N', arn_n
                [N, arn_n
                    [Partei, arn_x]]]]]]
\end{forest}
\end{document}

在此处输入图片描述

相关内容