3

I'm practicing using pin and marks on \coordinate and \node.

Goal/Expected output

  1. A regular polygon
  2. parametrized by the number of sides n
  3. with each vertex marked by a circular dot
  4. and a label outside the polygon.

regular polygon

\documentclass[dvisvgm]{standalone}
\usepackage{tikz}
\begin{document}
\newcommand\myR{2}
\newcommand\n{7}
\newcommand\dTheta{360/\n}
\tikzset{
  every pin edge/.style={opacity=0},
  pin distance=2pt,
  mypt/.style={circle,fill=black,minimum size=0.5pt,inner sep=-2pt},
}
\begin{tikzpicture}
\foreach \k in {1,...,\n} {\coordinate[mypt,pin={\dTheta*\k}:{$V_{\k}$}] (V\k) at ({\dTheta*\k}:\myR);}
\foreach \k in {2,...,\n} {\draw (V\the\numexpr\k-1) -- (V\k);}
\draw (V\n) -- (V1);
\end{tikzpicture}
\end{document}

Problem

Why the technique of

\draw (1) \foreach \n in {2,...,5} {--(\n)} --cycle;

in this accepted answer doesn't work when I included the styles for a vertex mypt?

error input

\documentclass[dvisvgm]{standalone}
\usepackage{tikz}
\begin{document}
\newcommand\myR{2}
\newcommand\n{7}
\newcommand\dTheta{360/\n}
\tikzset{
  every pin edge/.style={opacity=0},
  pin distance=2pt,
  mypt/.style={circle,fill=black,minimum size=0.5pt,inner sep=-2pt},
}
\begin{tikzpicture}
\foreach \k in {1,...,\n} {\coordinate[mypt,pin={\dTheta*\k}:{$V_{\k}$}] (V\k) at ({\dTheta*\k}:\myR);}
\draw (V1) foreach \k in {2,...,\n} { -- (V\k)} -- cycle;
\end{tikzpicture}
\end{document}

Note that if I remove mypt, from the above code block, then the edges will be correctly drawn.

regular polygon

Related question: \foreach and names

2 Answers2

1

(this is a long comment, so I post it here)

I never use the keys pin or edge, that are not so useful, and complexify the code in my opinion. In the code below: parameters are clear; [parse=true] is for labeling, and parentheses (n) is something undocumented in the pgfmanual.pdf, see this Henri Menke's answer. I also change a bit the style mypt to the style dot.

enter image description here

\documentclass[tikz,border=5mm]{standalone} 
\begin{document}
\begin{tikzpicture}[declare function={n=7;r=2;a=360/n;},
dot/.style={circle,fill,inner sep=2pt}]

\foreach \i [evaluate=\i as \ilabel,parse=true] in {1,...,(n)} \path (a\i:r) coordinate (V\i) node[dot]{} +(a\i:.4) node{$V_{\ilabel}$};

\draw (V1) \foreach \i [parse=true] in {2,...,(n)} {--(V\i)} --cycle;
\end{tikzpicture}

\begin{tikzpicture}[dot/.style={circle,fill,inner sep=2pt},teal] \def\n{7} \def\r{2} \pgfmathsetmacro{\a}{360/\n}
\foreach \i in {1,...,\n} \path (\a\i:\r) coordinate (V\i) node[dot]{} +(\a\i:.4) node{$V_{\i}$} ;

\draw (V1) \foreach \i in {2,...,\n} {--(V\i)} --cycle;
\end{tikzpicture} \end{document}

Asymptote code:

enter image description here

// from Nguyen Vu Tran
unitsize(2cm);
path g=polygon(7);
draw(g);
for (int i=0;i<size(g);++i)
dot("$V_{"+string(i+1)+"}$",point(g,i),dir(point(g,i)),purple);

enter image description here

// from PT Sinh
unitsize(2cm);
int n=9;
pair V(int i) {return expi(2*i*pi/n + pi/n);}
for (int i = 0; i < n; ++i) {
draw(V(i)--V(i+1));
dot("$V_{"+string(i+1)+"}$",V(i),1.2V(i));
}
Black Mild
  • 17,569
0
\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{shapes.geometric}

\begin{document}
\begin{tikzpicture}

\node[draw, regular polygon, regular polygon sides=7, minimum size=5cm](A){};
\foreach \i/\j in {1/above,2/above left, 3/left, 4/below, 5/below, 6/right,7/above right}
    \fill[shift=(A.corner \i)] circle (1mm) node[\j]{$V_\i$};
\end{tikzpicture}
\end{document}

enter image description here

Ignasi
  • 136,588
  • thanks for showing another way to Rome, but sorry, a codeblock without any explanation doesn't help me understand the functioning of \node and \coordinate with foreach. besides, i don't want to hard code the relative position of the text labels around the dots. you see me writing pin={\dTheta*\k}:{$V_{\k}$} to calculate a suitable position to place the label---looking back, pin there should be replaced by label since I'm not using pin edge. In your figure, the bottom two vertices V₄ and V₅ are too close to the black circular dots above them. – GNUSupporter 8964民主女神 地下教會 Jun 29 '22 at 14:53