如何在tikz中將倒角矩形定義為.pic?

如何在tikz中將倒角矩形定義為.pic?

我正在嘗試生成一個重複包含相同圖形的圖表。請參閱下面的範例圖形:

在此輸入影像描述

這就是我生成它的方式:

\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}

我希望將倒角矩形定義為 .pic,以便稍後可以透過\path命令使用它。

我正在尋找這樣的東西:

\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}

答案1

您在尋找這樣的東西嗎?

\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}

在此輸入影像描述

您可以定義pic 在這種情況下還使用pics/champic/.style={code={\node[mycham] (-node){};}}.然而,據我所知,這種語法不太靈活。想像一下,您想要向圖片傳遞多個參數,如下所示

\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}

在此輸入影像描述

正如您所看到的,使用此處選擇的語法這是沒有問題的,但是使用該/.pic=語法我不知道該怎麼做。

當然,如果你沒有參數,並且確定你永遠不需要參數,你可以這樣做

\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}

正如你所建議的。

相關內容