# TikZ/Forest: How to uniquely color parent/children/grandchildren branch nodes?

Following my previous question, in this MWE, how to, for example, set a unique color for each parent/children/grandchildren branch in such a way that the color is gradually fainting or brightening while going downwards?

In other words, if I want to set blue color for the nodes Parent 1, Child 1 and Grandchild 1, I would like make their fill colors be blue!80, blue!50 and blue!20, respectively. Similarly, I would to set other colors for the other branches, and the respective nodes should be colored in the same way.

\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage[edges]{forest}
\begin{document}
\newlength\gap
\setlength\gap{10mm}
\begin{forest}
forked edges,
for tree={
draw=blue!80!darkgray,
fill=blue!80!darkgray!25,
rounded corners,
minimum width=(\textwidth-6*\gap)/3,
minimum height=4ex,
edge={-Latex},
font=\sffamily,
text centered,
},
where={level()<=1}{%
parent anchor=children,
l sep+=10pt,
s sep'+=10pt,
}{%
folder,
grow'=0,
l sep+=0pt,% length of edge to grand child
if level=2{%
before typesetting nodes={child anchor=north},
!u.s sep'+=10pt,
edge path'={%
(!u.parent anchor) -- ++(0,-10pt) -| (.child anchor)
},
}{},
}
[Grandparent
[Parent 1
[Child 1
[Grandchild 1]
]
]
[Parent 2
[Child 2
[Grandchild 2]
]
]
[Parent 3
[Child 3
[Grandchild 3]
]
]
]
\end{forest}
\end{document}


• Haven't you asked this question before? :) TikZ/Forest: How to format/stylize a node based on its level?. The technique outlined in great detail there works here too I think. – Alan Munn Oct 28 '16 at 21:48
• I am not sure, but in my other question, the stylization is based on the level globally across all the branches. However, here in addition to level-based stylization, I need every branch (parent/children/grandchildren) to be uniquely colored. – Diaa Oct 28 '16 at 22:22
• Ok, then I misunderstood the question. The principle will be the same, but you may need to use a node walk, I think. – Alan Munn Oct 28 '16 at 22:30
• The forest or tikz manual is not friendly at least for me, but I will try to give them a look again. – Diaa Oct 28 '16 at 22:41
• It's a pretty daunting manual for sure. But we have @cfr to interpret it. :) – Alan Munn Oct 28 '16 at 22:43

Here's a different version (slightly less complicated):

\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage[edges]{forest}
\begin{document}
\newlength\gap
\setlength\gap{10mm}
\forestset{parent color/.style args={#1}{
{fill=#1},
for tree={fill/.wrap pgfmath arg={#1!##1}{1/level()*80},draw=#1!80!darkgray}},
root color/.style args={#1}{fill={{#1!60!gray!25},draw=#1!80!darkgray}}
}
\begin{forest}
forked edges,
for tree={%
rounded corners,
minimum width=(\textwidth-6*\gap)/3,
minimum height=4ex,
edge={-Latex},
font=\sffamily,
text centered,
},
where={level()<=1}{%
parent anchor=children,
l sep+=10pt,
s sep'+=10pt,
}{%
folder,
grow'=0,
l sep+=2pt,% length of edge to grand child
if level=2{%
before typesetting nodes={child anchor=north},
!u.s sep'+=10pt,
edge path'={%
(!u.parent anchor) -- ++(0,-10pt) -| (.child anchor)
},
}{},
},
[Grandparent,root color={brown}
[Parent 1, parent color={red}
[Child 1
[Grandchild 1]
]
]
[Parent 2,parent color={green}
[Child 2
[Grandchild 2]
]
]
[Parent 3,parent color={yellow}
[Child 3
[Grandchild 3]
]
]
]
\end{forest}
\end{document}


• Thanks for your answer. For alignment with children, I don't understand why the parents are aligned with children left instead of center. I would be grateful, if you could help me with this issue in my question. – Diaa Oct 29 '16 at 6:46

You might use the following style branch shade=from <colour> to <colour> to shade the branches as follows.

[Edited to support colours such as blue!80 which leave the specification of white as the second colour implicit.]

The new version of the code also creates a family tree style for trees which should follow this structural pattern. twist=<integer> sets the level at which the change should take place, so that children in later levels will be set folder style i.e. directory-like. The default is 2.

As Alan Munn indicated, this is really the same as before (which also implicitly relied on a nodewalk via, for example, the .max handler).

Note that

l sep+=0pt,


has no effect on anything whatsoever. It adds a length of zero to a dimension, which is obviously equivalent to nothing at all.

Then we can write

\begin{forest}
family tree,
[Grandparent, left color=cyan, right color=SpringGreen, middle color=Pink, draw=Silver
[Parent 1, branch shade=from cyan to blue
[Child 1
[Grandchild 1]
]
]
[Parent 2, branch shade=from Pink to WildStrawberry
[Child 2
[Grandchild 2]
]
]
[Parent 3, branch shade=from SpringGreen to ForestGreen
[Child 3
[Grandchild 3]
]
]
]
\end{forest}


to produce the tree created by the version of the code in the original version of this answer.

As you can see, I had no idea what to do with the root.

Or we can write

\begin{forest}
family tree,
[Grandparent, fill=darkgray, text=Silver, double=Silver, draw=darkgray
[Parent 1, branch shade=from blue!80 to blue!20
[Child 1
[Grandchild 1]
]
]
[Parent 2, branch shade=from WildStrawberry!80 to WildStrawberry!20
[Child 2
[Grandchild 2]
]
]
[Parent 3, branch shade=from ForestGreen!80 to ForestGreen!20
[Child 3
[Grandchild 3]
]
]
]
\end{forest}


to make use of white implicitly and style the root node differently.

Complete code:

\documentclass[border=10pt,multi,tikz,dvipsnames,svgnames,rgb]{standalone}
\usepackage[edges]{forest}
\forestset{
declare dimen register=gap,
gap'=10mm,
declare count register=twist,
twist'=2,
family tree/.style={
forked edges,
for tree={
rounded corners,
minimum width/.wrap pgfmath arg={##1}{(\textwidth-6*(gap))/3},
minimum height=4ex,
edge={-Latex},
font=\sffamily,
text centered,
edge=thick,
},
where={level()<(twist)}{%
parent anchor=children,
l sep+=10pt,
s sep'+=10pt,
}{%
folder,
grow'=0,
l sep'+=2pt,
if={level()==(twist)}{%
before typesetting nodes={child anchor=north},
!u.s sep'+=10pt,
edge path'={%
(!u.parent anchor) -- ++(0,-10pt) -| (.child anchor)
},
}{},
},
},
branch shade/.style args={from #1 to #2}{
before typesetting nodes={
tempcountc/.max={level}{current,tree},
tempcountb/.option=level,
tempcounta=(tempcountc)-(tempcountb)+1,
temptoksa/.option=name,
TeX/.wrap pgfmath arg={
\colorlet{##1col1}{#1}
\colorlet{##1col2}{#2}
}{name()},
for tree={
rounded corners,
top color/.wrap 2 pgfmath args={##2col2!##1!##2col1}{100*((level()-(tempcountb))/(tempcounta))}{(temptoksa)},
+edge/.wrap 2 pgfmath args={##2col2!##1!##2col1}{100*((level()-(tempcountb))/(tempcounta))}{(temptoksa)},
bottom color/.wrap 2 pgfmath args={##2col2!##1!##2col1}{100*((level()-(tempcountb)+1)/(tempcounta))}{(temptoksa)},
draw/.wrap 2 pgfmath args={##2col2!##1!##2col1}{100*((level()-(tempcountb)+1)/(tempcounta))}{(temptoksa)},
thick,
},
}
},
}
\begin{document}
\begin{forest}
family tree,
[Grandparent, left color=cyan, right color=SpringGreen, middle color=Pink, draw=Silver
[Parent 1, branch shade=from cyan to blue
[Child 1
[Grandchild 1]
]
]
[Parent 2, branch shade=from Pink to WildStrawberry
[Child 2
[Grandchild 2]
]
]
[Parent 3, branch shade=from SpringGreen to ForestGreen
[Child 3
[Grandchild 3]
]
]
]
\end{forest}
\begin{forest}
family tree,
[Grandparent, fill=darkgray, text=Silver, double=Silver, draw=darkgray
[Parent 1, branch shade=from blue!80 to blue!20
[Child 1
[Grandchild 1]
]
]
[Parent 2, branch shade=from WildStrawberry!80 to WildStrawberry!20
[Child 2
[Grandchild 2]
]
]
[Parent 3, branch shade=from ForestGreen!80 to ForestGreen!20
[Child 3
[Grandchild 3]
]
]
]
\end{forest}
\end{document}

• Many thanks for your decent answer. If you don't mind, I would like to get your explanations about some stuff: 1- when I tried to make branch shade = from blue!80 to blue!20, I got an error. is there any missing package to be loaded? 2- For the Grandparent node, I tried to make it uniformly filled with one color by replacing left color=cyan, right color=SpringGreen, middle color=Pink with color=white, but the result was not what I wanted. – Diaa Oct 29 '16 at 5:45
• @DiaaAbidou Maybe you want fill=white for the grandparent? color=white will affect the colour of the text, border and fill. If you want the node drawn in white also, use fill=white, draw=white. For the branch colouring, try branch shade=from blue!80!white to blue!20white or you could change the style definition to take only one colour. I did it this way because it is a bit more flexible if it allows 2 colours. What's happening is you are getting blue!80!<number>!blue!20` which won't work. Actually, let me do something else. Hang on. – cfr Oct 29 '16 at 21:01
• @DiaaAbidou Please see edited answer above. – cfr Oct 29 '16 at 21:44