Error de imagen de TikZ: no se conoce ningún nodo llamado n1, dentro de un bucle \foreach

Error de imagen de TikZ: no se conoce ningún nodo llamado n1, dentro de un bucle \foreach

Obtuve el siguiente error:

! Package pgf Error: No shape named n1 is known.

cuando intento compilar este código.

\documentclass[border=1mm]{standalone}
\usepackage{tikz,pgfplots}
\usetikzlibrary{calc,arrows}
\usepackage{xifthen}
\begin{document}

\begin{tikzpicture}[scale=1,>=latex',line width=0.7pt]

\foreach \i/\j in {0/1,1/2,2/3,3/4,4/5,5/6,6/7,7/8}{%
\node (n\i) [draw,minimum size=1cm] at (1.7*\i,0){};
\ifthenelse{\i=7}{}{%
\draw[->] (n\i)--(n\j);}%
}
\end{tikzpicture}
\end{document}

Aprecio mucho tu ayuda.

Respuesta1

Podrías (al menos en este caso) probar un chain:

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{chains,arrows}
\begin{document}
\begin{tikzpicture}[>=latex', ultra thick, 
  start chain, every join/.style=->, node distance=1.7]
\foreach \i in {0,...,8}
  \node (n\i) [draw, minimum size=1cm, on chain, join] {}; 
\end{tikzpicture}
\end{document}

ingrese la descripción de la imagen aquí

Respuesta2

Propondría utilizar una sintaxis más limpia. Aquí hay otras dos formas;

\documentclass[tikz,border=1mm]{standalone}
\usetikzlibrary{arrows}
\begin{document}
\begin{tikzpicture}[scale=1,>=latex',line width=0.7pt]
\node[draw,minimum size=1cm] (n0) at (0,0) {};
\foreach \x[remember=\x as \lastx (initially 0)] in {1,...,7}{%
\node(n\x) [draw,minimum size=1cm] at (1.7*\x,0){};
\draw[<-] (n\x) -- (n\lastx);
}
\end{tikzpicture}
% If feeling adventurous
\begin{tikzpicture}[scale=1,>=latex',line width=0.7pt]
\node[draw,minimum size=1cm] (n0) at (0,0) {};
\foreach \x[remember=\x as \lastx (initially 0)] in {1,...,7}{%
\draw[<-]node[draw,minimum size=1cm,append after command=(n\x) -- (n\lastx)](n\x) at (1.7*\x,0){};
}
\end{tikzpicture}
\end{document}

Si utiliza estrictamente números enteros, puede salirse con la suya\ifnum\i=7\else....\fi

Respuesta3

EDITAR: Sin romper la \foreachdeclaración, podríamos haber hecho esto de dos maneras:

\documentclass[border=1mm]{standalone}
\usepackage{tikz,pgfplots}
\usetikzlibrary{calc,arrows}
\usepackage{xifthen}
\begin{document}
\begin{tikzpicture}[>=latex',line width=0.7pt]
\foreach [evaluate=\i as \j using \i-1]\i in {0,...,7}{%
\node (n\i) [draw,minimum size=1cm] at (1.7*\i,0){};
\ifthenelse{\i=0}{}{%
\draw[->] (n\j)--(n\i);}%
}
\end{tikzpicture}
\begin{tikzpicture}[>=latex',line width=0.7pt]
\foreach \i/\j in {0/-1,1/0,2/1,3/2,4/3,5/4,6/5,7/6}{%
\node (n\i) [draw,minimum size=1cm] at (1.7*\i,0){};
\ifthenelse{\i=0}{}{%
\draw[->] (n\j)--(n\i);}%
}
\end{tikzpicture}
\end{document}

Estoy de acuerdo con el comentario de Jake. Puede lograr esto separando el \foreachen dos partes de la siguiente manera:

\documentclass[border=1mm]{standalone}
\usepackage{tikz,pgfplots}
\usetikzlibrary{calc,arrows}
\usepackage{xifthen}
\begin{document}

\begin{tikzpicture}[scale=1,>=latex',line width=0.7pt]

\foreach \i/\j in {0/1,1/2,2/3,3/4,4/5,5/6,6/7,7/8}%
\node (n\i) [draw,minimum size=1cm] at (1.7*\i,0){};

\foreach \i/\j in {0/1,1/2,2/3,3/4,4/5,5/6,6/7,7/8}{%
\ifthenelse{\i=7}{}{%
\draw[->] (n\i)--(n\j);}%
}
\end{tikzpicture}
\end{document}

Esto debería funcionar correctamente ya que todos los nodos n0... a través n7seguramente se crean en el \foreachbucle superior.

información relacionada