# Drawing boxes in matrices

In my document I have defined

\newcommand\bigzero{\makebox(0,0){\text{\huge0}}}


and using this the following matrix:

$$\begin{pmatrix} 0&1\\ 1&0&&&&\bigzero\\ &&0&1\\ &&1&0\\ &&&&\ddots\\ &\bigzero&&&&0&1\\ &&&&&1&0 \end{pmatrix}$$


How can I draw boxes around the three blocks which build the matrix?

A solution with basic tools:

\documentclass{article}

\usepackage{amsmath}
\usepackage{hhline}
\newcommand\bigzero{\makebox(0,0){\text{\huge0}}}
\newcommand{\fblock}[1]{\begin{array}{|@{\:}cc@{\:}|}
\hhline{|--|}#1 \\
\hhline{|--|}
\end{array}}

\begin{document}

$$\begin{pmatrix} \,\fblock{0&1\\ 1&0 }&&\\ &\mkern-9mu\fblock{0&1\\ 1&0}&\multicolumn{2}{c}{\raisebox{3ex}{\bigzero}} \\ &&\makebox[0pt]{ \ddots }\\[-0.5ex] \multicolumn{2}{c}{\raisebox{4ex}{\bigzero}} &&\fblock{0&1\\ 1&0}\, \end{pmatrix}$$

\end{document}


This is only a fun answer. I am trying out LoopSpace's great update of the tikzmark package, which has now a command in that I was always looking for: \tikzmarknode. And it just works. (Yes, using tikz here may be an overkill, as I said, this is just for fun and to learn something new.) EDIT: Moved the brackets away from the boxes, big thanks to @manooooh!

\documentclass{article}
\usepackage{amsmath,tikz}
\usetikzlibrary{tikzmark,fit}
\newcommand\bigzero{\makebox(0,0){\text{\huge0}}}
\begin{document}
$$\left(~\begin{matrix} \tikzmarknode{1A}{0}&1\\ 1&\tikzmarknode{1B}{0}&&&&\bigzero\\ &&\tikzmarknode{2A}{0}&1\\ &&1&\tikzmarknode{2B}{0}\\ &&&&\ddots\\ &\bigzero&&&&\tikzmarknode{3A}{0}&1\\ &&&&&1&\tikzmarknode{3B}{0} \end{matrix}~\right) \begin{tikzpicture}[overlay, remember picture] \foreach \X in {1,2,3} {\node[inner sep=1pt,draw,fit=(\X A)(\X B)]{};} \end{tikzpicture}$$
\end{document}


• +1, but I think the boxes are too stick to the braces of the matrix. Maybe using someting like \left(\;\begin{matrix}...\end{matrix}\;\right) would be ok? – manooooh Sep 9 '18 at 20:54
• @manooooh You are absolutely right. Yet this is really just for fun and to pay tribute to LoopSpace's updated package. In this case, you can easily produce a very simple \fbox only solution by placing the 2x2 matrices in \fboxes. I was actually not expecting to get any votes for that, just wanted to draw the attention to the updated library such that LoopSpace and I have company when testing it. ;-) – user121799 Sep 9 '18 at 20:58
• @manooooh No, I am now on a promotion tour. Undoubtedly someone else will come and say this is a sledge hammer and it is possible to achieve the same with \fboxes (which is true in this case, but not when the boxes should become a tiny bit more fancy). ;-) – user121799 Sep 9 '18 at 21:08
• Very nice, but why is the bottom zero of the middle box misaligned? – Max Sep 10 '18 at 5:44
• The misalignment is actually from the previous line where you have ~~ after the 1 which adds a bit of space after the 1, thus seeming to push the 0 further out due to the centring of the matrix entries. – Loop Space Sep 11 '18 at 18:39