# visually distinguish right and left children

I'm using the qtree package to render small inline trees in my document. The tree nodes in my case are limited to 0 or 2 child nodes. I'd like to visually distinguish the right and left branches somehow. Ideally with a red dashed line one one side and green solid line on the other, or possibly a bubble on one, however almost any clear visual feature would suffice.

Why do I want this? Because other places in my larger document I'm using more complex ways of drawing much larger trees. In these cases I always distinguish the two children with a red-dashed line and a green solid line. And in my text I refer to the two children as the "positive" child and "negative" child, never the left and right (as sometimes graphviz swaps the right and left to minimize crossing lines). I'd like to remove all mention of right and left for these graphs, but I can't because of this seeming limit of qtree.

Can someone suggest an fix?

as far as MWE consider the following.

\documentclass{article}
\usepackage{qtree}
\newcommand\bdd[1]{%
{\scriptsize\begin{tabular}[c]{@{}l@{}}$\Tree[#1]$\end{tabular}}}
\newcommand*\YLR{\bdd{.Y L R }}
\begin{document}
Simple tree \YLR and another
{\scriptsize \begin{tabular}[c]{@{}l@{}}
$\Tree[.non-string [.A ] [.B [.not-number [.L ] [.R ] ] !{\qframesubtree} [.\ C ] ] ]$
\end{tabular}} less simple.
\end{document}


Here is how it current renders.

Qtree encapsulates the drawing of the branch lines in the function \qdraw@branches, precisely for the purpose of allowing it to be overridden. Since you only use binary branching, you can start with the following definitions and add color to taste to the two-branching case. (The function always draws a picture one unit wide; the unit will be scaled to any width needed.)

\makeatletter
\def\qdraw@branches#1{\ifcase#1\relax % Zero case is unused
\or  % One-branching
\begin{picture}(0,1)
\put(0,0){\line(0,1){1}}
\end{picture}%
\or % Two-branching
\begin{picture}(2,0.5)
\put(0,0){\line(2,1){1}}
\put(2,0){\line(-2,1){1}}
\end{picture}%
\else\typeout{Qtree error--- Can't handle #1-way branching}
\fi}

• Hi Alexis, can you please elaborate? How can I use your proposal for getting a red-dashed line for the left branch, and a green-solid line for the right branch? – Jim Newton Nov 28 '18 at 9:55

If you're committed to dashed lines, there isn't really a simple way to do this with qtree because the pict2e package which draws the trees doesn't have a simple way to draw dashed lines. If you just want different coloured edges, this is easy to do using the method Alexis suggested. Here's a full example using qtree:

\documentclass{article}
\usepackage{xcolor}
\usepackage{qtree}
\makeatletter
\makeatletter
\def\qdraw@branches#1{\ifcase#1\relax % Zero case is unused
\or  % One-branching
\begin{picture}(0,1)
\put(0,0){\line(0,1){1}}
\end{picture}%
\or % Two-branching
\begin{picture}(2,0.5)
\put(0,0){\color{red}\line(2,1){1}}
\put(2,0){\color{green}\line(-2,1){1}}
\end{picture}%
\else\typeout{Qtree error--- Can't handle #1-way branching}
\fi}
\begin{document}
\Tree [.A [.B [.C ] [.D ] ] [.E ] ]
\end{document}


For dashed lines and much more flexibility, you might consider switching to tikz-qtree which for the most part is a drop-in replacement for qtree in terms of syntax.

Here's an example adapting the method described in this answer to conditionally change the edge options for the left and right edges.

\documentclass{article}

\usepackage{tikz-qtree,tikz-qtree-compat}

\newif\ifleft\lefttrue
\tikzset{
edge options/.code={
\ifleft
\tikzset{red,dashed}
\global\leftfalse%
\else
\tikzset{green}
\global\lefttrue%
\fi}
}

\makeatletter