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.

Is there an error in my code or did I find a bug? It seems like the \foreach statement doesn’t increase \lastn if there is an initally (value) but it works with a manually given initially value (\def\lastn).

This is what I expect …

right

… and that is what I get …

wrong

Here is my MWE:

\documentclass[border=1cm]{standalone}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[every node/.style={draw}]
    \node (A) at (0,0) {A};
    \node (B) at (2,0) {B};
    \node (C) at (4,0) {C};
    \node (D) at (6,0) {D};
%   DOESN'T WORK
%   \foreach \n [remember=\n as \lastn (initially A)] in {B,C,D} {
%       \draw [->] (\lastn) -- (\n);
%   }
%   WORKS
    \def\lastn{A}
    \foreach \n [remember=\n as \lastn] in {B,C,D} {
        \draw [->] (\lastn) -- (\n);
    }
\end{tikzpicture}
\end{document}
share|improve this question
    
It works for me with CVS version but not with CTAN version. –  Ignasi Oct 29 '12 at 17:16
1  
Replacing {B,C,D} with {B,...,D} also works correct. –  Qrrbrbirlbel Oct 29 '12 at 17:20
    
Possible duplicate: Tikz foreach with two variables and the remember option • Related: PGF/TikZ bug tracker –  Qrrbrbirlbel Oct 29 '12 at 17:28
    
The same issue here tex.stackexchange.com/questions/66844/… In the stable release the pattern somehow should be inline with the initial value otherwise it doesn't seem to like it. –  percusse Oct 29 '12 at 22:50
1  
I confirm that the issue is still there with in the TeXLive 2013 version of TikZ (v 2.10, rcs rev 1.76) –  Olivier Jul 20 '13 at 20:50
show 1 more comment

1 Answer

up vote 5 down vote accepted

It works with \newforeach:

\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\usepackage{loops}
\begin{document}
\begin{tikzpicture}[every node/.style=draw]
\node (A) at (0,0) {A};
\node (B) at (2,0) {B};
\node (C) at (4,0) {C};
\node (D) at (6,0) {D};
\newforeach \n [remember=\n as \lastn initially A] in {B,C,D}
  \draw [->] (\lastn) -- (\n);
\end{tikzpicture}
\end{document}

enter image description here

You can try also

\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\usepackage{loops}
\begin{document}
\begin{tikzpicture}
\node at (0,1) [circle,draw,red] {
  \foxloop [remember=#1 as \x initially A] {B,C,D,E,F,G,H}{%
    $\overrightarrow{\x#1}$\iflastfox.\else,\space\fi
  }
};
\end{tikzpicture}
\end{document} 
share|improve this answer
    
OK, thats a workaround, but shouldn’t this bug be fixed in TikI? –  Tobi Oct 30 '12 at 8:17
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.