我想画一个五角星,星的每个点都有不同的标签。我得到了下面的例子,它紧跟这个问题,但我无法用不同的文本标签替换数字,也无法删除圆圈线。 (我想我也不需要foreach
。)
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes}
\begin{document}
\begin{tikzpicture}[n/.style={circle,inner sep=1pt}]
\draw node [star, star point height=.5cm, minimum size=2cm, inner sep=0,outer sep=0] (s) {}
circle (1) (s.outer point 1) node[n,label={90:1}]{}
foreach\x in {4,2,5,3}{--(s.outer point \x) node[n,label={(-45+90*\x):\x}]{}}--cycle;
\end{tikzpicture}
\end{document}
答案1
修订:更短的 TikZ 代码
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[dot/.style={circle,fill,inner sep=2pt}]
\def\r{2} % radius of the star
\def\n{4} % n+1 = number of vertexes
\def\Vlabel{{"$V_a$","$V_b$","$V_c$","$V_d$","$V_e$"}}
\foreach \i in {0,...,\n}
\path ({90+\i*360/(\n+1)}:\r) coordinate (V\i) node[dot]{}
+({90+\i*360/(\n+1)}:.4) node{\pgfmathparse{\Vlabel[\i]}\pgfmathresult}
;
\draw (V2)--(V0)--(V3)--(V1)--(V4)--cycle;
\end{tikzpicture}
\end{document}
这是你想要的吗?你可以使用数组选择标签Vlabel
。
// Run on http://asymptote.ualberta.ca/
import math;
unitsize(1cm);
real r=2;
int n=5;
pair[] V;
for(int i=0; i<n; ++i) V.push(r*dir(90+360*i/n));
draw(V[0]--V[2]--V[4]--V[1]--V[3]--cycle);
string[] Vlabel={"$V_a$","$V_b$","$V_C$","$V_d$","$V_e$"};
for(int i=0; i<n; ++i)
dot(Vlabel[i],align=2dir(degrees(V[i])),V[i],blue);
更新
1.您可以通过加载嵌入到 LaTex 文档中\usepackage[inline]{asymptote}
。对于 MikTeX,您必须安装Asymptote 软件 (该软件已包含在 TeXlive 中)。
\documentclass{article}
\usepackage[inline]{asymptote}
\begin{document}
\begin{asy}
import math;
unitsize(1cm);
real r=2;
int n=5;
pair[] V;
for(int i=0; i<n; ++i) V.push(r*dir(90+360*i/n));
draw(V[0]--V[2]--V[4]--V[1]--V[3]--cycle);
string[] Vlabel={"$V_a$","$V_b$","$V_C$","$V_d$","$V_e$"};
for(int i=0; i<n; ++i)
dot(Vlabel[i],align=2dir(degrees(V[i])),V[i],blue);
\end{asy}
\end{document}
2.同样的,你也可以用 TikZ 来画图。我选择 Asymptote 是因为 TikZ 中的数组操作不太方便。你知道我们要用多少技巧吗?^^
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\def\r{2}
\def\n{5}
\pgfmathsetmacro\m{\n-1}
\foreach \i in {0,...,\n}
\path ({90+\i*360/\n}:\r) coordinate (V\i);
\draw (V2)--(V0)--(V3)--(V1)--(V4)--cycle;
\def\Vlabel{{"$V_a$","$V_b$","$V_c$","$V_d$","$V_e$"}}
\foreach \i in {0,...,\m}{
\fill[red] (V\i) circle(2pt);
\path (0,0)--(V\i)--([turn]0:.4)
node{\pgfmathparse{\Vlabel[\i]}\pgfmathresult};
}
\end{tikzpicture}
\end{document}
答案2
这是 TikZ 解决方案。您可以让\foreach
循环遍历标签集并用来count=
生成数值。该evaluate
命令计算角度 ( \k
),用于放置节点并定位标签。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \l[count=\t, evaluate=\t as \k using \t*72+18] in {label1,label2,label3,label4,label5}
{\node[inner sep=0pt,label={\k:\l}] at (\k:2)(n\t){};}
\draw (n1.center)--(n3.center)--(n5.center)--(n2.center)--(n4.center)--cycle;
\end{tikzpicture}
\end{document}
这是一个内置于宏的版本\labelstar[<radius>]{<label list>}
,可以处理任意(合理)长度的列表。
\documentclass{article}
\usepackage{tikz}
\newcommand{\labelstar}[2][1]{\begin{tikzpicture}
\foreach \l[count=\n] in {#2}{\xdef\nn{\n}}
\foreach \l[count=\n, evaluate=\n as \t using (\n-1)/\nn*360+90] in {#2} {
\node[inner sep=0pt] at (\t:#1)(n\n){};
\node at (\t:(#1+.25){\l};}
\foreach \k[evaluate=\k as \m using {int(1+mod(int(\k+(\nn+1)/2-1)-1,\nn))}] in {1,...,\nn}{
\draw[line cap=round] (n\k.center)--(n\m.center);}
\end{tikzpicture}}
\begin{document}
\labelstar{a,b,c}\qquad
\labelstar{a,b,c,d}\qquad
\labelstar{a,b,c,d,e}\qquad
\labelstar{a,b,c,d,e,f}\qquad
\bigskip
\labelstar{a,b,c,d,e,f,g}\qquad
\labelstar{a,b,c,d,e,f,g,h}\qquad
\labelstar{a,b,c,d,e,f,g,h,i}\qquad
\labelstar{a,b,c,d,e,f,g,h,i,j}\qquad
\bigskip
\labelstar[2]{a,...,q}\qquad
\labelstar[2]{a,...,z}\qquad
\end{document}
答案3
尝试使用 MetaPost,包含在 LuaLaTeX 程序中。可以与任意数量的峰会一起使用(大于 2,但小于 27,因为是字母标记 :-))
\documentclass[12pt, border=1mm]{standalone}
\usepackage{luamplib}
\mplibsetformat{metafun}
\mplibtextextlabel{enable}
\begin{document}
\begin{mplibcode}
beginfig(1);
u = 3cm; n = 5;
for i = 0 upto n-1:
z[i] = u*dir(90+360i/n);
freelabel("$V_" & char(97+i) & "$", z[i], origin);
endfor;
for i = 0 upto n-3: draw z[i]-- z[i+2]; endfor
draw z[n-2]--z0; draw z[n-1]--z1;
drawoptions(withpen pencircle scaled 3bp withcolor red);
for i = 0 upto n-1: drawdot z[i]; endfor
endfig;
\end{mplibcode}
\end{document}
n=5:
n=9:
答案4
带有以下代码的短代码pstricks
:
\documentclass[border=10pt, svgnames]{standalone}
\usepackage{pst-poly}
\begin{document}
\begin{pspicture}(-2,-2)(2,2)
\psset{unit = 2cm, linejoin=1, PstPicture=false}
\everypsbox{\scriptsize\emph{}}
\PstPolygon[PolyNbSides=5, PolyOffset = 2,PolyRotation =18, linecolor=LightSteelBlue]
\foreach \i/\angle/\s in{1/90/u, 2/162/l, 3/234/l, 4/306/r, 5/378/r}{\uput{3pt}[\s](1;\angle){\color{LightSteelBlue}$\i$}}
\end{pspicture}
\end{document}