Wie definiert man ein abgeschrägtes Rechteck in Tikz als .pic?

Wie definiert man ein abgeschrägtes Rechteck in Tikz als .pic?

Ich versuche, ein Diagramm zu erstellen, das immer wieder die gleichen Grafiken enthält. Nachfolgend sehen Sie eine Beispielgrafik:

Bildbeschreibung hier eingeben

So erzeuge ich es:

\documentclass[tikz, border=2px]{standalone}
\usetikzlibrary{shapes.misc, shapes.geometric}

\begin{document}
\tikzset{
    line/.style={-, draw=black!30, line width=1pt},
}

\begin{tikzpicture}[node distance=2cm]
    \node[draw=black!30, rectangle, minimum height=8mm, minimum width=8mm,line width=1pt, chamfered rectangle,chamfered rectangle corners={south west, south east},chamfered rectangle xsep=2pt, below] at (0, 0) (b1) {} ;
    \node[draw=black!30, rectangle, minimum height=8mm, minimum width=8mm,line width=1pt, chamfered rectangle,chamfered rectangle corners={south west, south east},chamfered rectangle xsep=2pt, below] at (1, 1) (b2) {} ;
    \node[draw=black!30, rectangle, minimum height=8mm, minimum width=8mm,line width=1pt, chamfered rectangle,chamfered rectangle corners={south west, south east},chamfered rectangle xsep=2pt, below] at (0, 2) (b3) {} ;
    \draw[line] (b1) -- (b2) -- (b3);
\end{tikzpicture}
\end{document}

Ich möchte das abgeschrägte Rechteck als .pic definieren, damit es später mit \pathBefehlen verwendet werden kann.

Ich suche so etwas:

\tikzset{
line/.style={-, draw=black!30, line width=1pt},
box/.pic={
    \draw[draw=black!30, rectangle, minimum height=8mm, minimum width=8mm,line width=1pt, chamfered rectangle,chamfered rectangle corners={south west, south east},chamfered rectangle xsep=2pt, below] (0, 0);
    },
}

\begin{tikzpicture}[node distance=2cm]
    \path[line] (0, 0) pic {box} --
                (1, 1) pic {box} --
                (0, 2) pic {box};
\end{tikzpicture}

Antwort1

Suchen Sie nach so etwas?

\documentclass[tikz, border=2px]{standalone}
\usetikzlibrary{shapes.misc, shapes.geometric}

\begin{document}
\tikzset{
    line/.style={-, draw=black!30, line width=1pt},
}

\begin{tikzpicture}[mycham/.style={draw=black!30, rectangle, minimum height=8mm,
minimum width=8mm,line width=1pt, chamfered rectangle,chamfered rectangle
corners={south west, south east},chamfered rectangle
xsep=2pt},pics/champic/.style={code={\node[mycham] (-node){};}}]
    \path (0, 0) pic (b1) {champic}
    -- (1, 1) pic (b2){champic} 
    -- (0,2)  pic (b3){champic};
    \draw[line] (b1-node) -- (b2-node) -- (b3-node);
\end{tikzpicture}
\end{document}

Bildbeschreibung hier eingeben

Sie können diepic in diesem Fallauch mit pics/champic/.style={code={\node[mycham] (-node){};}}. Soweit ich weiß, ist diese Syntax jedoch weniger flexibel. Stellen Sie sich vor, Sie möchten dem Bild mehr als ein Argument übergeben, wie in

\documentclass[tikz, border=2px]{standalone}
\usetikzlibrary{shapes.misc, shapes.geometric}

\begin{document}
\tikzset{
    line/.style={-, draw=black!30, line width=1pt},
}

\begin{tikzpicture}[mycham/.style={draw=black!30, rectangle, minimum height=8mm,
minimum width=8mm,line width=1pt, chamfered rectangle,chamfered rectangle
corners={south west, south east},chamfered rectangle
xsep=2pt},pics/champic/.style n args={2}{code={\node[mycham,#2] (-node){#1};}}]
    \path (0, 0) pic (b1) {champic={A}{blue}}
    -- (1, 1) pic (b2){champic={B}{red}} 
    -- (0,2)  pic (b3){champic={C}{green!70!black}};
    \draw[line] (b1-node) -- (b2-node) -- (b3-node);
\end{tikzpicture}
\end{document}

Bildbeschreibung hier eingeben

Wie man sieht ist das mit der hier gewählten Syntax kein Problem, allerdings /.pic=wüsste ich bei der Syntax nicht wie ich das machen sollte.

Wenn Sie keine Parameter haben und sicher sind, dass Sie diese nie benötigen, können Sie natürlich Folgendes tun:

\documentclass[tikz, border=2px]{standalone}
\usetikzlibrary{shapes.misc, shapes.geometric}

\begin{document}
\tikzset{
    line/.style={-, draw=black!30, line width=1pt},
}

\begin{tikzpicture}[mycham/.style={draw=black!30, rectangle, minimum height=8mm,
minimum width=8mm,line width=1pt, chamfered rectangle,chamfered rectangle
corners={south west, south east},chamfered rectangle
xsep=2pt},champic/.pic={\draw (0,0) node[mycham] (-node){};}]
    \path (0, 0) pic (b1) {champic}
    -- (1, 1) pic (b2){champic} 
    -- (0,2)  pic (b3){champic};
    \draw[line] (b1-node) -- (b2-node) -- (b3-node);
\end{tikzpicture}
\end{document}

wie Sie vorschlagen.

verwandte Informationen