5

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:

         Check
           A
           |
    reject | accept
    ---------------
   |              |
 Check        decision 1
   B

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)

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{trees,positioning,arrows,shapes,backgrounds}
\begin{document}
\begin{tikzpicture}[
% 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}
    }
;
\end{tikzpicture}
\end{document}

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?

1
  • 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, 2013 at 14:56

1 Answer 1

4

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.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{trees,shapes}
\begin{document}
\begin{tikzpicture}[
    test/.style={
        % Node 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}  
        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} } %
;
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .