我正在练习使用pin
和marks
和\coordinate
。\node
目标/预期产出
- 正多边形
- 由边数参数化n
- 每个顶点都用一个圆点标记
- 以及多边形外部的标签。
\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}
问题
为什么
\draw (1) \foreach \n in {2,...,5} {--(\n)} --cycle;
在这个被接受的答案当我包含顶点样式时它不起作用mypt
?
\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}
请注意,如果我mypt
从上面的代码块中删除,那么边缘将被正确绘制。
相关问题:\foreach 和名称
答案1
(这是一条很长的评论,所以我将其发布在这里)
我从不使用键pin
或edge
,因为它们不太有用,而且在我看来会使代码复杂化。在下面的代码中:参数很明确;[parse=true]
用于标记,括号(n)
是 pgfmanual.pdf 中未记录的内容,请参阅这是 Henri Menke 的回答mypt
。我也对风格做了一些改变dot
。
\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}
渐近线代码:
// 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);
// 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));
}
答案2
\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}