8

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.

 \def\checksectionshape#1{\ifthenelse{\value{section}=#1}{typea}{typeb}}
 ...
 \begin{tikzpicture}
 \node[\checksectionshape{1}] at (0,0) {Test};
 \end{tikzpicture}

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?

2
  • 1
    Please make your example compilable.
    – Caramdir
    Sep 20, 2011 at 20:03
  • You can try \xdef\tmp{\checksectionshape{1}} \node[\tmp] ... or the same with \expandafter\node\expandafter[\tmp] ....
    – Caramdir
    Sep 20, 2011 at 20:04

1 Answer 1

9

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.

\documentclass{article}
\usepackage{ifthen}
\usepackage{tikz}

\tikzset{
  check section shape/.code={
    \ifthenelse{\value{section}=#1}{%
      \tikzset{rectangle}%
    }{%
      \tikzset{circle}%
    }
  }
}

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

This produces:

Conditional node shape

4
  • 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, 2011 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) Sep 21, 2011 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. Sep 21, 2011 at 9:15
  • I see. Thank you very much. I understand TeX a bit better now.
    – Ramprasad
    Sep 21, 2011 at 10:27

You must log in to answer this question.

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