TikZ Diagrams – Create Multi Input-Output Block Diagrams in TikZ

diagramsnode-connectionstikz-pgf

My problem is to define blocks (mainly rectangles) that has labels in them in terms of matrices (mostly block diagonal) such that the n-many arrows that goes in this rectangle visually is at the same height as the corresponding element on the diagonal of the label. Consider the following example

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[>=stealth]
\node[draw,rectangle,inner sep=0.5cm] (y) at (0,0) {$A$};
\node[draw,rectangle,inner sep=0.3cm] (d) at (0,2) {$\begin{array}{cc}B&\\&C \end{array}$};
\draw[->] (y.west) -| ++(-1,1) |- (d.west);
\draw[->] (d.east) -| ++(1,-1) |- (y.east);
\end{tikzpicture}
\end{document}

Now, let me start by creating two subproblems:

  1. When I create this label, the inner sep option of node shape (the visible rectangle around B and C) is kind of annoying to adjust. So I don't think it would survive in a better solution. So this question would be obsolete if the following problem is solved.
  2. I would like to get a handle of the node height resulting from the matrix label that I have put in and then somehow obtain a subdivision of n+1 of this height creating n nodes to connect to and from on the west and east sides. such that in the minimal example above I would connect to two arrows going out from A (not from the same location but separate and equally dividing the height of A) and connecting to B and C respectively (and vice versa).

Best Answer

You could use the .<angle> syntax to manually control the position of the arrows; another option would be to use a matrix of (math) nodes instead of the array environment, to get access to the nodes in the matrix. The following example illustrates both these ideas:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,matrix}

\begin{document}

\begin{tikzpicture}[>=stealth]
  \node[draw,rectangle,inner sep=0.5cm] (y) at (0,0) {$A$};
  \node[draw,rectangle,inner sep=0.3cm] (d) at (0,2) {$\begin{array}{cc}B&\\&C \end{array}$};
  \draw[->] (y.west) -| ++(-1,1) |- (d.160);
  \draw[->] (y.west) -| ++(-0.8,1) |- (d.190);
  \draw[->] (d.-10) -| ++(0.8,-1) |- (y.east);
  \draw[->] (d.20) -| ++(1,-1) |- (y.east);
\end{tikzpicture}

\vspace*{10pt}

\begin{tikzpicture}[>=stealth,remember picture]
\node[draw,rectangle,inner sep=0.5cm] (y) at (0,0) {$A$};
\node[draw] (d) at (0,2) {%
  \begin{tikzpicture}[remember picture]
    \matrix [matrix of math nodes] (mat)
  {
    B & \phantom{C}   \\
    \phantom{B} & C \\
  };
  \end{tikzpicture}
  };
\draw[->,shorten >= 6pt] (y.west) -| ++(-1,1) |- (mat-1-1);
\draw[->,shorten >= 6pt] (y.west) -| ++(-0.8,1) |- (mat-2-1);
\draw[->] ($(mat-2-2)+(14pt,0)$) -| ++(0.8,-1) |- (y.east);
\draw[->] ($(mat-1-2)+(14pt,0)$) -| ++(1,-1) |- (y.east);
\end{tikzpicture}

\end{document}

Image 1

EDIT: a variant on kgr's solution, but using two matrices of nodes:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,matrix,positioning}

\begin{document}

\begin{tikzpicture}[>=stealth]
\matrix[matrix of math nodes,nodes in empty cells,draw] (d) at (0,2)
{
  A & & & & \\
  & B & & & \\
  & & C  & & \\
  & & & D & \\
  & & & & E \\
};
\matrix[matrix of math nodes,nodes in empty cells,draw,minimum width=30pt,below=of d] (y) 
{
   \\ \\ \\ \\ \\
};
\node at (y) {$D$};

\foreach \i in {1,2,3,4,5}
  {
  \draw[->] let \p1 = (d.west), \p2 = (d-\i-\i.west), \p3 = (y.west), \p4 = (y-\i-1.west) in (\x3,\y4) -| 
    ++(-2.4+0.2*\i,1) |- (\x1,\y2);
  \draw[->] let \p1 = (d.east), \p2 = (d-\i-\i.east), \p3 = (y.east), \p4 = (y-\i-1.east) in (\x1,\y2) -|
    ++(1.6-0.2*\i,-1) |- (\x3,\y4);
  }
\end{tikzpicture}

\end{document}

Image 2