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:
[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).
\node \ifnum#1=1 [style 1]\fi {<label>};
works fine. I think that you might get better answers if you made your question a little more concrete: What are the styles going to change, are they always going to be used in a loop, how many different options will you need? This can probably be solved more elegantly if the problem is described more concretely.\providetikzstyle
similar to\providecommand
?, although it really should be\providetikzset
based on Should\tikzset
or\tikzstyle
be used to define TikZ styles?.