迭代一组极坐标

迭代一组极坐标

绘制五边形的五个顶点:

\begin{tikzpicture}
\def\Verts{
    {0:1},
    {72:1},
    {144:1},
    {216:1},
    288:1
    }

    \foreach \Vert [count=\i] in \Verts {
        % Draw vertex
        \node at (\Vert) {\textbullet};
    }
\end{tikzpicture}

但是这样不会并且会报告错误No shape named {288:1} is known

\begin{tikzpicture}
\def\Verts{
    {0:1},
    {72:1},
    {144:1},
    {216:1},
    {288:1}
    }

    \foreach \Vert [count=\i] in \Verts {
        % Draw vertex
        \node at (\Vert) {\textbullet};
    }
\end{tikzpicture}

为什么数组的最后一个元素不需要用括号括起来?

答案1

花括号不是问题。问题在于解析器看到的多余空间。您可以通过添加 来解决这个问题%

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}
\def\Verts{
    {0:1},
    {72:1},
    {144:1},
    {216:1},
    {288:1}%
    }

    \foreach \Vert [count=\i] in \Verts {
        % Draw vertex
        \node at (\Vert) {\textbullet};
    }
\end{tikzpicture}
\end{document}

在此处输入图片描述

或者你可以使用

\def\Verts{
    {0:1},
    {72:1},
    {144:1},
    {216:1},
    {288:1}}

再次,多余的空间消失了。(其他坐标在结束的 后面有一个逗号},因此不存在这样的空间。)

相关内容