Take the 2-minute tour ×

I was looking for a way to use a conditional expression to define the node type in TikZ. Specifically, I wanted to do something based on the section number.

 \node[\checksectionshape{1}] at (0,0) {Test};

From some of the similar questions and their, I guess this is because the \ifthenelse part is probably not expanded when the macro is called. But I couldn't manage to get one of those answers to work for my case.

Is there a workaround?

share|improve this question
Please make your example compilable. –  Caramdir Sep 20 '11 at 20:03
You can try \xdef\tmp{\checksectionshape{1}} \node[\tmp] ... or the same with \expandafter\node\expandafter[\tmp] .... –  Caramdir Sep 20 '11 at 20:04
add comment

1 Answer

up vote 6 down vote accepted

I suspect that this is due to expansion. Generally, I would avoid requiring expansion in the actual arguments to a \node or other TikZ path component. However, it is easy to shift the expansion one layer out where it works fine. This is done using a key. The key executes some code which sets the shape accordingly.


  check section shape/.code={

\node[check section shape=1,draw] at (0,0) {Section 1};
\node[check section shape=2,draw] at (3,0) {Section 2};
\section{Main Part}

This produces:

Conditional node shape

share|improve this answer
Thanks a lot! But, is there a general way of achieving the same thing a level deeper? For example, \node at (0,0) {Parent} child {node [\checksectionshape{1}] {Child}}; Can it be made to work here? Or is this thing entire ordeal not recommended at all? –  Ramprasad Sep 21 '11 at 6:46
The important thing is the order of processing. When TeX encounters a key, it does some action. That action can be to execute some arbitrary piece of code. At that time, TeX is in its normal mode so it can be completely arbitrary code. When TeX is looking for a key, though, it can only execute expandable code. So if you put a macro in the options, it had better be expandable. (Also, the division in to a list has already taken place, but that's slightly different). (ctd) –  Andrew Stacey Sep 21 '11 at 9:00
so my general advice is that when TeX is looking for a key, you should give it a key. But that key can then do almost anything. In fact, you can think of keys as sort of macros (only a bit more general) so anywhere you would want to put a macro as a key, put an actual key but then use the /.code syntax to make it execute a macro. –  Andrew Stacey Sep 21 '11 at 9:15
I see. Thank you very much. I understand TeX a bit better now. –  Ramprasad Sep 21 '11 at 10:27
add comment

Your Answer


