# Can I put \fbox along the diagonal so that they touch each other?

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,

• 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.... Sep 21 '16 at 3:23

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}


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}


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}


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}


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

Like this?

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:

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}