我已经定义了正 n 边形,逐个输出它们没有问题。但是,我想在一页上排列多个正 n 边形。运行我的代码时,有些图形超出了页边距。我该怎么做?
\documentclass[12pt,a4paper]{article}
\usepackage{tikz}
\begin{document}
% General function: Draw a regular n-sided polygon
\newcommand{\drawPolygon}[1]{%
\begin{tikzpicture}
\pgfmathsetmacro{\n}{#1} % Set the number of sides
\pgfmathsetmacro{\angleStep}{360/\n} % Calculate the step angle for each side
\pgfmathsetmacro{\startAngle}{-90 + \angleStep} % Calculate the starting angle
% Draw vertices
\foreach \i in {1,...,\n} {
\node[draw, circle, fill=white, inner sep=2pt] (v\i) at ({\startAngle + (\i - 1) * \angleStep}:2) {};
}
% Connect adjacent vertices
\foreach \i in {1,...,\n} {
\pgfmathtruncatemacro{\next}{mod(\i, \n) + 1}
\draw (v\i) -- (v\next);
}
\end{tikzpicture}
}
\begin{figure}
\centering
\begin{tikzpicture}
\foreach [count=\i] \n in {3, 4, 5, 6, 7, 8, 9, 10} {
\begin{scope}
\drawPolygon{\n}
\end{scope}
}
\end{tikzpicture}
\caption{Regular 3- to 10-gons}
\end{figure}
\end{document}
我希望它以类似于网站(链接)的方式显示,每行 3 个项目,
另一个需要注意的问题是,当我使用图形标题时,标题和多边形中间之间的间隙似乎太大。
最佳答案
2
您不需要tikzpicture
使用\foreach
。我甚至会说您不应该嵌套tikzpicture
s。
\documentclass[12pt,a4paper]{article}
\usepackage{tikz}
\begin{document}
% General function: Draw a regular n-sided polygon
\newcommand{\drawPolygon}[2][1]{%
\begin{tikzpicture}[scale=#1]
\pgfmathsetmacro{\n}{#2} % Set the number of sides
\pgfmathsetmacro{\angleStep}{360/\n} % Calculate the step angle for each side
\pgfmathsetmacro{\startAngle}{-90 + \angleStep} % Calculate the starting angle
% Draw vertices
\foreach \i in {1,...,\n} {
\node[draw, circle, fill=white, inner sep=2pt] (v\i) at
({\startAngle + (\i - 1) * \angleStep}:2) {};
}
% Connect adjacent vertices
\foreach \i in {1,...,\n} {
\pgfmathtruncatemacro{\next}{mod(\i, \n) + 1}
\draw (v\i) -- (v\next);
}
\end{tikzpicture}
}
\begin{figure}
\centering
\foreach [count=\i] \n in {3, 4, ..., 10} {
\makebox[0.2\textwidth]{%
\begin{tabular}[t]{@{}c@{}}
$n=\n$ \\[1ex]
\drawPolygon[0.5]{\n}
\end{tabular}%
}\ifnum\i=4 \\[2ex]\fi
}
\caption{Regular 3- to 10-gons}
\end{figure}
\end{document}
我添加了一个可选参数来\drawPolygon
设置比例。
每个多边形都排版在一个占文本宽度 20% 的框中;该框包含一个顶部对齐的框tabular
,其中第一行是边数。在第四个多边形之后,插入一个换行符(具有垂直间距)。
|
像这样?
获取上图的一种方法是:
\documentclass[12pt,a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{chains,
positioning,
shapes.geometric}
\begin{document}
\begin{figure}[ht]
\centering
\begin{tikzpicture}[
node distance = 23mm and 7mm,
start chain = going right,
N/.style = {regular polygon, regular polygon sides=\i, minimum size=12mm, draw,
node contents={}}
]
\foreach \i in {3,...,5}%
{
\node (n1\i) [N, on chain];
\coordinate[above=3mm of n13] (y);
\node at (y -| n1\i) {$n=\i$};
}
\foreach \i [count=\j from 3] in {6,...,8}%
{
\node (n2\j) [N, below=of y -| n1\j];
\coordinate[above=3mm of n23] (z);
\node at (z -| n2\j) {$n=\i$};
}
\end{tikzpicture}
\caption{Regular 3- to 8-gons}
\end{figure}
\end{document}
正如您所看到的,我使用Ti kregular polygon
Z 库而不是您的宏。shapes.geometric
1
-
還是很好的。
–
|
|