4

Here is my tree:

\begin{figure}[H]
   \centering
   \begin{tikzpicture}[
            grow=right,
            edge from parent/.style={draw, -latex},
            level distance=20mm,
            sibling distance=15mm,
         ]
      \node[state, thickgray]{1}
         child[thickgray]{
            node[state, thickgray]{2}
               child{node[state]{3}}
         }
         child[thickgray]{node[state, thickgray]{4}}
         child{
            node[state, thickred]{5}
               child[thickred]{node[state, thickred]{6}}
               child{node[state]{7}}
         };
   \end{tikzpicture}
\end{figure}

Which currently looks like:

enter image description here

I want to colour the branch from 1 to 5 thickred, but if I put such parameter on the child{} that encompasses it, both 1 to 5 and 5 to 7 are coloured red. How can I isolate 1 to 5 (preferably while maintaining this way of constructing a tree, I want to avoid \tree for now).

1 Answer 1

1

You first need to set the root node and the first child that should be colored red to thickred. Then (since the styles in such a tree-like setting are inherited by the child nodes), lower in the hierarchy, set the first children that should be colored gray back to thickgray or reset their style (by overriding it with the default values):

\documentclass[border=1mm, tikz]{standalone}
\usetikzlibrary{trees}

\begin{document}

\begin{tikzpicture}[
            grow=right,
            edge from parent/.style={draw, -latex},
            level distance=20mm,
            sibling distance=15mm,
            state/.style={circle, draw},
            thickgray/.style={thick, gray},
            thickred/.style={thick, red},
            reset/.style={thin, black}
         ]
    \node[state, thickred]{1}
         child[thickgray]{
            node[state]{2}
               child{node[state]{3}}
         }
         child[thickgray]{node[state]{4}}
         child[thickred]{
            node[state]{5}
               child[reset]{node[state]{6}}
               child[reset]{node[state]{7}}
         };
\end{tikzpicture}
   
\end{document}

enter image description here

3
  • Oh...ok and since I want 7 to remain black (not thickgray), do I have to now go and define a black style to use as an override, or is there a better way to do that?
    – Five9
    Commented Dec 7, 2021 at 20:08
  • Yes, you would need to do this, because in such a tree, the styles are inherited. reset/.style={thin, black} would work, for example. Commented Dec 7, 2021 at 20:12
  • 1
    Thanks! 7 chars to go
    – Five9
    Commented Dec 7, 2021 at 20:31

You must log in to answer this question.

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