我正在处理用森林包创建的不同深度的树,我想用不同的灰色阴影自动为它们的背景着色,从相同的阴影开始到白色,例如:
树级树
级别 0:黑色!60 % 最暗灰色
级别 1:黑色!30
级别 2:黑色!0 % 白色
四级树
级别 0:黑色!60 % 最暗灰色
级别 1:黑色!40
级别 2:黑色!20
级别 3:黑色!0 % 白色
有没有办法获得树的最大级别并使用以下公式自动填充?
最暗 = 60%参数
级别 n:黑色!((maxlevel-level)* darkest / maxlevel)
我迄今为止的工作代码(最大级别手动设置为 2,最深灰色设置为 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}
我在某处找到了这个用于获取最高级别的代码,但我不知道如何使用它(在表达式中替换 maxlevel() 不起作用):
maxlevel/.max={level}{tree}
谢谢你!
答案1
您可以使用.max
来获取树的最大值level
并将其存储在临时寄存器中。然后可以在表达式中使用它来指定fill
。
例如,
\documentclass[border=10pt]{standalone}
\usepackage[edges]{forest}
\forestset{
AlignOddChildren/.style = {
for tree={
if={isodd(n_children())}{
calign primary child/.process={Ow+n {n children}{(##1+1)/2}},
calign=child edge
}{}
}
},
gradual fill/.style={
before typesetting nodes={
tempcounta/.max={level}{r,tree},
for tree={
fill/.process={
ROw2+nw {tempcounta} {level} { (100*##2)/##1 } {white!##1!black!60}
},
},
},
},
}
\begin{document}
\begin{forest}
for tree = {draw, rectangle, thick, align=center},
forked edges,
gradual fill,
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}
\begin{forest}
for tree = {draw, rectangle, thick, align=center},
forked edges,
gradual fill,
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}