# Squares in Matrix

I would like to write a matrix equation, where the matrix contains equally sized coloured blocks on the diagonal. Something like:

-
Hi Math Girl and welcome to TeX.SX! In its current form, your question might not receive many answers. Please take a look at the How to Ask page and try to improve your question according to the guidance there. This may require you to show some effort on your part in terms of attempting a solution. Please post a minimal working example (MWE) showing that you've tried to produce the result; then people will be happy to help you with any specific problems. – Paul Gessler Feb 19 '14 at 16:45
What's the purpose of this notation please? – Thruston Feb 19 '14 at 17:21
Related (although not a dupe): tex.stackexchange.com/questions/150334/… – alexwlchan Feb 19 '14 at 22:50
related also tex.stackexchange.com/questions/125123/… "Are there specialized libraries or packages to draw blocked/partitioned/splitting matrices in Tikz?" – Nasser Feb 20 '14 at 0:35

The following example calculates the contents of the matrix for the diagonal boxes automatically, given the number of rows. The environments for tables are a little tricky, because each cell is put in a group. Therefore the example first collects the contents of the matrix in a global macro \DiagonalMatrixLines and uses this macro inside the matrix environment. Macro \foreach of package pgffor is used for the loop. (There are many other possibilities.)

The square is determined by the height and depth of the row. The matrix environments are based on the environment array which inserts a special strut \@arstrut to set a minimum height and depth of the row. The additioal white space added by environment array is removed by setting \arraycolsep to zero.

\vdots occupies a little more than one line, but it seems that the matrix with the diagonal boxes should not contain a row with \vdots. Therefore is a matrix with four blocks smaller in height and with five blocks larger. The next example uses the inner part of the left matrix to get the height of the matrix and uses this height to resize the matrix with the diagonal blocks. The vertical middle of a matrix lies on the math axis.Therefore we must move the middle of the matrix to the baseline before resizing.

\documentclass{article}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{pgf,pgffor,graphicx}

\makeatletter
\newcommand*{\DiagonalMatrixBlock}{%
\begingroup
\setlength{\fboxsep}{0pt}%
\colorbox{red}{%
\@arstrut
\kern\dp\@arstrutbox
\kern\ht\@arstrutbox
}%
\endgroup
}

\newcommand*{\@DiagonalMatrix}[1]{%
\begingroup
\setlength{\arraycolsep}{0pt}%
\global\let\DiagonalMatrixLines\@empty
\foreach[count=\xi] \x in {1,...,#1} {%
\ifnum\xi>1 %
\fi
\foreach \x in {1,...,\xi} {
}%
}%
\endgroup
}

\newcommand*{\DiagonalMatrix}[1]{%
\begingroup
\setlength{\arraycolsep}{0pt}%
\@DiagonalMatrix{#1}%
\begin{pmatrix}\DiagonalMatrixLines\end{pmatrix}%
\endgroup
}
\newcommand*{\ScaledDiagonalMatrix}[2]{%
\begingroup
\sbox0{$\begin{matrix}#2\end{matrix}$}%
\sbox2{$\vcenter{}$}
\setlength{\arraycolsep}{0pt}%
\@DiagonalMatrix{#1}%
\left(%
\raisebox{\ht2}{%
\resizebox{!}{\dimexpr\ht0-\ht2\relax}{%
\raisebox{-\ht2}{%
$\begin{matrix}\DiagonalMatrixLines\end{matrix}$%
}%
}%
}%
\right)%
\endgroup
}
\makeatother

\begin{document}
$\begin{pmatrix} b_1 \\ b_2 \\ \vdots \\ b_n \end{pmatrix} = \DiagonalMatrix{5} \begin{pmatrix} a_1 \\ a_2 \\ \vdots \\ a_n \end{pmatrix}$
$\newcommand*{\MatrixB}{b_1 \\ b_2 \\ \vdots \\ b_n} \begin{pmatrix}\MatrixB\end{pmatrix} = \ScaledDiagonalMatrix{8}{\MatrixB} \begin{pmatrix} a_1 \\ a_2 \\ \vdots \\ a_n \end{pmatrix}$
\end{document}


-

There's probably lots of ways of doing this, but here is a simple approach using colorbox from color package.

\documentclass{article}
\usepackage{xcolor}
\begin{document}
$\left(\begin{array}{c}b_1\\b_2\\\vdots\\b_n\end{array}\right) = \left(\begin{array}{cccc} \colorbox{red}{\,}\\ &\colorbox{red}{\,}\\ &&\ddots\\ &&&\colorbox{red}{\,} \end{array}\right) \left(\begin{array}{c}a_1\\a_2\\\vdots\\a_n\end{array}\right)$
\end{document}


-
You may want to mention somewhere that the currently smallish red boxes will expand in size once they're populated by "real stuff". – Mico Feb 19 '14 at 18:31
@Mico, indeed but without some idea of what the OP was trying to do it's hard to know what extra detail should be given. – Thruston Feb 19 '14 at 23:12

Another approach using pmatrix from amsmath for the matrices and \cellcolor (from the table option to xcolor) to colorize the cells:

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{amsmath}

\begin{document}
$\begin{pmatrix} b_1\\b_2\\\vdots\\b_n \end{pmatrix} = \begin{pmatrix} & \cellcolor{red} \\ &&\cellcolor{red}\\ &&&\ddots\\ &&&&\cellcolor{red} & \end{pmatrix} \begin{pmatrix} a_1\\a_2\\\vdots\\a_n \end{pmatrix}$

\end{document}


-

Another solution with the \cellcolor command and a macro that ensures to have coloured squares. The colour is entered as an optional parameter (defaults to red, here)

       \documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{array, mathtools}
\usepackage[table]{xcolor}

\usepackage[table, svgnames]{xcolor}

\makeatletter
\newcommand{\mycbox}[1][red]{\cellcolor{#1}\rule{\dimexpr\ht\@arstrutbox+\dp\@arstrutbox-2\arraycolsep\relax}{0pt}}%
\makeatother

\begin{document}

$$\begin{pmatrix} b_1 \\ b_2 \\ \vdots \\ b_n \\ \end{pmatrix} = \left(\enspace\begin{matrix} \mycbox[SlateBlue] & & & \\ & \mycbox & & \\ & & \mycbox[VioletRed] & \\ & & & \mycbox[Purple] \end{matrix}\enspace\right) \begin{pmatrix} a_1 \\ a_2 \\ \vdots \\ a_n \\ \end{pmatrix}$$
\end{document}


-
The exact width for the \rule is \dimexpr\ht\@arstrutbox+\dp\@arstrutbox-2\arraycolsep\relax. – Heiko Oberdiek Feb 19 '14 at 22:51
@ Heiko Oberdiek: thanks! With this help, I could easily turn the colours of the boxes an optional parameter. – Bernard Feb 20 '14 at 0:19