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 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}
share|improve this question
    
Not sure about understanding your problem but may be this answer to How to fix the trajectory of Brownian motions which generated by the “rand” function with tikz in beamer frames could help you. It proposes to save all random generated values into data files and use them later without having to compute (or remeber) them again. –  Ignasi Apr 9 at 7:06
    
Yes, I think that might work. I'm trying to plot several Brownian bridges. –  Kevin Apr 9 at 14:21
    
Is it \lastx or \lasty? If all are \lasty try [remember=\x as \lasty (initially 0)] or define \lasty before the loop as zero –  percusse Apr 9 at 15:18

1 Answer 1

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}

enter image description here

\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}
share|improve this answer
    
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 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 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 at 15:01

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.