11

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}
6

1 Answer 1

5

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} 
3
  • OK, thats a workaround, but shouldn’t this bug be fixed in TikI?
    – Tobi
    Commented Oct 30, 2012 at 8:17
  • Where does \newforeach come from? Commented May 2, 2014 at 15:18
  • loops package.
    – Ahmed Musa
    Commented May 4, 2014 at 16:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .