[Tex/LaTex] Tikz-Tree: Arrow-style depending on parent node

tikz-pgftikz-trees

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?

Best Answer

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

Related Question