在 foreach 中定义和使用路径

在 foreach 中定义和使用路径

我看过,并且我无法编译和使用我在同一个 foreach 循环内命名的线的交点。

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,intersections}

\begin{document}
    \begin{tikzpicture}
        \pgfmathsetmacro{\xsidelength}{2}
        \pgfmathsetmacro{\ysidelength}{1.5}
        \path[draw, dashed,name path global=star2] (0,0) --
            ++(\xsidelength,0) --
            ++(45:\ysidelength) --
            ++(-\xsidelength,0) --
            cycle;

        \pgfmathsetmacro{\halfylength}{\ysidelength/2}
        \coordinate (O) at ($(\xsidelength / 2,0)+(45:\halfylength)$);
        \node[draw, circle, inner sep=1pt] at (O) {};

        \foreach \angle[count=\i] in {22.5,0,...,-225}
        {
            \path[name path global/.expanded=line\i] (O) -- ++(\angle:\xsidelength);
            \draw[name intersections = {of {line\i} and star2}](intersection\i) circle (2pt);
        }
    \end{tikzpicture}
\end{document}

第一条路径被绘制出来了,但是它的名字显然在 foreach 中是未知的,上面一行也没有定义它的名字

我遇到了以下错误

:26: Package pgfkeys Error: I do not know the key '/tikz/intersection/of {line1} and star2' and I am going to ignore it. Perhaps you misspelled it.

:26: Package tikz Error: I do not know the path named `'. Perhaps you misspelt it.

:26: Package pgf Error: No shape named intersection1 is known.

虽然这个例子并不是很简单,但我看不出它与已回答的问题有什么不同,除了我试图在同一个 foreach 中重用名称

答案1

拼写错误和一些小错误(!在下面的代码中用 标记):

  • \xvsidelength> \xsidelength(拼写错误 v)
  • (invtersection\i)> (intersection-1)(输入错误 v & -1 因为你指的是每个组合的第一个交点)
  • = {of {line\i}> = {of={line\i}(缺失=)
\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,intersections}

\begin{document}
    \begin{tikzpicture}
    \pgfmathsetmacro{\xsidelength}{2}
    \pgfmathsetmacro{\ysidelength}{1.5}
    \path[draw, dashed,name path global=star2] (0,0) --
    ++(\xsidelength,0) --
    ++(45:\ysidelength) --
    ++(-\xsidelength,0) --
    cycle;

    \pgfmathsetmacro{\halfylength}{\ysidelength/2}
    \coordinate (O) at ($(\xsidelength / 2,0)+(45:\halfylength)$);
    \node[draw, circle, inner sep=1pt] at (O) {};

    \foreach \angle[count=\i] in {22.5,0,...,-225}
    {
        \path[name path global/.expanded=line\i] (O) -- ++(\angle:\xsidelength); % !
        \draw[name intersections = {of ={line\i} and star2}](intersection-1) circle (2pt); % !!!
    }
    \end{tikzpicture}
\end{document}

结果

相关内容