In relation with an answer, I see that \foreach does not understand a constant (n) in declare function. Is it a bug? or there is another proper way of code for this situation?
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\def\n{7} % number of sides of a heptagon
\pgfmathsetmacro{\k}{360/\n}
\pgfmathsetmacro{\r}{2}
\draw[cyan] (90:\r) foreach \i in {1,...,\n} {--({90+\k*\i}:\r)}--cycle;
\end{tikzpicture}
% This does not work! Uncomment this tikzpicture to get a MWE
\begin{tikzpicture}[declare function={n=7;r=2;k=360/n;}]
\draw (90:r) foreach \i in {1,...,n} {--({90+k*\i}:r)}--cycle;
\end{tikzpicture}
% This works, but not natural! Is that a bug of \foreach ?
\begin{tikzpicture}[declare function={n=7;r=2;k=360/n;}]
\pgfmathsetmacro\m{n}
\draw (90:r) foreach \i in {1,...,\m} {--({90+k*\i}:r)}--cycle;
\end{tikzpicture}
\end{document}


declare functionconstants can be used and what the equivalent pgf set command is. I think\def\n{7}is bad because it can collide with packages - sonewcommandis preferred but seem silly for a constant. – hpekristiansen Sep 24 '21 at 02:20\newcommand\n{7}is better than\def\n{7}. Both of them can be use instandalonepdf picture that later it can be embbed into LaTeX document via\includegraphics. However, what I think TikZ' syntax should allow\draw (90:r) foreach \i in {1,...,n} {--({90+k*\i}:r)}--cycle;after declare constant/function[declare function={n=7;r=2;k=360/n;}]– Black Mild Sep 24 '21 at 02:36\pgfmathparse, which will recognize n as a math function. Evidently the foreach parser does not. Nor will it recognize (7) or 3+4. – John Kormylo Sep 24 '21 at 05:06kandr. Soforeachparser seems not as smart as other PGF parsers. It is unbalanced! – Black Mild Sep 24 '21 at 05:22