TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How do I align vertical trees to to the top?

This is my current tree:       I want to get this tree:
^ That's a part of my current tree

I'm trying to create a graph which illustrates a path algorithm of O(N!). For readability, I only show descendants for the top node, all of the lower nodes should be postfixed by a fork- / rake-like object.

I'd appreciate an answer which shows the correct solution, plus an explanation of the used code.

My relevant code:

\tikzset{grow'=right,level distance=50pt}
\tikzset{execute at begin node=\strut}
\tikzset{every tree node/.style={align=left,anchor=base west}}
\Tree [.\node(root){E};
           [.A \node(x){R};
               [.D ]
               [.U ]
               [.T ]
           [.R ]
           [.D ]
           [.U ]
           [.T ]
      [.R ]
      [.D ]
      [.U ]
      [.T ]
\draw[->](x)..controls +(north west:1.5) .. (root);
share|improve this question
I get centered dots after R if I write [ .R{\,$\cdots$} ] in the descendents of E. – egreg Jan 13 '12 at 11:32
up vote 7 down vote accepted

Perhaps a tree is not the right way to go for what you want to achieve. This example uses a matrix to place the nodes in the desired way.


    \matrix[matrix of nodes,column sep=2em,row sep=1em] (tree) {
      E & A & R & D & U & T \\
        & R & D & U & T &   \\
        & D & U & T &   &   \\
        & U & T &   &   &   \\
        & T &   &   &   &   \\
    \foreach \n in {1,2,3,4,5} {\path (tree-1-1) edge (tree-\n-2);};
    \foreach \n in {1,2,3,4} {\path (tree-1-2) edge (tree-\n-3);};
    \foreach \n in {1,2,3} {\path (tree-1-3) edge (tree-\n-4);};
    \foreach \n in {1,2} {\path (tree-1-4) edge (tree-\n-5);};
    \path (tree-1-5) edge (tree-1-6);
    \path (tree-1-6) edge[->,bend right=15] (tree-1-1);

The syntax is not as comfortable as typing a tree, but the result is what you want.

enter image description here

share|improve this answer
Thanks for the answer. How can I add a fork after the other letters (looks like a <-, with the hyphen slightly shifted to the left`)? – Rob W Jan 13 '12 at 12:50
You could construct the fork with \newcommand*\myfork{\ensuremath{{-\mkern-20mu <}}} and then add that to the relevant nodes. To keep the alignment, set text width=1em in the \matrix options. – Alan Munn Jan 14 '12 at 17:54
@AlanMunn Thanks! What's the difference between \newcommand* and \newcommand? Also, how can I dynamically insert the fork, rather than typing \myforkseveral times? – Rob W Jan 15 '12 at 14:03
@RobW The * prevents the command from containing \par. See What's the difference between \newcommand and \newcommand*? for the complete story. As for dynamically inserting the fork, I don't see a simple way since the nodes in the matrix aren't themselves dynamically created. You could define a command to insert an end node like the following: \newcommand*\endnode[1]{#1\myfork} and then use \endnode{R} etc. to insert the nodes you want to have the fork. That's semantically clearer, but doesn't save you much typing. – Alan Munn Jan 15 '12 at 14:16
I ended up using the fork as suggested by @AlanMunn. Instead of manually typing it all over again, I used two RegExes to pad the & and add the \fork. Thanks for your help! – Rob W Jan 15 '12 at 14:39

Your Answer


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.