# Retain coordinate value in tikz foreach loop

I would like to retain a computed coordinate value from one step of a tikz foreach loop to the next. In the code below, I would like the increment in the y-value to depend on the previous y-value, which I am denoting \lasty. Is there a way to retain this value? Thanks for the help. The example below errors out including the - \lasty; the example should work otherwise.

\documentclass{beamer}
\usepackage{tikz}

\begin{document}
\begin{frame}[fragile]
\newcommand{\Emmett}[6]{% points, advance, rand factor, options, end label
\draw[#4] (0,0)
\foreach \x [remember=\x as \lastx] in {1,...,749}
{   -- ++(#2,{rand*#3 + #2*(#6-\lasty)/(#1-\x)})
}
node[right] {#5};
}
\scalebox{0.5}{
\begin{tikzpicture}
\draw[help lines] (0,-5) grid (15,5);
\Emmett{750}{0.02}{0.2}{red}{first one}{2.0}
\Emmett{750}{0.02}{0.2}{green}{second one}{-1.0}
\Emmett{750}{0.02}{0.2}{blue}{third one}{-2.0}
\end{tikzpicture}
}
%\pgfmathsetseed{1337}
\end{frame}
\end{document}

## 1 Answer

Here's a solution which remembers previous y values.

\documentclass{article}
\usepackage{tikz}
\usepackage{etoolbox}

\makeatletter
\def\ae@path{}
\newcommand{\Emmett}[6]{% points, advance, rand factor, options, end label
\def\ae@last@y{0}
\def\ae@initial@portion{\draw[#4] (0,0)}
\xdef\ae@path{\expandonce\ae@initial@portion}
\foreach \x in {1,...,749}
{
\pgfmathparse{rand*#3 + #2*(#6-\ae@last@y)/(#1-\x)}
\xdef\ae@last@y{\pgfmathresult}
\xdef\ae@path{\expandonce\ae@path -- ++(#2,\expandonce\ae@last@y)}
}
\ae@path node[right] {#5};
}

\makeatother

\begin{document}

\scalebox{0.5}{
\begin{tikzpicture}
\draw[help lines] (0,-5) grid (15,5);
\Emmett{750}{0.02}{0.2}{red}{first one}{2.0}
\Emmett{750}{0.02}{0.2}{green}{second one}{-1.0}
\Emmett{750}{0.02}{0.2}{blue}{third one}{-2.0}
\end{tikzpicture}
}
%\pgfmathsetseed{1337}

\end{document}

## \typeout for debugging

Here for debugging purposes, you can see that they y's are being properly updated.

\documentclass{article}
\usepackage{tikz}
\usepackage{etoolbox}

\makeatletter
\def\ae@path{}
\newcommand{\Emmett}[6]{% points, advance, rand factor, options, end label
\def\ae@last@y{0}
\def\ae@initial@portion{\draw[#4] (0,0)}
\xdef\ae@path{\expandonce\ae@initial@portion}
\foreach \x [remember=\x as \lastx (initially 0)]in {1,...,749}
{
\typeout{==>--------------------------------------------------}%%
\typeout{==>(x=\x) ==> (old y=\ae@last@y)}%%
\pgfmathparse{rand*#3 + #2*(#6-\ae@last@y)/(#1-\x)}
\xdef\ae@last@y{\pgfmathresult}
\typeout{==>(x=\x) ==> (new y=\ae@last@y)}%%
\xdef\ae@path{\expandonce\ae@path -- ++(#2,\expandonce\ae@last@y)}
}
\ae@path node[right] {#5};
}
\makeatother

\begin{document}

\scalebox{0.5}{
\begin{tikzpicture}
\draw[help lines] (0,-5) grid (15,5);
\Emmett{750}{0.02}{0.2}{red}{first one}{2.0}
\Emmett{750}{0.02}{0.2}{green}{second one}{-1.0}
\Emmett{750}{0.02}{0.2}{blue}{third one}{-2.0}
\end{tikzpicture}
}
%\pgfmathsetseed{1337}

\end{document}
• Thanks for the response. I do want the previous y coordinate rather than the previous x coordinate, as it looks like you are trying to do in the second part of your answer. The second code provided still does not retain the y-value. For example, consider starting the line at (0,5) and incrementing by -#2*\ae@last@y (\pgfmathparse{-#2*\ae@last@y))}). This plots a flat line rather than a declining one. – Kevin Apr 9 '14 at 14:15
• @kevin It's remembering things correctly. I've added some more lines to type out the values so you can see how they change. – A.Ellett Apr 9 '14 at 14:58
• @kevin, \xdef<cmd>{<definition>} fully expands the definition for <cmd> before the assignment takes place. It also does so globally. \def would be completely ineffective, and \edef would not do so globally. – A.Ellett Apr 9 '14 at 15:01