10

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

Does anyone have any idea how to go about this?

4
  • 2
    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. Commented Feb 19, 2014 at 16:45
  • 1
    What's the purpose of this notation please? Commented Feb 19, 2014 at 17:21
  • Related (although not a dupe): tex.stackexchange.com/questions/150334/… Commented Feb 19, 2014 at 22:50
  • related also tex.stackexchange.com/questions/125123/… "Are there specialized libraries or packages to draw blocked/partitioned/splitting matrices in Tikz?" Commented Feb 20, 2014 at 0:35

5 Answers 5

5

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 % \g@addto@macro\DiagonalMatrixLines{\\}% \fi \foreach \x in {1,...,\xi} { \g@addto@macro\DiagonalMatrixLines{&}% }% \g@addto@macro\DiagonalMatrixLines{\DiagonalMatrixBlock}% }% \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} 

Result

9

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} 

enter image description here

2
  • You may want to mention somewhere that the currently smallish red boxes will expand in size once they're populated by "real stuff". Commented Feb 19, 2014 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. Commented Feb 19, 2014 at 23:12
7

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} 

enter image description here

4

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{equation} \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{equation} \end{document} 

enter image description here

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

With {pNiceMatrix} of nicematrix.

\documentclass{article} \usepackage{nicematrix} \begin{document} $\begin{pNiceMatrix}[nullify-dots] b_1 \\ b_2 \\ \Vdots \\ \\ b_n \end{pNiceMatrix} = \begin{pNiceMatrix}[columns-width=1.8mm,margin] \CodeBefore \cellcolor{red}{1-1,2-2,3-3,4-4,5-5} \Body &&&& \\ \\ \\ \\ \\ \end{pNiceMatrix} \begin{pNiceMatrix} a_1 \\ a_2 \\ \Vdots \\ \\ a_n \end{pNiceMatrix}$ \end{document} 

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.