# Trouble using pgfmathsetmacro in foreach loop more than once

I'm trying to draw a series of lines where the x-coordinate is given by a geometric sequence, as in the following example:

\begin{tikzpicture}
\foreach \i in {0,...,3}
\pgfmathsetmacro{\result}{7-pow(2,-\i)}
\draw [red, ultra thick] (\result,3) -- (\result,5);
\draw [blue, ultra thick] (\result,2.5) -- (\result,3);
\draw [green, ultra thick] (\result,5) -- (\result,5.5);
\end{tikzpicture}


However, I can't seem to use \result more than once. If I comment out 2 of the \draw lines it works, yet if 2 or more \draw lines are uncommented I receive an error:

! Undefined control sequence.
<argument> \result


There are curly braces around the block inside the foreach missing:

\begin{tikzpicture}
\foreach \i in {0,...,3} {
\pgfmathsetmacro{\result}{7-pow(2,-\i)}
\draw [red, ultra thick] (\result,3) -- (\result,5);
\draw [blue, ultra thick] (\result,2.5) -- (\result,3);
\draw [green, ultra thick] (\result,5) -- (\result,5.5);
}
\end{tikzpicture}


works for me.

• Ah, of course! I forgot the semicolon closes off the \foreach loop! Thank you for your help! – Giraffro Jun 25 '13 at 12:19