如何在 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){};}}。但是,据我所知,这种语法不太灵活。想象一下,你想向 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},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}

正如你所建议的。

相关内容