Do you want something like that?
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:
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.
I don't really get the question so I hope this is what you wanted. If you include a full document (such that we copy paste and see the problem on our systems) things are much more easier.
Here, you can change the default setting within a scope but your block
style had a node distance
which was resetting every time it is issued. I've made it 2mm such that we can see the difference easier.
\documentclass[tikz]{standalone}
\usetikzlibrary{arrows,shapes.geometric,positioning}
\begin{document}
\begin{tikzpicture}[decision/.style={diamond, draw, text width=4.5em, text badly centered, node distance=3.5cm, inner sep=0pt},
block/.style ={rectangle, draw, text width=6em, text centered, rounded corners, minimum height=4em, minimum height=2em},
cloud/.style ={draw, ellipse, minimum height=2em},
line/.style ={draw,-latex'},
node distance = 1cm,
auto]
\node [block] (1st) {1st};
\node [block, right= of 1st] (2nd1) {2nd1};
\begin{scope}[node distance=2mm and 10mm]%Here we change it for everything inside this scope
\node [block, above= of 2nd1] (2nd2) {2nd2};
\node [block, below= of 2nd1] (2nd3) {2nd3};
\node [block, right= of 2nd1] (3rd1) {3rd1};
\node [block, above= of 3rd1] (3rd2) {3rd2};
\node [block, above= of 3rd2] (3rd3) {3rd3};
\end{scope}
\node [block, below= of 3rd1] (3rd4) {3rd4};
\node [block, below= of 3rd4] (3rd5) {3rd5};
\path [line] (1st) -- (2nd1);
\path [line] (2nd1) -- (2nd2);
\path [line] (2nd1) -- (2nd3);
\path [line] (2nd2) -- (3rd3);
\path [line] (2nd1) -- (3rd1);
\path [line] (1st) -- (2nd1);
\end{tikzpicture}
\end{document}
Best Answer
You should use
instead of
This will make sure the text is vertically centered.