我正在创建一个通过 TikZ 自动生成资源和死锁的命令。我有以下可以正常工作的代码:
\usetikzlibrary{shapes,positioning,fit}
\begin{tikzpicture}
\tikzset{
proc/.style={draw,circle},
res/.style={rectangle,draw,minimum height=2.5em, minimum width=2.5em},
resi/.style={circle, inner sep=0.1em,circle,draw,node distance=0.5em},
}
\newcommand{\drawressingle}[2]{%
\node (#1l) {#2};
\node [resi,below=0.15em of #1l] (#1i1) {};
}
\newcommand{\drawres}[4]{%
\ifx&\tikzstyle{placeres}=[];%
\else\tikzstyle{placeres}=[#4];\fi%
\node [placeres] (#1l) {#2};
\node [resi,below=0.15em of #1l] (#1i1) {};
\foreach \i in {2,...,#3}{
\pgfmathtruncatemacro{\h}{\i-1}
\node [resi,below=of #1i\h] (#1i\i) {};
}
}
\drawressingle{r1}{R1}{}
\drawres{r2}{R2}{2}{left=of r1l}
\drawres{r3}{R3}{5}{right=of r1l}
\end{tikzpicture}
r1
我想要做的是创建一个新节点,该节点基本上是一个围绕所有节点的矩形,例如 r1i1,r1i2,...我需要的是如果第一个参数是并且第三个参数是,则可以生成以下内容3
:
\node[fit=(r1i1)(r1i2)(r1i3)] {};
我想不出循环并生成上述内容的方法。
答案1
这是我提出的方案。您必须对其进行调整才能按照自己的意愿处理边距,但我认为这可以回答您的问题。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning,fit}
\begin{document}
\begin{tikzpicture}
\tikzset{
proc/.style={draw,circle},
res/.style={rectangle,draw,minimum height=2.5em, minimum width=2.5em},
resi/.style={circle, inner sep=0.1em,circle,draw,node distance=0.5em},
}
\newcommand{\drawressingle}[2]{%
\node (#1l) {#2};
\node [resi,below=0.15em of #1l] (#1i1) {};
}
\newcommand{\drawres}[4]{%
\ifx&\tikzstyle{placeres}=[];%
\else\tikzstyle{placeres}=[#4];\fi%
\node [placeres] (#1l) {#2};
\node [resi,below=0.15em of #1l] (#1i1) {};
\foreach \i in {2,...,#3}{
\pgfmathtruncatemacro{\h}{\i-1}
\node (#1i\i) [resi,below=of #1i\h] {};
}
}
\newcommand{\drawnode}[2]{%
\coordinate (boxnode) at (#1i1);
\foreach \i in {2,...,#2}{
\node (boxnode) [inner sep=0pt,outer sep=0pt,fit=(boxnode)(#1i\i)] {};
}
\draw (boxnode.south west) rectangle (boxnode.north east);
}
\drawressingle{r1}{R1}{}
\drawres{r2}{R2}{2}{left=of r1l}
\drawres{r3}{R3}{5}{right=of r1l}
\drawnode{r3}{4}
\end{tikzpicture}
\end{document}