![绘制正多边形时出现编译错误](https://linux22.com/image/408983/%E7%BB%98%E5%88%B6%E6%AD%A3%E5%A4%9A%E8%BE%B9%E5%BD%A2%E6%97%B6%E5%87%BA%E7%8E%B0%E7%BC%96%E8%AF%91%E9%94%99%E8%AF%AF.png)
我根据以下示例创建了 tikset这里绘制正多边形。但我遇到了编译错误,我不知道为什么 :/ 。
此外:
- 五边形标签没有出现,而其他的标签都出现了
当我像示例中那样声明节点时发生崩溃
\draw (0:\R) \foreach \x in {120,240} { -- (\x:\R) } -- 循环 (90:\R) 节点[above] {$n=3$} ;
主文本
%%%%%%%%%%%%%%%%%% INTRODUCTION %%%%%%%%%%%%%%%%%%
\documentclass[border=10pt]{standalone}
%%%%%%%%%%%%%%%%%% PACKAGE %%%%%%%%%%%%%%%%%%
\usepackage{tikz, tkz-euclide}% permet de dessiner des figures, des graphiques
\usepackage{adjustbox}% permet de déterminer une taille de fenêtre
%% FONT
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tgadventor}% paquet de police de caractère TGadventor
\usepackage{sansmath}% Copie-colle la police active dans
% \sfdefault (/!\ N'EST PAS UNE POLICE DE CARACTÈRES)
\usepackage{xcolor}
%%%%%%%%%%%%%%%%%% INPUT %%%%%%%%%%%%%%%%%%
%\input{preamble.tex}
%\input{parameters.tex}
%\input{types/f2d_fig}
%%%%%%%%%%%%%%%%%% SETUP %%%%%%%%%%%%%%%%%%
\tikzset{pics/triangloid_regular/.style={code={%
\tikzset{triangloid_regular/.cd,#1}%
\def\pv##1{\pgfkeysvalueof{/tikz/triangloid_regular/##1}}%
\draw[thick, pic actions] (-30:\pv{radius}) \foreach \x in {90,210}%
{
-- (\x:\pv{radius})}%
-- cycle (90:\pv{radius})%
(0,\pv{radius}) coordinate node[above, scale=2.5] {\pv{lab}}%
;
}
},
triangloid_regular/.cd,
radius/.initial=3cm,
lab/.initial=,
;
}
\tikzset{pics/squaroid_regular/.style={code={%
\tikzset{squaroid_regular/.cd,#1}%
\def\pv##1{\pgfkeysvalueof{/tikz/squaroid_regular/##1}}
\draw[thick, pic actions] (45:\pv{radius}) \foreach \x in {135,225,...,359}
{
-- (\x:\pv{radius})}%
-- cycle (90:\pv{radius})%
(0,\pv{radius}) coordinate node[above, scale=2.5] {\pv{lab}}
;
}
},
squaroid_regular/.cd,
radius/.initial=3cm,
lab/.initial=,
;
}
\tikzset{pics/pentagoid_regular/.style={code={%
\tikzset{pentagoid_regular/.cd,#1}%
\def\pv##1{\pgfkeysvalueof{/tikz/squaroid_regular/##1}}
\draw[thick, pic actions] (18:\pv{radius}) \foreach \x in {90,162,...,359}
{
-- (\x:\pv{radius})}%
-- cycle (90:\pv{radius})%
(0,\pv{radius}) coordinate node[above, scale=2.5] {\pv{lab}}
;
}
},
pentagoid_regular/.cd,
radius/.initial=3cm,
lab/.initial=,
;
}
%%%%%%%%%%%%%%%%%% DOCUMENT %%%%%%%%%%%%%%%%%%
% Radius of regular polygons
\newdimen\R
\R=2cm
\begin{document}
\begin{tikzpicture}[font={\sansmath\sffamily},thick, line cap=round, line join=round, >=latex, x=1.0cm, y=1.0cm]
% Indicate the boundary of the regular polygons
\pic {triangloid_regular={lab=Triangle}};
\pic[xshift=7cm] {squaroid_regular={lab=Square}};
\pic[yshift=-7cm] {pentagoid_regular={lab=Pentagone}};
\end{tikzpicture}
\end{document}
我想这只是常事,我忽略了一些细节,导致我陷入困境>-<。
答案1
使用极坐标似乎更容易。代码如下:
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{xcolor}
\tikzset{pics/polygon/.style 2 args={
code = {
\foreach \a [evaluate=\a as \an using (270+(\a+0.5)*360/#1)] in {1, ..., #1} {
\draw(\an-360/#1:1)--(\an:1);
}
\node at (90:1.3){#2};
}
}
}
\begin{document}
\begin{tikzpicture}
\pic at (0,0) {polygon=3{Triangle}};
\pic at (3,0) {polygon=4{Square}};
\pic at (6,0) {polygon=5{Pentagon}};
\pic at (9,0) {polygon=6{Hexagon}};
\end{tikzpicture}
\end{document}
生成:
唯一有点棘手的是计算出适当的角度,以便“底部”边缘像 OP 中那样水平。添加其他功能来控制多边形的大小、它们fill
等等会很容易。(另一个选项是使用shape=regular polygon
来自\usetikzlibrary{decorations.shapes}
;请参阅第 51.5.3 节蒂克兹手动的。)
编辑
至于 OP 中的代码,问题似乎是您在triangloid_regular
定义之前引用了等。如果您将初始定义放在稍高的位置,则代码可以编译。“Pentagone”(五角大楼?)没有出现,因为squareoid
您pentagoid
在\pv##1{\pgfkeysvalueof{...}}
。第三个非常小的问题是,我不会使用带下划线的名称,因为 TeX 往往不喜欢它们。当然,在这种情况下它们可以工作,所以我只是很珍惜,欢迎您忽略我:)。
这是来自 OP 的更新后的代码,包含这些更改,现在可以编译了。
\documentclass[border=10pt]{standalone}
%%%%%%%%%%%%%%%%%% PACKAGE %%%%%%%%%%%%%%%%%%
\usepackage{tikz, tkz-euclide}% permet de dessiner des figures, des graphiques
\usepackage{adjustbox}% permet de déterminer une taille de fenêtre
%% FONT
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tgadventor}% paquet de police de caractère TGadventor
\usepackage{sansmath}% Copie-colle la police active dans
% \sfdefault (/!\ N'EST PAS UNE POLICE DE CARACTÈRES)
\usepackage{xcolor}
%%%%%%%%%%%%%%%%%% INPUT %%%%%%%%%%%%%%%%%%
%\input{preamble.tex}
%\input{parameters.tex}
%\input{types/f2d_fig}
%%%%%%%%%%%%%%%%%% SETUP %%%%%%%%%%%%%%%%%%
\tikzset{triangloid regular/.cd,
radius/.initial=3cm,
lab/.initial=,
}
\tikzset{pics/triangloid regular/.style={code={%
\tikzset{triangloid regular/.cd,#1}%
\def\pv##1{\pgfkeysvalueof{/tikz/triangloid regular/##1}}%
\draw[thick, pic actions] (-30:\pv{radius}) \foreach \x in {90,210}%
{
-- (\x:\pv{radius})}%
-- cycle (90:\pv{radius})%
(0,\pv{radius}) coordinate node[above, scale=2.5] {\pv{lab}}%
;
}
},
}
\tikzset{squaroid regular/.cd,
radius/.initial=3cm,
lab/.initial=,
}
\tikzset{pics/squaroid regular/.style={code={%
\tikzset{squaroid regular/.cd,#1}%
\def\pv##1{\pgfkeysvalueof{/tikz/squaroid regular/##1}}
\draw[thick, pic actions] (45:\pv{radius}) \foreach \x in {135,225,...,359}
{
-- (\x:\pv{radius})}%
-- cycle (90:\pv{radius})%
(0,\pv{radius}) coordinate node[above, scale=2.5] {\pv{lab}}
;
}
},
}
\tikzset{
pentagoid regular/.cd,
radius/.initial=3cm,
lab/.initial=,
}
\tikzset{pics/pentagoid regular/.style={code={%
\tikzset{pentagoid regular/.cd,#1}%
\def\pv##1{\pgfkeysvalueof{/tikz/pentagoid regular/##1}}
\draw[thick, pic actions] (18:\pv{radius}) \foreach \x in {90,162,...,359}
{
-- (\x:\pv{radius})}%
-- cycle (90:\pv{radius})%
(0,\pv{radius}) coordinate node[above, scale=2.5] {\pv{lab}}
;
}
},
}
%%%%%%%%%%%%%%%%%% DOCUMENT %%%%%%%%%%%%%%%%%%
% Radius of regular polygons
\newdimen\R
\R=2cm
\begin{document}
\begin{tikzpicture}[font={\sansmath\sffamily},thick, line cap=round, line join=round, >=latex, x=1.0cm, y=1.0cm]
% Indicate the boundary of the regular polygons
\pic {triangloid regular={lab=Triangle}};
\pic[xshift=7cm] {squaroid regular={lab=Square}};
\pic[yshift=-7cm] {pentagoid regular={lab=Pentagone}};
\end{tikzpicture}
\end{document}
输出:
答案2
当你在等待 TikZ 调试服务(坦率地说,这超出了我的能力范围)时,这里有一个版本元帖子纯粹为了比较。
\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\usepackage{fontspec}
\setmainfont{TeX Gyre Adventor}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
string name[];
name[3] = "Triangle";
name[4] = "Square";
name[5] = "Pentagon";
name[6] = "Hexagon";
name[7] = "Heptagon";
name[8] = "Octagon";
vardef polygon(expr n, r) =
for i=0 upto n-1: up scaled r rotated (360/n*i) -- endfor cycle
enddef;
beginfig(1);
for i=3 upto 8:
path p;
p = polygon(i, 60) shifted -144 (i mod 2, floor ((i-1)/2));
fill p withcolor 7/8[blue, white];
draw p;
label.top(name[i], point 0 of p);
endfor
endfig;
\end{mplibcode}
\end{document}
这已包含在内luamplib
,因此请使用 进行编译lualatex
。
答案3
该shapes.geometric
库已内置这些多边形。
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}[font=\sffamily]
\foreach \X [count=\Y starting from 3] in
{Triangle,Square,Pentagon,Hexagon,Heptagon,Octagon,Nonagon,Decagon}
{\pgfmathsetmacro{\myx}{2.5*Mod(\Y-1,2)}
\pgfmathsetmacro{\myy}{-3*int((\Y-3)/2)}
\node[regular polygon,regular polygon sides=\Y,draw,
minimum size=2cm,label=below:\X] at (\myx,\myy){};
}
\end{tikzpicture}
\end{document}