防止 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}

在此輸入影像描述

相關內容