Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm trying to create a family tree with TikZ growing right. I have the tree working, but the text at each node is too long, it overlaps with later lines (e.g. My Family Tree, Paternal GMother and Paternal GFather below). Is there a way to fix this?

broken tikz tree

MWE

\documentclass{article}
\usepackage{calc}
\usepackage{tikz}
\usetikzlibrary{trees}

\begin{document}
\newlength\treeheight
\setlength{\treeheight}{\textheight-2cm}
\begin{tikzpicture}[grow=right,anchor=west,
  every node/.style={},
  level 1/.style={sibling distance=\treeheight/2},
  level 2/.style={sibling distance=\treeheight/4},
  level 3/.style={sibling distance=\treeheight/8}]
  % parents
  \node {My Family Tree}[edge from parent fork right]
    child { node {Father}
      child{ node {Paternal Gfather}
        child{ node {Paternal GGfather}}
        child{ node {Paternal GGmother}}
      }
      child{ node {Paternal Gmother}
        child{ node {Maternal GGfather}}
        child{ node {Maternal GGmother}}
      }
    }
    child { node {Mother}
      child{ node {Maternal Gfather}}
      child{ node {Maternal Gmother}}
    }

  ;
  \end{tikzpicture}
\end{document}
share|improve this question

3 Answers 3

up vote 4 down vote accepted

The level distance in your MWE is the distance between the center of a parent node and the left anchor of the child node. If there are more than one child the edge is splitted at half of the level distance. That's why the lines overlap the text if (half of) the node text is longer as (half of) level distance.

Using parent anchor=east and growth parent anchor=east the level distance is the distance between the borders of the parent and the childe node.

To avoid an overfull hbox I decrease the level distance to 0.5cm in the following example. If it is not necessary to observe the \textwidth you can remove this change.

enter image description here

\documentclass{article}
\usepackage{calc}
\usepackage{tikz}
\usetikzlibrary{trees}


\begin{document}
\newlength\treeheight
\setlength{\treeheight}{\textheight-2cm}
\noindent\begin{tikzpicture}[
  grow=right,
  anchor=west,
  growth parent anchor=east, % added code
  parent anchor=east, % added code
  level distance=0.5cm, % added code (not necessary)
  level 1/.style={sibling distance=\treeheight/2},
  level 2/.style={sibling distance=\treeheight/4},
  level 3/.style={sibling distance=\treeheight/8}]
  % parents
  \node {My Family Tree}[edge from parent fork right]
    child { node {Father}
      child{ node {Paternal Gfather}
        child{ node {Paternal GGfather}}
        child{ node {Paternal GGmother}}
      }
      child{ node {Paternal Gmother}
        child{ node {Maternal GGfather}}
        child{ node {Maternal GGmother}}
      }
    }
    child { node {Mother}
      child{ node {Maternal Gfather}}
      child{ node {Maternal Gmother}}
    }

  ;
  \end{tikzpicture}
\end{document}

To vertical align the forks add an align=center or align=left (it aligns the text in the nodes) to the picture options and change the level styles to

  level 1/.style={sibling distance=\treeheight/2,text width=\widthof{Mother}},
  level 2/.style={sibling distance=\treeheight/4,text width=\widthof{GGmother}},
  level 3/.style={sibling distance=\treeheight/8}]

In addition horizontal space is saved.

enter image description here

share|improve this answer

As an alternative to TikZ trees you can use forest and forget problems computing distances between nodes. The package does it for you.

\documentclass{article}
\usepackage{forest}

\begin{document}
\begin{forest}
   for tree={grow=0,parent anchor=east, child anchor=west, edge path={\noexpand\path[\forestoption{edge}] (\forestOve{\forestove{@parent}}{name}.parent anchor)-- +(0:2mm)|-(\forestove{name}.child anchor)\forestoption{edge label};}}
  [My Family Tree
    [father 
        [Paternal Gfather 
                [Paternal GGfather] 
                [Paternal GGmother]] 
        [Paternal Gmother 
                [Maternal GGfather] 
                [Maternal GGmother]]]
    [mother 
        [Maternal Gfather]
        [Maternal Gmother]]] 
  \end{forest}
\end{document}

enter image description here

share|improve this answer

Try adding a level distance to your level settings:

level 1/.style={sibling distance=\treeheight/2,level distance=\treeheight/6},
level 2/.style={sibling distance=\treeheight/4,level distance=\treeheight/8},
level 3/.style={sibling distance=\treeheight/8,level distance=\treeheight/4}]

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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