Consider the following MWE:

\usepackage{pgf, tikz}
\node {} child foreach \i in {1,2,3} {node {$\i$}};

If I add a \pgfmathparse{} inside the child braces, like this

\node {} child foreach \i in {1,2,3} {\pgfmathparse{1+1} node {$\i$}};

the following Error occurs:

! Package tikz Error: Giving up on this path. Did you forget a semicolon?.


Essentially I want to make conditional drawing of childs as in

\node (O) {} child foreach \i in {1,2,3} {
    \ifthenelse{\i=2}{}{node {$\i$}}

But I need some \pgfmathparse commands to evaluate more complicated conditions first.

  • 1
    move the math parsing into the node content. – percusse Oct 11 '14 at 17:58
  • I want to make the drawing of a child dependent on some circumstances. Essentially I thought of something like \pgfmathparse{\i=2?0:1} (and if 1 draw the child, and if 0 don't draw the child). Therefore it the evalulation needs to be there, where it is, I guess. – Rafael Wörner Oct 11 '14 at 18:31
  • Can you adjust your MWE to reflect what exactly what you want? This is simply writing math stuff in a node but what you describe is different. – percusse Oct 11 '14 at 18:39
  • @percusse I added an Edit section to my qustion. – Rafael Wörner Oct 11 '14 at 18:43
  • Maybe you are looking for \node (O) {} child foreach[evaluate={\j=(\i==2?:"node{$\i$}")}] \i in {1,2,3} {\j}; or \node (O) {} child foreach[evaluate={\j=(\i==2?"edge from parent[draw=none]":"node{$\i$}")}] \i in {1,2,3} {\j}; – esdd Oct 11 '14 at 20:23

TikZ has its own parser for creating paths and a slightly different parser for children in trees. So it isn't possible to stick arbitrary code just anywhere.

Inside normal paths code can be wrapped inside the \pgfextra command, but this isn't allowed in trees. If it is simply evaluation required then the (possibly still undocumented) evaluate key can be used in the foreach loop:

\node {} child foreach \i [evaluate={\j=int(\i*3);}] in {1,2,3} { node {$\i:\j$}};

enter image description here

  • Or as @percusse points out in his comment above, do the math inside the node. – Mark Wibrow Oct 11 '14 at 18:04
  • Thanks so far for this useful information. I think I should have gone further with my needs. As I pointed out above in a comment to my question, I want to do a conditional drawing of the childs. And I think therefore the code should be in that position I choose in my question, I guess. – Rafael Wörner Oct 11 '14 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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