# Advice on How to Draw Diagram in TIKZ

I am new to tikz and I am trying to draw the attached image.

I have had a go at drawing the shapes as below, but have several questions.

1. How do I position the label for each of the regions to be at the top left corner of the dotted box?
2. Is it possible to draw the NodeA1,
NodeA2 and NodeA3 connected horizontally / vertically centred in the RegionA node?
3. Is it possible to draw Region B relative to region A - ie 1 cm below?
4. How do I draw the lines connecting the nodes to the centre of the sides? Ie NodeA1 to NodesA2 and NodeA3?
5. How do I draw the lines form Master to NodeA1 and NodeB1?

Here is the basic code I have already completed. In this code I have draw the basic shapes and tried to position them using absolute coordinates and join the lines between the nodes.

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{positioning}

\tikzset{
block/.style={rectangle, draw, fill=blue!10, rounded corners, text centered}
}

\begin{document}
\begin{tikzpicture}

%Master
\node[rectangle, draw, block] (nodeA1) {\begin{tabular}{l} Master Label 1 \\ Node Master 2 \end{tabular}};

%Region A
\node [label=left, style=dashed, minimum width=15cm, minimum height = 5cm, draw] (RegionA) {Region A};
\node[rectangle, draw, block, below =-3.0 cm of RegionA] (nodeA1) {\begin{tabular}{l} Node A1 Label 1 \\ Node A1 Label 2 \end{tabular}};

\node[rectangle, draw, block, above right = 0.25cm and 2cm of nodeA1] (nodeA2) {\begin{tabular}{l} Node A2 Label 1 \\ Node A2 Label 2 \end{tabular}};

\node[rectangle, draw, block, below right = 0.25cm and 2cm of nodeA1] (nodeA3) {\begin{tabular}{l} Node A3 Label 1 \\ Node A3 Label 2 \end{tabular}};

\draw  (nodeA1) -- (nodeA2);
\draw  (nodeA1) -- (nodeA3);

% Region B
\node [style=dashed, minimum width=15cm, minimum height = 5cm, draw] (RegionB) at (0, -6cm) {Region B};

\node[rectangle, draw, block, below =-3.0 cm of RegionB] (nodeB1) {\begin{tabular}{l} Node B1 Label 1 \\ Node B1 Label 2 \end{tabular}};

\node[rectangle, draw, block, above right = 0.25cm and 2cm of nodeB1] (nodeB2) {\begin{tabular}{l} Node B2 Label 1 \\ Node B2 Label 2 \end{tabular}};

\node[rectangle, draw, block, below right = 0.25cm and 2cm of nodeB1] (nodeB3) {\begin{tabular}{l} Node B3 Label 1 \\ Node B3 Label 2 \end{tabular}};

\draw  (nodeA1) -- (nodeA2);
\draw  (nodeA1) -- (nodeA3);

\end{tikzpicture}
\end{document}


1. After creating the RegionA node, you could add a new node as follows:

\node [below right] at (RegionA.north west) {Region A};

2. Probably one of the most asked TikZ questions. Vertical and horizontal lines in pgf-tikz shows a few methods. In the code below I took a slightly different approach, using a relative coordinate followed by a |- path (see TikZ: What EXACTLY does the the |- notation for arrows do?).

3. Yes. The code below demonstrates one possible method, though this particular approach assumes that the two regions have the same size.

4. In general, if you want to specify the point a line leaves/enters a node, specify the anchor. For example, nodeA.east is the right side of the node. In addition to north, south, west, east, north east, south west etc., you can also specify a degree. For example nodeA.45 is the same as nodeA.north east, and nodeA.90 is nodeA.north.

5. I assumed the master node was supposed to be placed above RegionA. The line was drawn using a -| coordinate specification (see link in point 2.)

There are certainly several other approaches that could be taken to draw such a diagram. Such as using a tree or a \matrix to draw the two sub-structures.

Note I simplified things a bit. I moved the node distance to the tikzpicture options, and I removed draw, rectangle from all the individual nodes, as those settings are included in the block style. I also set align=left in that style, and remove the tabular. There's nothing wrong with the tabular approach though, so use that if you prefer it.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,fit}

\tikzset{
block/.style={
rectangle,
draw,
fill=blue!10,
rounded corners,
align=left, % allows for line breaks in node without tabular
inner sep=\tabcolsep
}
}

\begin{document}
\begin{tikzpicture}[node distance=0.25cm and 2.5cm]

%Region A
\node[block] (nodeA1) { Node A1 Label 1 \\ Node A1 Label 2 };

\node[block, above right=of nodeA1] (nodeA2) { Node A2 Label 1 \\ Node A2 Label 2 };

\node[block, below right=of nodeA1] (nodeA3) { Node A3 Label 1 \\ Node A3 Label 2 };

\draw  (nodeA1.east) -- ++(1.5cm,0) |- (nodeA2);
\draw  (nodeA1.east) -- ++(1.5cm,0) |- (nodeA3);

\node [
dashed,
draw,
inner ysep=1cm,inner xsep=1.5cm,
fit=(nodeA1)(nodeA2)(nodeA3)] (RegionA) {};

% label for region
\node [below right] at (RegionA.north west) {Region A};

% Region B
\node [
dashed,
draw,
inner ysep=1cm,inner xsep=1.5cm,
fit=(nodeA1)(nodeA2)(nodeA3), % same size as RegionA
below=1cm of RegionA % but placed below it
] (RegionB) {};

\node [below right] at (RegionB.north west) {Region B};

\node[block] (nodeB1) at (RegionB -| nodeA1) { Node B1 Label 1 \\ Node B1 Label 2 };

\node[block, above right = of nodeB1] (nodeB2) { Node B2 Label 1 \\ Node B2 Label 2 };

\node[block, below right = of nodeB1] (nodeB3) { Node B3 Label 1 \\ Node B3 Label 2 };

\draw  (nodeB1.east) -- ++(1.5cm,0) |- (nodeB2);
\draw  (nodeB1.east) -- ++(1.5cm,0) |- (nodeB3);

% master node
\node [above=of RegionA, xshift=-2.5mm, block] (nodeM) {Master Label 1 \\ Node Master 2};

\draw (nodeM) -- (nodeM |- nodeB1);

\end{tikzpicture}

\end{document}