
Ich erhalte die folgende Fehlermeldung:
! Package pgf Error: No shape named n1 is known.
wenn ich versuche, diesen Code zu kompilieren.
\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}
Ich weiß Ihre Hilfe sehr zu schätzen.
Antwort1
Sie könnten (zumindest in diesem Fall) Folgendes versuchen 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}
Antwort2
Ich würde vorschlagen, eine sauberere Syntax zu verwenden. Hier sind zwei weitere Möglichkeiten:
\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}
Wenn Sie ausschließlich Ganzzahlen verwenden, können Sie damit davonkommen\ifnum\i=7\else....\fi
Antwort3
BEARBEITEN: Ohne die \foreach
Anweisung zu verletzen, hätten wir dies auf zwei Arten tun können:
\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}
Ich stimme Jakes Kommentar zu. Sie können dies erreichen, indem Sie es \foreach
wie folgt in zwei Teile aufteilen:
\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}
Dies sollte ordnungsgemäß funktionieren, da alle Knoten n0
... bis n7
sicher in der oberen \foreach
Schleife erstellt werden.