6

Is there an elegant way to color subtrees in tikz-qtree, without going through each of the respective nodes and edges by hand?

The following code example shows the ugly way:

\documentclass{standalone}           
\usepackage{tikz-qtree}  
\begin{document}

\tikz{  
\Tree 
[.A 
    [.B D ]
    [.\node[text=red]{C}; 
        \edge[draw=red];\node[text=red]{E}; 
        \edge[draw=red];\node[text=red]{F}; ]   
]
}

\end{document}

subtree-red

Instead I would like to fix the color of a subtree just in one place, for example at its root node (hence at 'C' in the code example). Any suggestions?

  • 1
    I don't have tikz-qtree installed, but can you use a \begin{scope}...\end{scope} within the tree? – Turion Aug 28 '14 at 17:18
  • 1
    Do you mean \tikzset{every level 1 node/.style={text=red}}? – user11232 Aug 29 '14 at 0:37
  • @Turion: no, the scope environment is not to be used inside tikz-qtree. – Timm Aug 29 '14 at 21:48
  • @Harish: no, this only colors B and C, but not the subtree under C. – Timm Aug 29 '14 at 21:53
3

There is a fairly simple way to color subtrees using the forest package, which has a similar syntax, but is more flexible than tikz-qtree. Here is an MWE:

\documentclass{standalone}           
\usepackage{forest} 
\begin{document}

\forestset{
    sn edges/.style={for tree={parent anchor=south, child anchor=north}},
    red subtree/.style={for tree={text=red},for descendants={edge=red}}}
\begin{forest}
sn edges
[A
    [B [D]]
    [C,red subtree
        [E] 
        [F]]   
]
\end{forest}

\end{document}

enter image description here

| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.