[Tex/LaTex] vertical lines in matrix using tikz package

matricestikz-pgf

my problem is interconnected to my two previous posts (here and here) and i've tried to incorporate answers into solution but without success.

My task is this matrix multiplication with added description:
My task

and my so far code follows:

\documentclass{article}
\usepackage{tikz,amsmath} 
\begin{document}
\begin{tikzpicture}
\node[minimum height=0cm,minimum width=2cm] (m) {};
\path[shade,draw] (0,2) -- (0,5) -- (3,5) -- (3,0);
\draw (0,0) --(0,5);
\draw (3,0) --(0,2);
\draw (3,0) --(3,5);
\node (r11) at ([xshift=0.6cm,yshift=4cm]m.south) {$S_{11}$};
\node (r11) at ([xshift=2.5cm,yshift=4cm]m.south) {$S_{12}$};
\node (r11) at ([xshift=2.5cm,yshift=2.15cm]m.south) {$S_{22}$};
\node (r11) at ([xshift=0.6cm,yshift=2.15cm]m.south) {$S_{21}$};
\node (r11) at ([xshift=0.6cm,yshift=2.6cm]m.south) {$--------------$};
\node (r11) at ([xshift=0.6cm,yshift=1.5cm]m.south) {$--------------$};
\end{tikzpicture}
\[
\begin{bmatrix}
\begin{equation}
       wn_{1}           \\[0.3em]
       wn_{2}    

 \end{bmatrix}
 \]
 \end{document}    

with following output:

My Output

I am not able to add vertical line and nodes and incorporate the matrix into document (vector that follows behind the matrix is not centered). Problem with nodes and incorporation is the same as in this question.

Best Answer

Do you want something like that?

enter image description here

How I did it?

1- The matrix.

\matrix[draw,inner sep=0pt] (S) 
       [matrix of math nodes,
       nodes={outer sep=0pt,minimum width=15mm,minimum height=20mm}]
      {S_{11} & S_{12}\\ S_{21} & S_{22}\\ 0 & S_{32}\\};

Tikz command matrix let's you define some nodes and compose them like in a tabular: columns defined with & and new rows with \\. Options draw and inner sep=0pt draw an adjusted matrix border. If you comment inner sep you will see the diference.

matrix of math nodes (you need TikZ library matrix) allow you easily declare your nodes. You don't need to use \node[options] (name) {$S_{11}$}; for every matrix node but just it's contains. This library also sets a name for every node: matrix_name-column-row. As the matrix is named S, node with $S_21$ will be S-2-1.

With nodes={nodes options} you declare how will be all matrix nodes. In this case, their size is fixed and also outer sep=0pt which makes coincide matrix border with node borders.

2 - Shaded background

Once a node is declared you have some coordinates related to it (north, north west, east, ...). So, let's use them to draw the background. We need backgrounds library to draw anything into the background layer.

\begin{scope}[on background layer]
\draw[shade] (S-2-1.west) |- (S-1-2.north east) |- (S-3-2.south) -- cycle;
\end{scope}

Syntax (S-2-1.west) |- (S-1-2.north east) means a vertical line which starts at (S-2-1.west) and finishes at its intersection with an horizontal line through (S-1-2.north east). Then starts a horizontal line which finishes at (S-1-2.north east).

3 - Horizontal and vertical lines

\draw[dashed,gray] (S-2-2.north east) --++(180:3cm);
\draw[dashed,gray] (S-2-2.south east) --++(180:3cm);
\draw[dashed,gray] (S-2-1.south east) --++(90:4cm);

We fix where the line starts (S-2-2.north east) and where it will end ++(180:3cm). This last syntax means a point which is 3cm far from the previous coordinate and following direction 180 degrees.

4 - bracketed matrix

\node[label=above:i] (wm) at ([xshift=1cm]S-2-2.east) 
{$\begin{bmatrix}wn_1\\[.3em]wn_2\end{bmatrix}$};

It's just a node placed 1cm to the right of coordinate S-2-2.east.

Before to look at the complete code, and because you want to illustrate a matrix multiplication, I would suggest to look at Example: Matrix multiplication from the excellent TeXample.net.

The complete code is:

\documentclass{article}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{matrix,backgrounds}

\begin{document}
\begin{tikzpicture}

\matrix[draw,inner sep=0pt] (S) [matrix of math nodes,nodes={outer sep=0pt,minimum width=15mm,minimum height=20mm}]
{S_{11} & S_{12}\\S_{21} & S_{22}\\
0 & S_{32}\\
};

\draw[dashed,gray] (S-2-2.north east) --++(180:4cm);
\draw[dashed,gray] (S-2-2.south east) --++(180:4cm);
\draw[dashed,gray] (S-2-1.south east) --++(90:5cm);

\begin{scope}[on background layer]
\draw[shade] (S-2-1.west) |- (S-1-2.north east) |- (S-3-2.south) -- cycle;
\end{scope}

\node[label=above:i] (wm) at ([xshift=1cm]S-2-2.east) {$\begin{bmatrix}wn_1\\[.3em]wn_2\end{bmatrix}$};

\node[rotate=90,above] (mobs) at (S-3-1.west) {mobs};
\node[rotate=90,above] (m) at (S-2-1.west) {m};
\end{tikzpicture}

 \end{document}

EDIT: How to find intersection between horizontal dashed line and diagonal line and draw a vertical from this point?

1- Add library intersections to \usetikzlibrary in previous code.

2- Add name path = name_you_like_it to horizontal

\draw[dashed,gray,name path=line1] (S-2-2.south east) --++(180:4cm);

and diagonal lines

\path [name path=line2] (S-2-1.west)--(S-3-2.south);

3- Draw a vertical lined from the intersection point:

\draw [red, name intersections={of=line1 and line2}] (intersection-1) --++(90:5cm);

The result is:

enter image description here

Now I imagin you will have to change all matrix dimensions because red line is going over name of nodes. If you prefer to move the diagonal line, I suggest to read 'Tutorial: Euclid’s Amber Version of the Elements' in TikZ's manual.