Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
1  
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 1

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.

\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

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) –  Loop Space 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. –  Loop Space 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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.