6

Question.

Why does

\documentclass{amsart}   
\usepackage{tikz}
\usetikzlibrary{calc}


\begin{document}

\begin{tikzpicture}

\foreach \index in {0,1,2,3}{
  \node (v\index) at (\index cm, \index cm)[]{$v_{\index}$};
    %\draw (v0)--(v1);    
  \draw (v0)--($(v0)+(1 cm,0 cm)$);
 }
\end{tikzpicture}

\end{document}

compile, while

\documentclass{amsart}   
\usepackage{tikz}
\usetikzlibrary{calc}


\begin{document}

\begin{tikzpicture}

\foreach \index in {0,1,2,3}{
  \node (v\index) at (\index cm, \index cm)[]{$v_{\index}$};
  \draw (v0)--(v1);    
  %\draw (v0)--($(v0)+(1 cm,0 cm)$);
  }
\end{tikzpicture}

\end{document}

results in an error message

No shape named v1 is known.

?

Remark.

  • I would also appreciate advice, if the present one is not recommendable, about how to make the name of a node depend on a loop variable.
  • I simply do not see a reason for the above error message, neither from rereading the code, nor the manual, nor from reading discussions about naming nodes in TikZ---the node name seems to have been defined. I hope that this is not a bug, rather, that I am overlooking something easily fixed.
3
  • 4
    You are using v1 without creating it before, when the foreach index is = 0 – CarLaTeX Aug 27 '17 at 8:21
  • @CarLaTeX: many thanks. Of course, you are right. This is embarassing for me. Whether you believe it or not, the reason for having asked it that way is this: the real motivation is a larger code, wherein I use the \draw (v0)--(v1); outside the loop parentheses, and still get an error. However, this error might be due to some additional complexities. I then tried to comply with the 'give a minimal nonworking example' guideline, and ended up producing this triviality. I am afraid I cannot easily describe the error I am really interested in, without posting a huge code. Many thanks again. – Peter Heinig Aug 27 '17 at 8:57
  • Don't worry, the important thing is that you've found a solution :) – CarLaTeX Aug 27 '17 at 9:03
9

The reason for the error message is already explained in CarLaTeX's comment.

I assume, you want to connect the nodes with lines. Then, the previous node can be remembered by option remember. The \if construct excludes the first loop, when there is only just one node defined yet.

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}

\foreach \index [remember=\index as \lastindex] in {0,1,2,3}{
  \node (v\index) at (\index cm, \index cm)[]{$v_{\index}$};
  \ifnum\index>0
    \draw (v\lastindex) -- (v\index);
  \fi
}

\end{tikzpicture}
\end{document}

Result

A variant without \ifnum:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}

\foreach \index in {0, 1, 2, 3} {
  \node (v\index) at (\index cm, \index cm)[]{$v_{\index}$};
}
\draw
  \foreach \index [remember=\index as \lastindex (initially 0)] in {1, 2, 3} {
    (v\lastindex) -- (v\index)
  }
;

\end{tikzpicture}
\end{document}
6

A possible solution is to define the first node out of the cicle:

\documentclass{amsart}   
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

    \begin{tikzpicture}

    \node (v0) {$v_0$};
    \foreach[evaluate=\index as \indexj using int(\index+1)] \index in {0,1,2}{
        \node (v\indexj) at (\indexj cm, \indexj cm)[]{$v_{\indexj}$};
        \draw (v\index)--(v\indexj);    
    }
    \end{tikzpicture}

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.