我想用tikz绘制太小的正多边形shapes.geometric。我的目标是让它们与文本保持一致:

但是当我尝试编写一个tikzset以便能够快速轻松地绘制任何给定的多边形时,我收到一个错误! Illegal unit of measure (pt inserted).

tikzset我认为问题在于我对定义循环的集合内部的参数执行了计算for。但我不知道如何将TeX参数识别为数字。

以下是我对上述文本的工作代码:

\documentclass[border=0.2cm]{standalone}
\usepackage{tikz}
\begin{document}
\newdimen\R
\R=0.1cm
foo
\begin{tikzpicture}
    % Indicate the boundary of the regular polygons

    \draw[xshift=2.5\R] (0:\R) \foreach \x in {90,180,...,359} {
            -- (\x:\R)
        } -- cycle (90:\R) ;

\end{tikzpicture}
baz 
\begin{tikzpicture}
    % Indicate the boundary of the regular polygons

    \draw[xshift=2.5\R] (0:\R) \foreach \x in {72,144,...,360} {
            -- (\x:\R)
        } -- cycle (90:\R) ;

\end{tikzpicture}
\end{document}

得到了多边形的代码。)

错误代码如下:

\documentclass[border=0.2cm]{standalone}
\usepackage{tikz}

\tikzset{
pics/Polygon/.style n args ={2}{%
background code = {
    \draw[xshift=2.5*#2] (0:#2) \foreach \x in {360/#1,2*360/#1,...,360} {
            -- (\x:#2)
        } -- cycle (360/3:#2);
    }
}
}
\begin{document}

\begin{tikzpicture}
\draw (0,0) pic {Polygon={5}{0.8cm}};
\end{tikzpicture}

\end{document}

通过尝试不同的选项,我知道问题在于,360/#1并且2*360/#1在用于定义循环的集合中不能很好地工作for

1

  • 为什么要在 后面添加坐标cycle?这只是移动到该点。


    – 


6 个回答
6

正多边形太小,无法shapes.geometric

设置inner sep为零来控制它们的大小minimum size(设置半径)。

不过,如果你不需要它成为节点,就不需要使用形状。使用sharp cycle具有内置循环的图可以轻松实现。

由于绘图非常简单,我还添加了 PGF 版本。

代码

\documentclass[varwidth, border=3pt]{standalone}
\usepackage{tikz} \usetikzlibrary{shapes.geometric}
\newcommand*\tikzPolygonShape[3][]{%
  \tikz[#1]
    \node[draw, inner sep=+0pt, minimum size={2*(#3)}, rotate=90-180/(#2),
      shape=regular polygon, regular polygon sides={#2}]{};}

\newcommand*\tikzPolygonPlot[3][]{%
  \tikz[#1]
    \draw plot[sharp cycle, samples at={1, ..., #2}] ({\x*360/(#2)}:{#3});}

\newcommand*\tikzPolygonPGF[2]{%
  \begin{pgfpicture}
    \pgfplotfunction{\x}{1, ..., #1}{\pgfpointpolar{\x*360/(#1)}{#2}}
    \pgfusepath{stroke}
  \end{pgfpicture}}

\pgfset{foreach/eval col/.style={evaluate={\c=(\Sides00-300)/7;}}}

\begin{document}
foo \tikzPolygonShape{4}{.3em} baz \tikzPolygonShape{5}{.3em} --%
\foreach[eval col] \Sides in {3, ..., 10}{
  \tikzPolygonShape[red!\c!blue]{\Sides}{.3em}}

foo \tikzPolygonPlot{4}{.3em} baz \tikzPolygonPlot{5}{.3em} --%
\foreach[eval col] \Sides in {3, ..., 10}{
  \tikzPolygonPlot[red!\c!blue]{\Sides}{.3em}}

foo \tikzPolygonPGF{4}{.3em} baz \tikzPolygonPGF{5}{.3em} --%
\foreach[eval col] \Sides in {3, ..., 10}{
  \color{red!\c!blue}\tikzPolygonPGF{\Sides}{.3em}}
\end{document}

输出

1

  • 关于字符高度和宽度的一个好建议


    – 

\documentclass[border=0.2cm]{standalone}
\usepackage{tikz}

\tikzset{
pics/Polygon/.style n args ={2}{%
background code = {
    \draw[xshift=2.5*#2] (0:#2) \foreach \x [evaluate=\x as \angle using \x*360/#1] in {1,2,...,#1} {-- (\angle:#2)} -- cycle ;
    }
}
}
\begin{document}

\begin{tikzpicture}
\draw (0,0) pic {Polygon={5}{0.8cm}};
\end{tikzpicture}

\end{document}

\documentclass[border=0.2cm]{standalone}
\usepackage{tikz}
\tikzset{
    pics/Polygon/.style n args={2}{background code={
        \draw (0:#2) \foreach \x in {2,...,#1} {-- (-360/#1+360*\x/#1:#2)} -- cycle;
}}}
\begin{document}
\begin{tikzpicture}
\draw (0,0) pic {Polygon={5}{0.8cm}};
\end{tikzpicture}
\end{document}

您只需parse = true添加\foreach, \foreach[parse = true] ...

\documentclass[border=0.2cm]{standalone}
\usepackage{tikz}

\tikzset{
pics/Polygon/.style n args ={2}{%
background code = {
    \draw[xshift=2.5*#2] (0:#2) \foreach[parse = true] \x in {360/#1,2*360/#1,...,360} {
            -- (\x:#2)
        } -- cycle (360/3:#2);
    }
}
}
\begin{document}

\begin{tikzpicture}
\draw (0,0) pic {Polygon={5}{0.8cm}};
\end{tikzpicture}

\end{document}

您还可以使用lualatex支持通过+轻松地完成此操作luamplib

用 编译它lualatex

\documentclass{article}
\usepackage{luamplib}
\newcommand\poly[1]{\ensuremath{\mathbin{\vcenter{\begin{mplibcode}beginfig(0); 
  draw for i=1 upto #1: 3 dir (360 / #1 * (i-1)) -- endfor cycle;
endfig;\end{mplibcode}}}}}
\begin{document}

foo \poly{4} baz \poly{5}. As a math binary:  \(5 \poly{3} 7\).

\end{document}

如果您愿意PS-Tricks,请尝试以下步骤:

\documentclass{book}
\usepackage{pstricks-add}
\usepackage{pst-poly}

\begin{document}

\begin{pspicture}[showgrid=false](-1,-1)(1,1)
\PstPolygon[PstPicture=false]
\end{pspicture}

\end{document}