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

I'm new to TikZ and have a problem with a decision tree. In the example below I would like to have text above the horizontal line of an edge. Something like:

    reject | accept
   |              |
 Check        decision 1

When I try to use something like

 edge from parent node[above] {accept}

The tree gets totally messed. (example commented out in the code below)

% Node style
    test/.style={diamond, aspect=2.5,very thick,draw=black,fill=gray!20,text   width=1.1cm,
    align= center, anchor=north},
   dec/.style={rectangle,very thick,draw=black,fill=gray!50,text width=2cm,
    text centered, anchor=north},
% Children and edges style
    edge from parent/.style={very thick,draw=black},
    edge from parent fork down,
    level 1/.style={sibling distance=4cm,level distance=2cm}

\node (t1) [test] {Check\\A} 
    child{node (t2) [test] {Check\\B} % edge from parent node[above] {reject}
         child{node (t3) [test] {Check\\C} 
               child{node (t4) [test] {Check\\D}}
               child{node (d3) [dec] {decision 3}}
      child{node (d2) [dec] {decision 2}}
    child{node (d1) [dec] {decision 1} % edge from parent node[above] {accept}

Then I was wondering also if it is possible to force the horizontal line of the edge to be in the middle between the parent and its children. And if the nodes could be such that their centers have equal height?

share|improve this question
I'm not quite sure what you mean by your question about the line of the edge, but if you change the node style anchors to center instead of north the node centres will line up. Is this what you want? – Alan Munn Apr 25 '13 at 14:56

For reasons I'm not sure I fully understand, the edge from parent node seems to have to go at the end of the child group before the closing }

EDIT: The manual (section 18.6 "Edges From the Parent Node" in the 2.10 version) states that

This path operation can only be used inside child paths and should be given at the end, possibly followed by node specifications

and gives some further examples.

Anyway, the following appears to do what you require. Hopefully the example demonstrates how it should be used better than the explanation did.

        % Node style
        very thick,
        text width=1.1cm,
        very thick,
        text width=2cm,
        text centered, 
    % Children and edges style
    edge from parent/.style={
        very thick,
    edge from parent fork down,
    level 1/.style={
        sibling distance=4cm,
        level distance=2cm}

\node (t1) [test] {Check\\A} 
    child { node (t2) [test] {Check\\B}  
        child { node (t3) [test] {Check\\C} 
            child { node (t4) [test] {Check\\D} edge from parent node [above] {reject C} }
        child { node (d3) [dec]  {decision 3} edge from parent node [above] {accept C} }
            edge from parent node[above] {reject B}
        child { node (d2) [dec] {decision 2}  edge from parent node[above] {accept B} }
            edge from parent node[above] {reject A}
    child{ node (d1) [dec] {decision 1} edge from parent node[above] {accept A} } %

enter image description here

share|improve this answer

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.