8

I am drawing trees in LATEX. The qtree library seems to be what I need but the parent is always placed in the center of its children like this:

    parent
    /    \ 
child_1 child_2

(I'm a new user and does not have enough reputation to post an image.)

What I want is a right-growing tree like this:

root  -- child_1  -- grandchild_1_1
      \- child_2
      \- child_3  -- grandchild_3_1
                  \- grandchild_3_2 -- great-grandchild_3_2_1
      \- child_4  -- grandchild_4_1

Rotating the qtree to right-growing is insufficient since the parent nodes are then vertically aligned to the center of their children, but I need them to remain on the top of their subtrees.

I've tried trees library of TikZ, which allows customized "growth function" to specify the position of children. However it does not consider the size of subtrees, so in this case child_4 would follow immediately under child_3, making grandchild_4_1 overlap with grandchild_3_2.

Is there a simple way to just "disable" the centering of parents in qtree? Or is there any better solution (may be writing my own macro)?

user31039
  • 197

2 Answers2

9

I'm not sure if this is what you want, but here's a solution using the forest package. It provides an alignment parameter for aligning the children of a node which does most of what you want. I didn't know whether you wanted square edges or not, but they seem more appropriate for this kind of tree, (in the screen shot image the lines look odd, but this is an artefact of the screen rendering). I've added a simple alternative that looks a bit nicer without the squared edges.

\documentclass{article}
\usepackage{forest}
\begin{document}

\begin{forest}
grow right/.style={for tree={%
  calign=last,
  grow=east,
  ,s sep=.5cm,
  parent anchor=east,
  child anchor=west,
  edge path={\noexpand\path[\forestoption{edge}] 
     (!u.parent anchor) -- +(0pt,-10pt) |- (.child anchor)
     \forestoption{edge label};}
  }
}
,grow right
[Root [Child1 ] 
      [Child2
         [GChild1 ]
         [Gchild2 ]
         [GChild3 ]
      ] 
      [Child3
         [Gchild4 ]
         [Gchild5 ]
      ]
]
\end{forest}
\hfill
\begin{forest}
[Root,for tree={calign=last,grow=east,draw, parent anchor=east,child anchor=west} [Child1 ] 
      [Child2
         [GChild1 ]
         [Gchild2 ]
         [GChild3 ]
      ] 
      [Child3
         [Gchild4 ]
         [Gchild5 ]
      ]
]
\end{forest}
\end{document}

output of code

Edit: Notice that the children are organized in a reversed order, due to the counter-clockwise coordinates. Using the options calign=first, reversed=true in the for tree = {...} block produces the exact tree needed, as shown below:

order corrected

user31039
  • 197
Alan Munn
  • 218,180
  • Thanks! It works well (quite a new package, it wasn't in my repository before updated), and I've edited your answer to correct the ordering of child nodes after reading the manual. – user31039 May 21 '13 at 21:08
3

This is just a supplement to Alan Munn's answer which uses the edges library and other features of the current version of Forest. This therefore requires version 2+.

\documentclass[tikz,border=10pt,multi,rgb]{standalone}
\usepackage[edges]{forest}
\begin{document}
\begin{forest}
  for tree={
    grow'=0,
    parent anchor=children,
    child anchor=parent,
    anchor=parent,
    if n children=0{folder}{},
    edge path'={(!u.parent anchor) -- ++(5pt,0) |- (.child anchor)},
  },
  where n=1{
    calign with current edge
  }{},
  [Root
    [Child1]
    [Child2
      [GChild1]
      [Gchild2]
      [GChild3]
    ]
    [Child3
      [Gchild4 ]
      [Gchild5 ]
    ]
  ]
\end{forest}
\end{document}

This code produces the squared-edge version of the tree:

squared edges

I think the small offset at the start of the path, prior to the vertical line, looks a bit neater, but this could be eliminated if preferred by changing the definition of the edge path to

edge path'={(!u.parent anchor) |- (.child anchor)}, 

squared edges without offset

Alternatively, deleting this line altogether results in a tree using the default path:

non-squared default edges

cfr
  • 198,882