4

I'm drawing tree structures of sentences with tikz, where nodes contain words, which may be longer sometimes. If that occurs, they are likely to overlap with neighbouring nodes. How can I tell tikz to keep a minimum spacing between neighbouring nodes? My 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}

Rendered MWE

In this case, the nodes containing "unserer" and "N'" overlap on the fifth level. What's a smart way to prevent that?

1

forest does it for you:

\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}

enter image description here

  • @LukeLR I'd recommend loading the linguistics library, probably. – cfr Jan 27 '17 at 23:35
  • You are redefining the styles for every node in the tree. It would be better to define these once rather than repeatedly. And 2 of them are never used. There's no reason not to leave them in the \tikzset{} in the preamble of the document if they are globally useful. – cfr Jan 27 '17 at 23:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.