我现在有一张这样的图像:
它由以下 LaTeX 代码创建:
\documentclass[varwidth=true, border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{amsmath,amssymb}
\begin{document}
\begin{tikzpicture}
\newcommand{\R}{1.5} % radius of the circle
\newcommand{\n}{6} % edges of the polygon
% Center
\path ( 0,0) coordinate (M);
% Inner polygon
\foreach \nr in {1, ..., \n}{
\path (360/\n*\nr:\R) coordinate (i\nr);
\draw (M) -- (i\nr);
}
\draw (0:0.3*\R) arc (0:360/\n:0.3*\R);
\coordinate[label=right:$\alpha$] (Alpha) at ({2*360/(\n+2)}:0.1*\R);
\draw (i1) -- (i2) -- (i3) -- (i4) --(i5) -- (i6) -- cycle;
% Circle
\draw[green] (0,0) circle (\R);
\end{tikzpicture}
\end{document}
我希望有独立于 n 的代码。因此,目前,当我n
从 6 更改为 7 时,我得到:
我知道我必须更改以下部分:
\draw (i1) -- (i2) -- (i3) -- (i4) --(i5) -- (i6) -- cycle;
但这不起作用:
\foreach \nr in {1, ..., \n-1}{
\draw (M) -- (i{\nr+1});
}
它给:
! Package pgf Error: No shape named i{1+1} is known.
问题是我的数学表达式没有被求值,但我不知道如何让 LaTeX 求值。如何将某个东西添加到带有数字的变量中?
答案1
您可以使用
\draw (i1) \foreach \i in {2,...,\n} {-- (i\i)} -- cycle;
循环遍历所有角落:
\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{amsmath,amssymb}
\begin{document}
\begin{tikzpicture}
\newcommand{\R}{1.5} % radius of the circle
\newcommand{\n}{5} % edges of the polygon
% Center
\path ( 0,0) coordinate (M);
% Inner polygon
\foreach \nr in {1, ..., \n}{
\path (360/\n*\nr:\R) coordinate (i\nr);
\draw (M) -- (i\nr);
}
\draw (0:0.3*\R) arc (0:360/\n:0.3*\R);
\coordinate[label=right:$\alpha$] (Alpha) at ({2*360/(\n+2)}:0.1*\R);
\draw (i1) \foreach \i in {2,...,\n} {-- (i\i)} -- cycle;
% Circle
\draw[green] (0,0) circle (\R);
\end{tikzpicture}
\end{document}