1

I was wondering if there is a way to change the color of a subtree with qtree. I saw there is a post that you can do this in forest, but I don't know if there is a way to do it on qtree or tikz-qtree. I paste below my first try: a tree with all the nodes in gray, but what I want is to also have the branches in gray. Can I use xcolor to change the color of subtrees? Thanks! MWE:

  \usepackage{qtree}

\usepackage{xcolor}

\begin{document}

\Tree [.A B [.C   D [.{\color{gray} E}  {\color{gray} F}  {\color{gray} G}  ] ] ]

\end{document}

1 Answer 1

0

With qtree this is actually rather tricky. This particular tree is constructed from the left branching nodes top to bottom (B-0, D-1 and F-2), then the right leaf node of the deepest subtree (G-3) is drawn, then the head node of that subtree (E-4), and then the branches of that subtree (5 and 6). Subsequently this process is repeated for the upper parts of the tree, so first the head node and then the branches. See the following tree annotated for the drawing order:

tree annotated with drawing order

In general you can add a \color statement without surrounding braces to switch the color for all the following content (nodes and branches) until the next \color statement is encountered. Concretely, this means you set the color to gray before the subtree, and set it back to black after the subtree. However, you need to take the drawing order into consideration to find out where exactly the \color statements need to be added.

It is important to use the \qtree syntax with the ! character for escaping the tree parser, i.e., !\color{gray}, instead of just \color{gray} which is interpreted as part of the node contents.

Now, the color switch should be placed before the F node (draw index 2) and switched back after the bracket that closes the E subtree (draw index 6).

MWE:

\documentclass{article}
\usepackage{qtree}
\usepackage{xcolor}
\begin{document}
\Tree [.A B [.C D [.E !\color{gray} F G ] !\color{black} ] ]
\end{document}

Result:

tree with colored subtree

1
  • Thank you very much! Just what I need it!
    – crealp
    Nov 16, 2022 at 7:43

You must log in to answer this question.

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