我使用以下命令创建正多边形。第 5 个参数用于插入用于设置多边形样式的选项。虽然我设置的线宽为 0.3 毫米,但看起来该fill=red
选项不被接受。
\documentclass{article}
\usepackage{amsmath,tikz,tkz-euclide,pgfplots,calculator}
\newcommand\polygon[5][]
{\pgfmathsetmacro{\angle}{360/#2}
\pgfmathsetmacro{\a}{(#2-1)/2}
\FLOOR{\a}{\sol}
\pgfmathsetmacro{\startangle}{-90 + \angle/2}
\foreach \i in {1,2,...,#2}
{\pgfmathsetmacro{\x}{\startangle + \angle*\i}
\draw[#5] (\x:#3) -- (\x + \angle:#3) -- cycle;
\tkzDefPoint(\x:#3){A_#2}
\tkzDrawPoint(A_#2)}
\foreach [count=\i] \j in {#4}{
\pgfmathsetmacro{\x}{\startangle - \angle*(\i+\sol)}
\node[anchor=center] at (\x:#3+.3) {$\j$};}}
\begin{document}
\begin{tikzpicture}
\draw[line width=0.3mm] (0,0) circle (1);
\polygon[]{6}{1}{A,B,\varGamma,\varDelta,E,Z}{line width=0.3mm,fill=red}
\end{tikzpicture}
\end{document}
我不知道命令的结构是否不允许任何其他选项。
答案1
我想知道你为什么不采用这样的方法
\documentclass{article}
\usepackage{amsmath,tikz}
\usetikzlibrary{shapes}
\newcommand\polygon[5][]{%
\node[draw,#5,regular polygon,regular polygon sides=#2,minimum size=2*#3cm] (p) {};
\foreach \i[count=\j] in {#4} {
\path (p.center)--(p.corner \j) node[pos=1.2] {$\i$};
}
}
\begin{document}
\begin{tikzpicture}
\draw[line width=0.3mm] (0,0) circle (1);
\polygon[]{6}{1}{A,B,\varGamma,\varDelta,E,Z}{line width=0.3mm,fill=red}
\end{tikzpicture}
\end{document}
不过,我认为您不应该为此定义宏。
如果你想走自己的路,克皮姆已经给出了正确的方法:使用(0,0)
而不是cycle
。但是,还得再多做一点。而且,还会有一条非常细的白线。
\documentclass{article}
\usepackage{amsmath,tikz,tkz-euclide,pgfplots,calculator}
\newcommand\polygon[5][]{%
\pgfmathsetmacro{\angle}{360/#2}
\pgfmathsetmacro{\a}{(#2-1)/2}
\FLOOR{\a}{\sol}
\pgfmathsetmacro{\startangle}{-90 + \angle/2}
\foreach \i in {1,2,...,#2} {
\pgfmathsetmacro{\x}{\startangle + \angle*\i}
\fill[#5] (\x:#3) -- (\x + \angle:#3) -- (0,0);
\draw[#5] (\x:#3) -- (\x + \angle:#3);
\tkzDefPoint(\x:#3){A_#2}
\tkzDrawPoint(A_#2)
}
\foreach [count=\i] \j in {#4} {
\pgfmathsetmacro{\x}{\startangle - \angle*(\i+\sol)}
\node[anchor=center] at (\x:#3+.3) {$\j$};
}
\foreach \i in {1,2,...,#2} {
\pgfmathsetmacro{\x}{\startangle + \angle*\i}
\tkzDefPoint(\x:#3){A_#2}
\tkzDrawPoint(A_#2)
}
}
\begin{document}
\begin{tikzpicture}
\draw[line width=0.3mm] (0,0) circle (1);
\polygon[]{6}{1}{A,B,\varGamma,\varDelta,E,Z}{line width=0.3mm,fill=red}
\end{tikzpicture}
\end{document}