使用循环变量命名 \pic 对象

使用循环变量命名 \pic 对象

\pic使用变量时,命名对象的好方法是什么\foreach?我使用中的变量\foreach作为节点名称的一部分。如果我不使用local bounding box,则不会出现形状名称错误。如果我使用local bounding box,则会出现另一个与字段名称相关的错误。下面是我使用的代码。

    \begin{center}
    \begin{tikzpicture}[
        pics/multisquare/.style args={#1/#2}{
            code={%
                \node [draw, minimum height=.4em, minimum width=.8em,
                       fill=#1, inner sep=0pt] (s1) {};
                \node [draw, minimum height=.4em, minimum width=.8em,
                       fill=#2, anchor=north, inner sep=0pt] (s2) at (s1.south) {};}
        },
    ]
        \foreach \y in {0,...,4}
            \pic [local bounding box=node-\y] at (0, -\y) {multisquare=red/blue};

        \foreach \y in {0,...,4}
            \pic [local bounding box=node-\y] at (1.6, -\y) {multisquare=red/blue};

        % -----------------------------------------------------------
        % code below shows an error
        % \foreach \source in {0,...,4}
        %     \foreach \dest in {0,...,4}
        %         \draw [->] (node-\dest) -- (node-\source);
        % -----------------------------------------------------------

        % -----------------------------------------------------------
        % code below does not have an error
        \pic [local bounding box=node-1] at (0, 1) {multisquare=green/yellow};
        \pic [local bounding box=node-2] at (0, 2) {multisquare=green/yellow};

        \draw [->] (node-1) -- (node-2);
        % -----------------------------------------------------------

        % -----------------------------------------------------------
        % code below has an error
        % \pic (l3-1) at (0, 1) {multisquare=green/yellow};
        % \pic (l4-1) at (0, 2) {multisquare=green/yellow};

        % \draw [->] (l3-1) -- (l4-1);
        % -----------------------------------------------------------

    \end{tikzpicture}
    \end{center}

更准确地说,我的计划是定义一个由多个形状(或节点)组合而成的新形状(或新节点)。当使用相对定位时,我想将该新形状(或新节点)用作对象。

答案1

我不完全理解你想做什么,但我认为你被困在如何\node在 a 中引用 a 的地方\pic。你可以这样做:习惯上在 a 中用 a\pic开头-(连字符减号)来命名节点,例如-s1。此外,你可以将 a 命名\pic为节点,例如像这样:

\pic (p1) at (0,1) {multisquare=green/yellow};

\pic现在,它的名称为p1,如果其中有一个名为的节点-s1,则可以使用来引用该节点p1-s1。无需使用边界框,这也可以在\foreach循环内工作。

应用于您的代码,您可以执行如下操作:

\documentclass[border=15pt]{standalone}
\usepackage{tikz}

\begin{document}
    \begin{tikzpicture}[
        pics/multisquare/.style args={#1/#2}{
            code={
                \node[draw, minimum height=.4em, minimum width=.8em,
                    fill={#1}, inner sep=0pt] (-s1) {};
                \node[draw, minimum height=.4em, minimum width=.8em,
                    fill={#2}, anchor=north, inner sep=0pt] (-s2) at (-s1.south) {};
            }
        },
    ]

        \pic (p1) at (0,1) {multisquare={green/yellow}};
        \pic (p2) at (0,2) {multisquare={green/yellow}};

        \draw[->] (p1-s1) -- (p2-s2);


        \foreach \i in {3,4} {
            \pic (p\i) at (0,\i) {multisquare={red/blue}};
        }

        \draw[->] (p3-s1) -- (p4-s2);

    \end{tikzpicture}
\end{document}

在此处输入图片描述


您也可以不使用节点,而只是在您的图形中\pic设置一些坐标:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}

\begin{document}
    \begin{tikzpicture}[
        pics/multisquare/.style args={#1/#2}{
            code={
                \coordinate (-north) at (0,.4em);
                \coordinate (-south) at (0,-.4em);
                \draw[fill=#1] (-.4em,0em) rectangle (.4em,.4em);
                \draw[fill=#2] (-.4em,-.4em) rectangle (.4em,0em);
            }
        }
    ]

        \pic (p1) at (0,1) {multisquare={green/yellow}};
        \pic (p2) at (0,2) {multisquare={green/yellow}};

        \draw[->] (p1-north) -- (p2-south);


        \foreach \i in {3,4} {
            \pic (p\i) at (0,\i) {multisquare={red/blue}};
        }

        \draw[->] (p3-north) -- (p4-south);

    \end{tikzpicture}
\end{document}

在此处输入图片描述


如果您想使用一个节点,但不喜欢引用不同的节点名称,您可以只使用一个节点并用 填充它path picture。您也可以使用多部分节点,但我认为调整尺寸会稍微复杂一些:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}

\begin{document}
    \begin{tikzpicture}[
        multisquare/.style args={#1/#2}{
            draw, minimum height=.8em, minimum width=.8em, inner sep=0pt, 
            path picture={
                \fill[#1] (path picture bounding box.east) 
                    rectangle (path picture bounding box.north west);
                \fill[#2] (path picture bounding box.south east) 
                    rectangle (path picture bounding box.west);
                \draw (path picture bounding box.east) 
                    -- (path picture bounding box.west);    
            }
        }
    ]

        \node[multisquare={green/yellow}] (p1) at (0,1) {};
        \node[multisquare={green/yellow}] (p2) at (0,2) {};

        \draw[->] (p1) -- (p2);


        \foreach \i in {3,4} {
            \node[multisquare={red/blue}] (p\i) at (0,\i) {};
        }

        \draw[->] (p3) -- (p4);

    \end{tikzpicture}
\end{document}

与上面相同的输出。

相关内容