I want to draw a regular polygon in which there is an edge between the consecutive vertices of odd index: For instance with 8 vertices, I want a path 1 -- 3 -- 5 -- 7 -- 1
. To do this with a large number of vertices, I'd like to do this automatically rather than to handwrite the full path.
Using some answers here (in particular this one), I've come up with the following solution (here with 16 vertices):
\documentclass[tikz]{standalone}
\usetikzlibrary{shapes.geometric,calc}
\begin{document}
\begin{tikzpicture}
\node (pol) [minimum size=\textwidth,regular polygon, rotate=90,regular polygon sides=8] at (0,0) {};
\foreach \n in {1, 2, ..., 8} {
\node[anchor=\n*(360/8)] at (pol.corner \n) {\n};
}
\foreach \n [remember=\n as \lastn (initially 7)] in {1, 3, ..., 7} {
\path[draw] (pol.corner \lastn) -- (pol.corner \n);
}
\end{tikzpicture}
\end{document}
The problem is that I get the path 3 -- 5 -- 7 -- 1
plus the edge 3 -- 7
instead of the path I wanted. It seems to me (I may well be wrong!) that \lastn
is not correctly updated at the beginning of the loop.
Do you see what is going wrong? Do you have a different way to achieve the same goal?
\lastn
be initialized to7
instead of8
? If I do that, I get exactly the shape that you describe at the beginning... path from1--3--5--7--1
\lastn
is not correctly initialized in the\foreach
loop.