Block matrix equation with dimensioning

I need to write following matrix equation in LaTex. The dotted lines shown need to be present in the structure but need not have colors (could as well be black). I have used it here only to show the alignment needed. I also need to have the dimensions specified for various blocks as shown. What's the best way to do it?

-
Please have some mercy and don't do this. – percusse May 10 '14 at 14:50
You mean it will be too difficult to do this in LaTex? Is there any other way to do it - perhaps using Inkscape or TikZ? – Ganesh Diwan May 10 '14 at 14:57
No it is possible but with no offense it is very ugly. – percusse May 10 '14 at 14:58
In addition to @Gonzalo Medina's simplified answer, I would also make sure to make the square matrixes(A_1, A_2, B_1, B_2) approximately square, and exaggerate the non square property of 0's. – Hans-Peter E. Kristiansen May 10 '14 at 16:03
@Hans-PeterE.Kristiansen that's a good point. I modified my second example to incorporate your suggestion; however, the part to exaggerate the non square blocks was not easy to accomplish. – Gonzalo Medina May 10 '14 at 17:12

1 Answer

A possibility using TikZ and matrix of math nodes (from the matrix library) to build the natrices; the braces for the dimensions were built using a brace decoration from the decorations.pathreplacing library:

\documentclass{article}
\usepackage{tikz}
\usepackage{geometry}
\usetikzlibrary{matrix,positioning,decorations.pathreplacing}

\begin{document}

\begin{tikzpicture}[
style1/.style={
matrix of math nodes,
every node/.append style={text width=#1,align=center,minimum height=5ex},
nodes in empty cells,
left delimiter=[,
right delimiter=],
},
style2/.style={
matrix of math nodes,
every node/.append style={text width=#1,align=center,minimum height=5ex},
nodes in empty cells,
left delimiter=\lbrace,
right delimiter=\rbrace,
}
]
\matrix[style1=0.65cm] (1mat)
{
& & & & & \\
& & & & & \\
& & & & & \\
& & & & & \\
& & & & & \\
& & & & & \\
& & & & & \\
& & & & & \\
};
\draw[dashed]
(1mat-3-5.south west) -- (1mat-3-6.south east);
\draw[loosely dashed]
(1mat-5-1.south west) -- (1mat-5-6.south east);
\draw[dash dot]
(1mat-1-4.north east) -- (1mat-8-4.south east);
\draw[]
(1mat-5-2.south east) -- (1mat-8-2.south east);
\node[font=\huge]
at ([xshift=-10pt]1mat-3-3) {$A_{1}$};
\node[font=\Large]
at (1mat-2-5.east) {$0$};
\node[font=\Large]
at (1mat-5-5.north east) {$A_{2}$};
\node[font=\Large]
at (1mat-7-5.east) {$B_{2}$};
\node[font=\Large]
at (1mat-7-1.east) {$0$};
\node[font=\Large]
at (1mat-7-3.east) {$B_{1}$};
\draw[decoration={brace,mirror,raise=12pt},decorate]
(1mat-1-1.north west) --
node[left=15pt] {$n_{1}$}
(1mat-5-1.south west);
\draw[decoration={brace,mirror,raise=12pt},decorate]
(1mat-6-1.north west) --
node[left=15pt] {$n_{2}$}
(1mat-8-1.south west);
\draw[decoration={brace,mirror,raise=5pt},decorate]
(1mat-8-3.south west) --
node[below=7pt] {$n_{2}$}
(1mat-8-4.south east);
\draw[decoration={brace,mirror,raise=5pt},decorate]
(1mat-8-5.south west) --
node[below=7pt] {$n_{2}$}
(1mat-8-6.south east);
\draw[decoration={brace,raise=12pt},decorate]
(1mat-4-6.north east) --
node[right=15pt] {$n_{2}$}
(1mat-5-6.south east);
\draw[decoration={brace,raise=7pt},decorate]
(1mat-1-1.north west) --
node[above=8pt] {$n_{1}$}
(1mat-1-4.north east);

\matrix[style2=1.2cm,right=40pt of 1mat] (2mat)
{
Q_{1} \\
\vdots \\
Q_{n_{1} - n_{2}} \\
\vdots \\
Q_{n_{1}} \\
R_{1} \\
\vdots \\
R_{n_{2}} \\
};
\draw[dashed]
(2mat-3-1.south west) -- (2mat-3-1.south east);
\draw[loosely dashed]
(2mat-5-1.south west) -- (2mat-5-1.south east);

\node at ([xshift=17pt,yshift=-1.2pt]2mat.east) {$=$};

\matrix[style2=0.7cm,right=35pt of 2mat] (3mat)
{
0 \\
\vdots \\
\\
\vdots \\
0 \\
b_{1} \\
\vdots \\
b_{n_{2}} \\
};
\draw[loosely dashed]
(3mat-5-1.south west) -- (3mat-5-1.south east);
\end{tikzpicture}

\end{document}


Perhaps you should consider whether there's another way to display the information? Removing the braces with the dimensions and using a single dashed style improves the result:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning,decorations.pathreplacing}

\begin{document}

\begin{tikzpicture}[ style1/.style={ matrix of math nodes, every node/.append style={text width=#1,align=center,minimum height=5ex}, nodes in empty cells, left delimiter=[, right delimiter=], }, style2/.style={ matrix of math nodes, every node/.append style={text width=#1,align=center,minimum height=5ex}, nodes in empty cells, left delimiter=\lbrace, right delimiter=\rbrace, } ] \matrix[style1=0.85cm] (1mat) { & & & & & \\ & & & & & \\ & & & & & \\ & & & & & \\ & & & & & \\ & & & & & \\ & & & & & \\ & & & & & \\ & & & & & \\ }; \draw[dashed] (1mat-3-5.south west) -- (1mat-3-6.south east); \draw[dashed] (1mat-6-1.south west) -- (1mat-6-6.south east); \draw[dashed] (1mat-1-4.north east) -- (1mat-9-4.south east); \draw[dashed] (1mat-6-2.south east) -- (1mat-9-2.south east); \node[font=\huge] at (1mat-3-2.south east) {A_{1}}; \node[font=\Large] at (1mat-2-5.east) {0}; \node[font=\Large] at (1mat-5-5.east) {A_{2}}; \node[font=\Large] at (1mat-8-5.east) {B_{2}}; \node[font=\Large] at (1mat-8-1.east) {0}; \node[font=\Large] at (1mat-8-3.east) {B_{1}}; \matrix[style2=1.2cm,right=40pt of 1mat] (2mat) { Q_{1} \\ \vdots \\ Q_{n_{1} - n_{2}} \\ Q_{n_{1} - n_{2}+1} \\ \vdots \\ Q_{n_{1}} \\ R_{1} \\ \vdots \\ R_{n_{2}} \\ }; \draw[dashed] (2mat-3-1.south west) -- (2mat-3-1.south east); \draw[dashed] (2mat-6-1.south west) -- (2mat-6-1.south east); \node at ([xshift=17pt,yshift=-1.2pt]2mat.east) {=}; \matrix[style2=0.7cm,right=35pt of 2mat] (3mat) { 0 \\ \vdots \\ \\ \\ \vdots \\ 0 \\ b_{1} \\ \vdots \\ b_{n_{2}} \\ }; \draw[dashed] (3mat-6-1.south west) -- (3mat-6-1.south east); \end{tikzpicture}

\end{document}


-
I do have equations that precisely convey what is shown in the matrix equation above. The matrix structure may not look nice but it is just another way of displaying the information. Probably I should remove the curly braces for the dimensions. I have never used TikZ, can you explain how you did it? – Ganesh Diwan May 10 '14 at 15:30
@GaneshDiwan I see. Yes, perhaps removing the braces with the dimensions would be better. I added some short remarks to the solution. Let me know of you need to know anything else about the solution. – Gonzalo Medina May 10 '14 at 15:37
Is there a way to put this in an equation environment or alternatively generate an .eps file to put in a figure environment? – Ganesh Diwan May 10 '14 at 15:42
@GaneshDiwan For an equation environment, you can simply use $$\begin{tikzpicture}...\end{tikzpicture}$$; similarly, you can also put the figure inside a figure environment: \begin{figure}\centering\begin{tikzpicture}...\end{tikzpicture}\end{figure}. You can even use the standalone document class to generate a single file for the image that can be included using the standard \includegraphics command (I am not sure, however, whether standalone can generate a .eps file). – Gonzalo Medina May 10 '14 at 15:45
No worries- equation environment works, thanks very much! – Ganesh Diwan May 10 '14 at 15:49