# Dynamically calculate fitter in TikZ

I'm creating a command for automatic generation of resources and deadlocks through TikZ. I have the following code that works correctly:

\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&#4&\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}


What I want to do is create a new node that is basically a rectangle around all of the nodes called e.g. r1i1, r1i2, .... What I will need is something that can generate the following if the first param is r1 and the third param is 3:

\node[fit=(r1i1)(r1i2)(r1i3)] {};


I can't think of a way to loop over and generate the above.

-

This is what I came with. You will have to tweak it to do what you want with margins, but I think this answers your question.

\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&#4&\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}

-
Thanks. Works with a bit of tweaking. Essentially, I had to add another boxnodeouter and give it an inner sep. Also changed some anchors around but the general idea works. –  recluze Feb 21 '12 at 6:50