# Is there some way put boxes around matrix elements without too much effort?

I know this question has been asked before but the examples that they give are too complicated to replicate, for example.

Is there some way to make boxes like these (dashes are not necessary but preferable):

for entries of a matrix?

Thanks!

Here is TikZ proposal using fit. I've wrapped the code in a command I've called mymatrixbox. It takes 4 inputs which define the boundaries of the box using the cell coordinates (it also takes 1 optional input which allows you some control over the default style).

The way it works is like this, say you want to draw a box from (1,1) to (3,1), then use \mymatrixbox{1}{1}{3}{1}. To draw a box around a single cell then just repeat the cell coordinates - e.g. \matrixbox{2}{3}{2}{3}. If I wanted to draw a box around the entire matrix the code for that would be \mymatrixbox{1}{1}{3}{3}.

\documentclass[margin=0.5cm]{standalone}

\usepackage{tikz}
\usetikzlibrary{matrix,fit}
\pgfkeys{tikz/mymatrix/.style={matrix of math nodes,inner sep=0pt,row sep=0em,column sep=0em,nodes={inner sep=6pt}}}

\newcommand*\mymatrixbox[5][]{\node [fit= (m-#2-#3) (m-#4-#5)] [draw=blue,thick,dashed,rounded corners,inner sep=-2pt,#1] {};}

\begin{document}

\begin{tikzpicture}[baseline=0cm]
\matrix [mymatrix] (m)
{
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1 \\
};

\mymatrixbox{1}{1}{3}{1}
\mymatrixbox{3}{2}{3}{3}
\mymatrixbox[red]{2}{3}{2}{3}

\end{tikzpicture}

\end{document}


A simple solution with pstricks and a plain matrix environment from amsmath:

\documentclass[11pt, svgnames]{article}
\usepackage{amssymb, amsmath, array}
\usepackage{pst-node}
\usepackage{auto-pst-pdf}

\begin{document}

\begin{equation*}
\begin{postscript}
\psset{nodesep=5pt, boxsize=0.7em, linearc=.1, framearc=0.2, linestyle=dashed, dash= 4pt 2pt, linecolor=LightBlue}
\setlength{\arraycolsep}{6pt}
\setlength{\extrarowheight}{3pt}
\begin{matrix}
\Rnode{A}{1}& 0 & 0 \\
0 &1 & \rnode{E}{0} \\
\Rnode{B}{0}& \Rnode{C}{0} & \Rnode{D}{1}
\end{matrix}
\ncbox{A}{B}
\ncbox{C}{D}
\fnode[framesize = 0.56 0.50](E){F}
\end{postscript}
\end{equation*}

\end{document}


• how to set the same width to all columns? – pzorba75 Aug 12 '18 at 14:50
• @pzorba75: I didn't notice, but you're right: there's a width problem and a priori I don't see why (normally postscript code is dimensionless, so it doesn't modify the column widths). If I find a solution, I'll post a new code and warn you. – Bernard Aug 12 '18 at 14:59
• @pzorba: Problem solved: it came from incorporating postcript code directly in the matrix (for the frame in the second row) instead of writing code outside. I think now it's OK. Thanks for pointing it! – Bernard Aug 12 '18 at 15:21

Just for completeness: a slightly modified version of Milo's nice answer which works with arbitrary matrices, i.e. has that flexibility of Bernhards nice answer without having to go through the usual "how do I compile documents containing pstricks and can I be sure that the outcome does not depend on the compiler?" questions. That is, you do not have to write the matrix as a tikzpicture. The price you have to pay is that you have to "mark" the elements you want to box with \tikznode beforehand.

\documentclass{article}
\usepackage{amsmath,bbm}
\usepackage{tikz}
\usetikzlibrary{fit}
\newcommand{\tikznode}[2]{\relax
\ifmmode%
\tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {$#2$};
\else
\tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {#2};%
\fi}
\tikzset{box around/.style={
draw,rounded corners,
inner sep=2pt,outer sep=0pt,
node contents={},fit=#1
},
}
\begin{document}
$$\mathbbm{1}_3=\begin{pmatrix} \tikznode{m-1-1}{1} & 0 & 0\\ \tikznode{m-2-1}{0} & 1 & \tikznode{m-2-3}{0}\\ \tikznode{m-3-1}{0} & \tikznode{m-3-2}{0} & \tikznode{m-3-3}{1} \end{pmatrix}\;.$$
\tikz[overlay,remember picture]{%
\node[blue,dash pattern=on 2pt off 1.25pt, thick,box around=(m-1-1)(m-3-1)];
\node[red,dash pattern=on 2pt off 1.25pt, thick,box around=(m-2-3)];
\node[purple,dash pattern=on 2pt off 1.25pt, thick,box around=(m-3-2)(m-3-3)];}
\end{document}


Of course, you could also wrap this into a macro, i.e. the following yields the same output.

\documentclass{article}
\usepackage{amsmath,bbm}
\usepackage{tikz}
\usetikzlibrary{fit}
\newcommand{\tikznode}[2]{\relax
\ifmmode%
\tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {$#2$};
\else
\tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {#2};%
\fi}
\tikzset{box around/.style={
draw,rounded corners,
inner sep=2pt,outer sep=0pt,
node contents={},fit=#1
},
}
\newcommand{\BoxAround}[2][]{
\tikz[overlay,remember picture]{%
\node[blue,dash pattern=on 2pt off 1.25pt,thick,#1,box around=#2];}}
\begin{document}
$$\mathbbm{1}_3=\begin{pmatrix} \tikznode{m-1-1}{1} & 0 & 0\\ \tikznode{m-2-1}{0} & 1 & \tikznode{m-2-3}{0}\\ \tikznode{m-3-1}{0} & \tikznode{m-3-2}{0} & \tikznode{m-3-3}{1} \end{pmatrix}\;.$$
\BoxAround{(m-1-1)(m-3-1)}
\BoxAround[red]{(m-2-3)}
\BoxAround[purple]{(m-3-2)(m-3-3)}
\end{document}


today i discover interesting package nicematrix. with it you can write:

\documentclass[margin=3mm, preview]{standalone}
\usepackage{nicematrix}
\NiceMatrixOptions{transparent}
\usetikzlibrary{fit}

\begin{document}
$\mathbf{I}_3 = \begin{bNiceMatrix}[columns-width = 1em] [code-after= {\begin{tikzpicture}[FIT/.style = {% draw, semithick, rounded corners, densely dashed, inner sep=2pt, fit=#1, node contents={}} ] \node[FIT=(1-1) (3-1),blue]; \node[FIT=(3-2) (3-3),blue]; \node[FIT=(1-3) (2-3),red]; \end{tikzpicture} } ] 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \\ \end{bNiceMatrix}$
\end{document}


for correct result you need to compile document at least twice.