# How to automatically assign a range of colors to children of a single mindmap node?

In mindmap we define each child node ourselves, but I like to specify some of the attributes of child nodes in their parents, one of the most important attributes is their color. For this, we can say what should be the color of nodes at a specific level. However, I would like to assign a range of colors to a set of children of a specific node, for instance, I like to say the start and end color and then tikz automatically create a gradient color list and assign it to the children, or we define a color list, then the parent picks arbitrary colors for its children from that color list. Coloring is one example, another attribute might be the shape of children, we might define a specific list of possible shapes and ask to assign each child one of the forms arbitrarily (every child looks different).

Is there a trick to do so? Currently either I specify the color of each child node or I'm leaving all of them to have the same color.

For normal tikz nodes, when we define the nodes themselves automatically, e.g. with a for loop, it is possible to assign automatic colors/shapes by employing techniques like the one described here, but this clearly doesn't generalize to mindmap as children and their text are usually not generated automatically.

It is rather easy. At the moment TikZ looks at a child, two relevant numbers are known:

• \tikznumberofcurrentchild is the index of the current child,
• \tikznumberofchildren is the number of children, i.e. siblings.

This counter can be used for the purposes you are describing. Here is an example for the colors, shapes can be done very similarly.

\documentclass[tikz,border=3mm]{standalone}

\usetikzlibrary{mindmap}
\begin{document}
\begin{tikzpicture}[auto concept color/.code 2 args={%
\ifnum\tikznumberofchildren=1\relax
\pgfmathtruncatemacro{\mycf}{50}%
\else
\pgfmathtruncatemacro{\mycf}{100*(\tikznumberofcurrentchild-1)/(\tikznumberofchildren-1)}%
\fi
\tikzset{concept color=#1!\mycf!#2}
}]
\path[mindmap,concept color=black,text=white,
every child/.append style={auto concept color={blue}{red}}] node[concept] {M}
[clockwise from=0]
child {node[concept] {A}}
child { node[concept] {B}}
child { node[concept] {C} }
child { node[concept] {D} };
\end{tikzpicture}
\end{document}


• Thanks a lot, this was amazing! May 5, 2021 at 21:02