# How to align parent nodes to the left/top in a tree

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)?

-
Welcome to TeX.SX! You may have a look on our starter guide. If you want, you can include your image, and remove the leading !. Then someone with enough reputation can edit it back in. –  mafp May 21 '13 at 15:05
@mafp Thanks! I think plain text is fine :) What is the leading !? –  user31039 May 21 '13 at 15:14
If you include an image, the markup looks like ![description][link], there is a ! in front of it. But now you have enough rep to do it yourself anyway, and your ASCII art explains it fine, indeed. Good question, btw. –  mafp May 21 '13 at 15:18
Perhaps this is a duplicate question? Horizontal hierarchy tree in tikz-qtree: bad layout for longer node-names. And for a more complex version see: How to draw a horizontal tree with branches on the right and left?. And for another version of a genealogical tree see Joining two branches of a family tree. –  Alan Munn May 21 '13 at 15:22
@AlanMunn No it's not. I've reviewed answers to these questions and although they talked about horizontal trees it seems that none of them provide the alignment to left/top option - the parents are still center-aligned. –  user31039 May 21 '13 at 16:16

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}


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:

-
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