1

I have a specific label style (named "aux" in the code below). This style sets color to "gray" and I am trying to figure out how to properly change the color of the style to "blue" only in the leaf nodes of the tree (to remark that labels). I am able to easily change the color of the leaf nodes (to red) using a where clause, however the same does not seem to work for the style of the label.

My base code is:

\begin{forest}
for tree={
s sep=10mm,l=10mm,
circle,draw,
minimum size=1.5em,
inner sep=1pt,
aux/.style={ label={[left,font=\tiny,gray]:{#1}}, },
where n children=0{delay={color=red, aux/.style={ label={[left,font=\tiny,blue]:{#1}}, }, content={#1 }}}{}}
[0,
[a,aux=0]
[1,aux=1,
[2,aux=10, [\text{N},rectangle,rounded corners,aux=100],[c,aux=101]]
[b,aux=11]]
]
\end{forest}

The output can be seen in the next figure:

enter image description here

1
  • 1
    Your chances of getting an answer would improve significantly if you would add a minimal working example (MWE) of your existing attempt, starting from \documentclass an ending with \end{document}. (see also the related meta question) Commented May 1, 2021 at 0:34

1 Answer 1

2

Based on the brute force code below, a possible solution is rather simple: add the if condition to the aux style instead of using some delay to redefine aux.

\documentclass{article}
\usepackage{amsmath}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={
s sep=10mm,l=10mm,
circle,draw,
minimum size=1.5em,
inner sep=1pt,
aux/.style={if n children=0{label={[left,font=\tiny,blue]:{#1}}}%
{label={[left,font=\tiny,gray]:{#1}}}},
if n children=0{delay={color=red,content={#1}}}{}}
[0,
[a,aux=0]
[1,aux=1,
[2,aux=10, [\text{N},rectangle,rounded corners,aux=100],[c,aux=101]]
[b,aux=11]]
]
\end{forest}
\end{document}

enter image description here

Here is a much more brutal but almost equivalent way of doing this.

\documentclass{article}
\usepackage{amsmath}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={
s sep=10mm,l=10mm,
circle,draw,
minimum size=1.5em,
inner sep=1pt,
aux/.code={\pgfmathtruncatemacro{\ic}{n_children}%
\ifnum\ic=0
\forestset{label={[left,font=\tiny,blue]:{#1}}}
\else
\forestset{label={[left,font=\tiny,gray]:{#1}}}
\fi
},
if n children=0{delay={color=red,content={#1}}}{}}
[0,
[a,aux=0]
[1,aux=1,
[2,aux=10, [\text{N},rectangle,rounded corners,aux=100],[c,aux=101]]
[b,aux=11]]
]
\end{forest}
\end{document}

You must log in to answer this question.

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