TransWikia.com

Using pics/port/.style n args = {3} doesn't work correctly in tikzset environment

TeX - LaTeX Asked by Z.J on June 1, 2021

In TikZ i use the pics/ command inside the tikzset enviroment to define the picture port, which is a element of a network switch. As you can see, the call of picture port in line 34 an 38 requires three arguments. The first two contains the values for the style attributes height and width. The third argument contains the port label, which is no value for a style attribute but rather the label of a node element. Unfortunately i don’t succeed to display the port labels correctly. The finished result doesn’t show the label 3 as i enter in line 34 an 38 but rather a mix of argument 2 and other indefinable characters.

I am assuming that you can only pass arguments, which contains values for style attributs.
If there is a better approach then mine to solve this kind of problem, please tell me.

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}

One Answer

If you say style n args={3} then this literally expects three TeX arguments, which are not to be separated by commas. Just removing the commas gives

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}

enter image description here

However, I would not do things in this way. I would use pgf keys, with which you can use a comma-separated list. This allows you to set some default values, spares you from having to remember the order of the argument, and allows you to add more keys later on without losing backwards compatibility.

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}

Correct answer by user238301 on June 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP