在 \tikzset 环境中使用 pics/port/.style n args = {3} 无法正常工作

在 \tikzset 环境中使用 pics/port/.style n args = {3} 无法正常工作

在 TikZ 中,我使用 \tikzset 环境中的 pics/ 命令来定义图片端口,它是网络交换机的一个元素。如您所见,第 34 行和第 38 行的图片端口调用需要三个参数。前两个参数包含样式属性高度和宽度的值。第三个参数包含端口标签,它不是样式属性的值,而是节点元素的标签。不幸的是,我无法正确显示端口标签。最终结果没有显示我在第 34 行和第 38 行输入的标签 3,而是参数 2 和其他无法定义的字符的混合。

我假设您只能传递包含样式属性值的参数。如果有比我的方法更好的方法来解决此类问题,请告诉我。

\usepackage{tikz}
\usetikzlibrary{calc, shadings, shadows, shapes.arrows}

\tikzset{
    pics/port/.style n args = {3}{% style, size, label
        code = {
            \coordinate[] (main) at (0cm,0cm);
            \coordinate[] (labelposition) at (.5cm,1.2cm);
            \draw[#1] (main) rectangle (#2);
            \draw[fill=black] (.1cm,0cm) rectangle (.9cm,.5cm);
            \draw[fill=black] (.2cm,.5cm) rectangle (.8cm,.7cm);
            \draw[fill=black] (.4cm,.7cm) rectangle (.6cm,.8cm);
            \node (labelnode) at (labelposition) {#3};  
        }
    },
    rack switch/.style={
        fill=white, draw,
        minimum width=18cm,
        minimum height=4cm,
        path picture={
            %Gehäuse des Switch
            \coordinate (mainpoint) at (0cm,0cm);
            %
            \draw[top color=gray!5,bottom color=gray!40]
            (path picture bounding box.south west) rectangle 
            (path picture bounding box.north east);
            %
            \coordinate (A-west) at ([xshift=0.8cm,yshift=.1cm]path picture bounding box.west);
            \coordinate (A-center) at ($(path picture bounding box.center)!0!(path picture bounding box.south)$);
            \coordinate (A-east) at (path picture bounding box.east);
            \foreach \x in {0,1.2,2.4,3.6, 4.8, 6, 7.2, 8.4, 9.6, 10.8, 12, 13.2}{
                %innerhal von $...$ die x-Koordinate weitersetzen. 
                \draw ($(A-west)+(\x,0cm)$) pic{port={{thin, fill=gray!20},{1cm,1.5cm},3}};
            }
            \foreach \x in {1,2.2,3.4, 4.6, 5.8, 7, 8.2, 9.4, 10.6, 11.8, 13, 14.2}{
                %innerhal von $...$ die x-Koordinate weitersetzen. 
                \draw ($(A-west)+(\x,-.2cm)$) pic[rotate=180]{port={{thin, fill=gray!20},{1cm,1.5cm},3}};
            }           
        }
    }
}   

\begin{document}
    \begin{tikzpicture} 
        \node(Switch1) at (0,0) [rack switch]{};
    \end{tikzpicture}   
\end{document}

答案1

如果你说,style n args={3}那么这实际上需要三个 TeX 参数,它们是不是用逗号分隔。只需删除逗号即可

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc, shadings, shadows, shapes.arrows}
\tikzset{
    pics/port/.style n args = {3}{% style, size, label
        code = {
            \coordinate[] (main) at (0cm,0cm);
            \coordinate[] (labelposition) at (.5cm,1.2cm);
            \draw[#1] (main) rectangle (#2);
            \draw[fill=black] (.1cm,0cm) rectangle (.9cm,.5cm);
            \draw[fill=black] (.2cm,.5cm) rectangle (.8cm,.7cm);
            \draw[fill=black] (.4cm,.7cm) rectangle (.6cm,.8cm);
            \node (labelnode) at (labelposition) {#3};  
        }
    },
    rack switch/.style={
        fill=white, draw,
        minimum width=18cm,
        minimum height=4cm,
        path picture={
            %Gehäuse des Switch
            \coordinate (mainpoint) at (0cm,0cm);
            %
            \draw[top color=gray!5,bottom color=gray!40]
            (path picture bounding box.south west) rectangle 
            (path picture bounding box.north east);
            %
            \coordinate (A-west) at ([xshift=0.8cm,yshift=.1cm]path picture bounding box.west);
            \coordinate (A-center) at ($(path picture bounding box.center)!0!(path picture bounding box.south)$);
            \coordinate (A-east) at (path picture bounding box.east);
            \foreach \x in {0,1.2,2.4,3.6, 4.8, 6, 7.2, 8.4, 9.6, 10.8, 12, 13.2}{
                %innerhal von $...$ die x-Koordinate weitersetzen. 
                \draw ($(A-west)+(\x,0cm)$) pic{port={{thin, fill=gray!20}{1cm,1.5cm}3}};
            }
            \foreach \x in {1,2.2,3.4, 4.6, 5.8, 7, 8.2, 9.4, 10.6, 11.8, 13, 14.2}{
                %innerhal von $...$ die x-Koordinate weitersetzen. 
                \draw ($(A-west)+(\x,-.2cm)$) pic[rotate=180]{port={{thin, fill=gray!20}{1cm,1.5cm}3}};
            }           
        }
    }
}   

\begin{document}
    \begin{tikzpicture} 
        \node(Switch1) at (0,0) [rack switch]{};
    \end{tikzpicture}   
\end{document}

在此处输入图片描述

但是,我不会这样做。我会使用 pgf 键,您可以使用逗号分隔的列表。这允许您设置一些默认值,免除您记住参数顺序的麻烦,并允许您稍后添加更多键而不会失去向后兼容性。

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc, shadings, shadows, shapes.arrows}
\tikzset{
    pics/port/.style={% style, size, label
        code = {\tikzset{port/.cd,#1}
            \coordinate[] (main) at (0cm,0cm);
            \coordinate[] (labelposition) at (.5cm,1.2cm);
            \draw[port/rect] (main) rectangle (\pgfkeysvalueof{/tikz/port/tr});
            \draw[fill=black] (.1cm,0cm) rectangle (.9cm,.5cm);
            \draw[fill=black] (.2cm,.5cm) rectangle (.8cm,.7cm);
            \draw[fill=black] (.4cm,.7cm) rectangle (.6cm,.8cm);
            \node (labelnode) at (labelposition) 
                {\pgfkeysvalueof{/tikz/port/label}};  
        }
    },port/.cd,rect/.style={thin, fill=gray!20},tr/.initial={1cm,1cm},label/.initial={},
    /tikz/.cd,
    rack switch/.style={
        fill=white, draw,
        minimum width=18cm,
        minimum height=4cm,
        path picture={
            %Gehäuse des Switch
            \coordinate (mainpoint) at (0cm,0cm);
            %
            \draw[top color=gray!5,bottom color=gray!40]
            (path picture bounding box.south west) rectangle 
            (path picture bounding box.north east);
            %
            \coordinate (A-west) at ([xshift=0.8cm,yshift=.1cm]path picture bounding box.west);
            \coordinate (A-center) at ($(path picture bounding box.center)!0!(path picture bounding box.south)$);
            \coordinate (A-east) at (path picture bounding box.east);
            \foreach \x in {0,1.2,2.4,3.6, 4.8, 6, 7.2, 8.4, 9.6, 10.8, 12, 13.2}{
                %innerhal von $...$ die x-Koordinate weitersetzen. 
                \draw ($(A-west)+(\x,0cm)$) 
                pic{port={rect/.style={thin, fill=gray!20},tr={1cm,1.5cm},label=3}};
            }
            \foreach \x in {1,2.2,3.4, 4.6, 5.8, 7, 8.2, 9.4, 10.6, 11.8, 13, 14.2}{
                %innerhal von $...$ die x-Koordinate weitersetzen. 
                \draw ($(A-west)+(\x,-.2cm)$) 
                pic[rotate=180]{port={rect/.style={thin, fill=gray!20},
                    tr={1cm,1.5cm},label=3}};
            }           
        }
    }
}   

\begin{document}
    \begin{tikzpicture} 
        \node(Switch1) at (0,0) [rack switch]{};
    \end{tikzpicture}   
\end{document}

相关内容