9

How can I define an array of coordinates in TikZ? Consider the following:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
  \begin{tikzpicture}
    \newcommand{\vertA}{{(0,0),(1,0),(2,0)}}
    \foreach \i in {0,1,2} {
      \coordinate (V\i) at ($\vertA[\i]$);
    }
  \end{tikzpicture}
\end{document}

Compiling with pdflatex gives the error:

Runaway argument?
\vertA [\i ]$); \pgffor@endhook \ifx \pgffor@assign@after@code \pgfutil@empty \
ETC.
! File ended while scanning use of \tikz@cc@parse@factor.

1 Answers1

11

I would go about this differently: Instead of looping over the indices and then accessing the members of the \vertA array, you can loop directly over the coordinate array and use the count=<macro name> function of \foreach.

I couldn't get the \coordinate (<name>) at (<coordinate>) command to accept a macro as its coordinate, so I went with the \coordinate [at=<coordinate macro>] approach instead:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
  \begin{tikzpicture}
    \newcommand{\vertA}{(0,0),(1,0),(2,3)}
    \foreach \coord [count=\i] in \vertA {
      \coordinate [at=\coord, name=A\i];
    }
    \draw (A1) -- (A2) -- (A3);
  \end{tikzpicture}
\end{document}
Jake
  • 232,450