我正在尝试定义一个由三个接一个的矩形组成的 TikZ 节点样式,就像下面的代码一样:
\documentclass[tikz]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}
\node[draw,
minimum width=3cm,
minimum height=1.2cm,
outer sep=0] (subr) at (0,0) {Subroutine};
\draw (subr.north west) -- ++(-0.3,0) |- (subr.south west);
\draw (subr.north east) -- ++(0.3,0) |- (subr.south east);
\end{tikzpicture}
\end{document}
我想将以上所有内容捕获到如下所示的单个节点样式中:
\tikzstyle{subroutine} = {
draw,
thick, etc...
???
}
所以我可以简单地按如下方式使用它:
\begin{tikzpicture}
\node[subroutine] at (0,0) {Subroutine};
\end{tikzpicture}
(灰色边框来自我的文档查看器,它不是实际 tikz 绘图的一部分)
答案1
TikZ 的库shapes.multipart
可以做这类事情。
\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{shapes.multipart}
\begin{document}
\begin{tikzpicture}
\tikzset{subroutine/.style={
rectangle split, rectangle split horizontal,
rectangle split parts=3,
draw, minimum width=3cm,
minimum height=1.2cm,
outer sep=0}}
\path
(0,0) node[subroutine] {\nodepart{two}subroutine\nodepart{three}}
(0,-1.5) node[subroutine,fill=yellow] {\nodepart{two}routine\nodepart{three}}
;
\end{tikzpicture}
\end{document}
有关详细信息,请参阅第 71.6 节。手册。