[Tex/LaTex] Choosing styles conditionally in TikZ

conditionalstikz-pgftikz-styles

I want to create a series of images that visualise a recursion tree as walked on by an algorithm. That means that different styles are applied to nodes depending on the step the algorithm is in, such as hiding, highlighting, etc. Consider this example:

example
[source]

As you can see, some styles are (not) applied "from image i on"; for instance, hidden nodes become unvisited (grayed out) and then normal, but never change back. Other styles are applied more flexibly, for example the yellowish highlighting active (which shows what node the visualised algorithm currently investigates). Above image was created by explicitly writing code for six images. Naturally, I want to avoid duplicating code as much as possible.

I thought that the following should be a straightforward way to achieve this:

\tikzset{<TikZ style definitions>}
\newcommand{\image}[1]{<TikZ image code>}
\begin{document}
  \foreach \n in {1,...,<N>}{%
    \image{\n}
  }
\end{document}

Now, \image has to choose styles depending on parameter #1 (i.e. \n); I am fine with hardcoding which style is applied on which image numbers.

But neither

\node \ifthenelse{<condition>}{[style=<name>]}{} {<label>};

nor

\node[\ifthenelse{<condition>}{style=<name>}{}] {<label>};

works. So I tried to use ifthenelse inside the (now parametric) style definition, which also failed, and then put

\tikzset{%
  test/.code={\ifthenelse{<condition>}{style=<name>}{}}
}

in the preamble, together with

\node[test=<param>] {<label>};

in the image; this does not work, either.

How can I choose TikZ styles conditionally?

A feasible workaround might be to create the nodes conditionally (i.e. duplicating node definitions for each case), but this is nasty for trees. Therefore, I would prefer a more surgical solution.

I use xifthen for conditions, in particular \isin. For example, I try assigning style active by checking

\ifthenelse{\isin{!#1!}{!1!2!4!6!}}{<set style active>}{<no style>}

in the case of root node a, where #1 is the parameter of \image. Ideally, I would be able to put such a statement at every affected node, that is the information which style is applied on which image is defined locally at the styled elements (for all images at once).

Best Answer

A really simple approach would probably be to use the image number directly to index the style:

\documentclass{article}

\usepackage{tikz}

\tikzset{
    image/.cd,
    1/.style={fill=orange},
    2/.style={fill=yellow},
    3/.style={fill=cyan}
}

\newcommand{\image}[1]{
    \tikz{ \draw [image/#1] circle [radius=1cm];}
}

\begin{document}
  \foreach \n in {1,...,3}{%
    \expandafter\image\expandafter{\n}
  }
\end{document}

However, as Jake pointed out, a more complex selection scheme is probably wanted by the OP, who in particular mentioned a "parametric style". The following uses Jake's example, but moves the testing from the macro to the style definition:

\documentclass{article}

\usepackage{tikz}
\usepackage{xifthen}

\tikzset{
    image/.cd,
    1/.style={fill=orange},
    2/.style={fill=yellow},
    3/.style={fill=cyan},
    test/.code={%
      \ifthenelse{\isin{#1}{ABC}}{\tikzset{image/1}}{
      \ifthenelse{\isin{#1}{DEF}}{\tikzset{image/2}}{
      \ifthenelse{\isin{#1}{GHI}}{\tikzset{image/3}}{
      }}}
    },
}

\newcommand{\image}[1]{
    \tikz{ \draw [image/test=#1] circle [radius=1cm];}
}

\begin{document}
  \foreach \n in {A,E,G}{%
    \expandafter\image\expandafter{\n}
  }
\end{document}

enter image description here