I'm working with trees of different depth, created with forest package, and I'd like to automatically colour their background with different shades of gray, starting from the same shade down to white, for example:
Tree-level tree
level 0: black!60 % darkest gray
level 1: black!30
level 2: black!0 % white
Four-level tree
level 0: black!60 % darkest gray
level 1: black!40
level 2: black!20
level 3: black!0 % white
Is there any way to get the maximum level of the tree and automatize the filling using the formula below?
darkest = 60 % parameter
level n: black!((maxlevel-level)*darkest/maxlevel)
My working code so far (with maxlevel manually set to 2, and darkest gray set to 40):
\documentclass{book}
\usepackage[edges]{forest}
\forestset{
AlignOddChildren/.style = {
for tree={
if={isodd(n_children())}{
calign primary child/.pgfmath={(n_children()+1)/2},
calign=child edge
}{}
}
},
}
\begin{document}
\begin{forest}
for tree = {draw, rectangle, thick, align=center},
forked edges,
for tree = {fill/.wrap pgfmath arg={black!#1}{40*(2-level())/2}},
AlignOddChildren
[ Maintenance
[ Corrective\\Maintenance\\(CM)
[ Run to failure\\(RTF) ]
]
[ Preventive\\Maintenance\\(PvM)
[ Experience-based\\Maintenance\\(EBM) ]
[ Time-based\\Maintenance\\(TBM) ]
]
[ Predictive\\Maintenance\\(PdM)
[ Condition-based\\Maintenance\\(CBM) ]
]
]
\end{forest}
\end{document}
I found somewhere this code for getting the maximum level, but I don't know how to use it (replacing maxlevel() in the expression does not work):
maxlevel/.max={level}{tree}
Thank you!
max
, section of 3.14 of the docs. The level of a node is stored inlevel
.max
.