4

I am creating a block-diagonal matrix and want to use boxes along the diagonal but the boxes don't touch each other, so it looks funny.

What's a workaround for this?

Here's my code:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
  \begin{bmatrix}       
    \fbox{$B_1$} & 0 & 0  \\
     0 & \fbox{$B_2$} & 0 \\
     0 & 0 & \fbox{$B_3$} \\
   \end{bmatrix}
\]
\end{document}

something like the matrix in the middle ...of this answer (but without the red and color filling)

Squares in Matrix

Thanks,

1
  • Hi @cfr, nope, I will try that now. But the other issue is: the boxes don't touch each other, which I would like for a block-diagonal matrix....
    – user58865
    Sep 21 '16 at 3:23
8

The space between the columns is controlled by the dimen register \arraycolsep:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
  \setlength{\arraycolsep}{0pt}
  \begin{bmatrix}
    \fbox{$B_1$} & 0 & 0  \\
    0 & \fbox{$B_2$} & 0 \\
    0 & 0 & \fbox{$B_3$} \\
  \end{bmatrix}
\]
\end{document}

Result

Without touching the delimiters:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
  \setlength{\arraycolsep}{0pt}
  \begin{bmatrix}
    \,\fbox{$B_1$} & 0 & 0\,  \\
    \,0 & \fbox{$B_2$} & 0\, \\
    \,0 & 0 & \fbox{$B_3$}\, \\
  \end{bmatrix}
\]
\end{document}

Result

The size of the delimiters are at least \delimiterfactor per mille of the formula height or the height of the formula minus \delimitershortfall. Full size is achieved by either

\delimiterfactor=1000\relax

or

\setlength{\delimitershortfall}{0pt}

Example:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
  \setlength{\arraycolsep}{0pt}
  % \delimiterfactor=1000 %
  \setlength{\delimitershortfall}{0pt}
  \begin{bmatrix}
    \,\fbox{$B_1$} & 0 & 0\,  \\
    \,0 & \fbox{$B_2$} & 0\, \\
    \,0 & 0 & \fbox{$B_3$}\, \\
  \end{bmatrix}
\]
\end{document}

Result

Colored boxes would also be possible as in Zarko's answer:

\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor}
\begin{document}
\[
  \setlength{\arraycolsep}{0pt}
  \setlength{\delimitershortfall}{0pt}
  \newcommand*{\myfbox}[1]{%
    \fcolorbox{red}{red!20!white}{$#1$}%
  }
  \begin{bmatrix}
    \,\myfbox{B_1} & 0 & 0\,  \\
    \,0 & \myfbox{B_2} & 0\, \\
    \,0 & 0 & \myfbox{B_3}\, \\
  \end{bmatrix}
\]
\end{document}

Result with colored boxes

2
  • awesome, @Heiko. How could I get the matrix brackets to be big enough to accommodate the boxes?
    – user58865
    Sep 21 '16 at 4:45
  • @user58865 See updated answer. Sep 21 '16 at 5:05
5

Like this?

enter image description here

Well, this matrix is drawn ...

\documentclass[border=3mm,tikz,preview]{standalone}
\usetikzlibrary{matrix}

\begin{document}
\[
    \begin{tikzpicture}
\matrix (m1)    [matrix of nodes,
                 left delimiter={[}, right delimiter={]},
                 row sep=-0.5pt,column sep=-0.5pt,
                 every node/.style={inner sep=5pt}
                 ]
{
|[draw]| $B_1$ &   0           &   0             \\
 0            & |[draw]| $B_2$ &   0             \\
 0            &   0           & |[draw]| $B_3$   \\
 };
    \end{tikzpicture}
\]
\end{document}

by tikz with TikZ library matrix. Distance between cells of matrix as well to its brackets are set by inner sep=5pt. Cells with "frame" have option |[draw]|.

Addendum: Since the matrix is now actually image, it is simple to present on more fancy ways. For example as:

enter image description here

MWE for above version of matrix is:

\documentclass[border=3mm,tikz,preview]{standalone}
\usetikzlibrary{matrix}

\begin{document}
\[
    \begin{tikzpicture}
\matrix (m1)    [matrix of nodes,
                 left delimiter={[}, right delimiter={]},
                 row sep=-0.5pt,column sep=-0.5pt,
                 every node/.style={inner sep=5pt},
                 F/.style = {draw=red, line width=0.5pt, fill=red!20}
                 ]
{
|[F]| $B_1$ &   0           &   0           \\
 0          & |[F]| $B_2$   &   0           \\
 0          &   0           & |[F]| $B_3$   \\
 };
    \end{tikzpicture}
\]
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.