[Tex/LaTex] Matrix color blocks

matrices

I am looking to produce a matrix multiplication where one of the matrices consists of coloured blocks. Much like:
enter image description here

Is there any way to produce something like this in Latex?

Best Answer

I did this with stacks. Unfortunately, there is some tailoring that one must do, depending on the arrangement of boxes, so each particular image must be thought through. The \colblock[color]{rows}{content} is the basic unit. \belowbaseline[length]{} is used in \colblock to align boxes at their top. That and \belowbaseline[length]{} can be used in the code to shift/align boxes. Passing either of them a multiple of \baselineskip would allow some staggering in the vertical box placement.

If the \colblocks are allowed to be rectangular (I assumed square), let me know so that I may correct the answer.

\documentclass{article}
\usepackage{stackengine}
\usepackage{xcolor}
\usepackage{calc}
\newlength\mytemp
\newlength\myoffset
\myoffset=.5\ht\strutbox
\newcommand\colblock[3][blue!20]{%
  \setlength\mytemp{#2\baselineskip}%
  \setlength\mytemp{.5\mytemp-\myoffset}%
  \belowbaseline[0pt]{%
  \fboxsep=-\fboxrule\fbox{\colorbox{#1}{\rule[-\mytemp]{0ex}{#2\baselineskip}%
  \makebox[#2\baselineskip]{$#3$}}}%
}}
\begin{document}
\[
\parenVectorstack{b_1 b_2 . . . . . b_n} =
\left(
\raisebox{4\baselineskip+.5\myoffset}{%
\def\stackalignment{l}\stackunder[0pt]{\colblock{2}{-2}\colblock{2}{-2}}%
  {\colblock{6}{-1}\colblock[red!15]{4}{1}}%
}%
\right)
\parenVectorstack{a_1 a_2 . . . . . a_n}
\]
\end{document}

enter image description here

Related Question