2

I have drawn a tree in tikz with different styles for nodes (this simplified example has nodes with style a or b). The arrows from nodes to childs are always the same and I would like to apply different styles to the edge/arrow depending on the style of the parent node.

\begin{tikzpicture}[
    a/.style={circle, draw=black, edge from parent/.style=myEdge1},
    b/.style={rectangle, draw=red, edge from parent/.style=myEdge2},
    myEdge1/.code={
        \draw[-] \tikzparentnode -- \tikzchildnode;
    }
    myEdge2/.code={
        % No edge or applying a different style
    }
]
\node[a]{}
    child {node[a] {}}
    child {
        node[b] {no arrows from this node}
        child {node[a] {no arrow to this node}}
        child {node[b] {no arrow to this node}}
    }
    child {node[a] {}}
;
\end{tikzpicture}

This is how it is supposed to look:

example

First of all, the code above does not work when edge from parent/.style=myEdge1 is defined within a style (a or b). It only works when I put it right behind the child, i.e. child[edge from parent/.style=myEdge1]. Second (and this would probably solve the first issue), I would rather need edge to child than edge from parent since the style of the parent shall decide whether an arrow has to be drawn or not. But there is no such thing as edge to child in the tikz documentation. Is there an easier solution than telling each child whether it should have an arrow from its parent or not such that tikz automatically adopts the edge style from the style of the parent node?

1

You can set an option that only applies to the children, if you use the every child style.

The no edge from this parent style is setup in a way that the children of the child it is used on doesn’t draw the edge.

Instead of draw one you can set any other option an edge can have. This does not include a \draw command!

For edges in a tree there is a special key that sets the path: edge from parent path.

The default path is

(\tikzparentnode\tikzparentanchor) -- (\tikzchildnode\tikzchildanchor)

Code

\documentclass[tikz,convert=false]{standalone}
\begin{document}
\begin{tikzpicture}[
  no edge from this parent/.style={
    every child/.append style={
      edge from parent/.style={draw=none}}},
  a/.style={circle, draw=black},
  b/.style={rectangle, draw=red},
]
\node[a] {}
  child {node[a] {}}
  child[no edge from this parent] {node[b] {no}
    child {node[a] {no}}
    child {node[b] {no}}
  }
  child {node[a] {}}
;
\end{tikzpicture}
\end{document}

Output

enter image description here

  • Thanks you, it's working perfectly. Unfortunately I can't apply a different edge style to the pointers from the root node in the same way I do it for children (e.g. when the root node is a b-node). I'd have to write \begin{tikzpicture}[edge from parent/.style={draw=none}] and overwrite all the styles of a-nodes. – user34321 Aug 13 '13 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy