I am trying to write a command to draw a cycle graph with Tikz, basically a polygon. The parameters of the command are the number #1 of vertices, and the radius #2 of the figure.
My initial thought was to draw the #1 nodes first, give them names and then connect them by an edge. The code was
\newcommand{\cyclegraph}[2]{\begin{tikzpicture}
\foreach \n in {1,...,#1}{
\node[circle,fill=black] (n\n) at ({\n*360/#1}:#2cm) {};
}
\foreach \n in {1,...,#1-1}{
\draw (n\n) -- (n{1+\n})
}
\draw (n#1) -- (n1);
\end{tikzpicture}}
Unfortunately, the code in the \foreach loop does not work. The error messages are "Package pgf Error: No shape named n{1+1} is known. \cyclegraph{7}{2}", "Package pgf Error: No shape named n{1+2} is known. \cyclegraph{7}{2}", etc. Obviously, that's a problem with the naming of the nodes. Is there a way to fix that?
Of course, I could rewrite the code as
\newcommand{\cyclegraph}[2]{\begin{tikzpicture}
\foreach \n in {1,...,#1}{
\node[circle,fill=black] (n\n) at ({\n*360/#1}:#2cm) {};
}
\foreach \n in {1,...,#1-1}{
\draw ({\n*360/#1}:#2cm) -- ({(1+\n)*360/#1}:#2cm);
}
\draw ({0}:#2cm) -- ({360/#1}:#2cm);
\end{tikzpicture}}
but I think it would be cleaner to use the names of the nodes. Also, it could be useful for later figures. Any idea?