# Rectangle box around elements of matrix

Here, I wish to draw a rectangle around the principal diagonal elements (red colored) of the below matrix.

MWE:

\documentclass{article}

\usepackage{amsmath,xcolor}

\begin{document}

Here, I wish to draw a rectangle around the principal diagonal elements
(red colored) of the below matrix.
$A = \begin{bmatrix} \textcolor{red}{1} & 2 & 3 & 4\\ 1 & \textcolor{red}{2} & 3 & 4\\ 1 & 2 & \textcolor{red}{3} & 4\\ 1 & 2 & 3 & \textcolor{red}{4} \end{bmatrix}$

\end{document}

• – Werner
Jan 1, 2020 at 6:29

Like this?

With use of nicematrix  package you obtain above result after two compilation ot he following MWE:

\documentclass{article}
\usepackage{nicematrix}

\begin{document}
$\begin{bNiceArray}{>{\strut}llll}[margin=3mm] \Block[draw=red]{}{1} & 2 & 3 & 4 \\ 1 & \Block[draw=red]{}{2} & 3 & 4 \\ 1 & 2 & \Block[draw=red]{}{3} & 4 \\ 1 & 2 & 3 & \Block[draw=red]{}{4} \\ \end{bNiceArray}$
\end{document}


or like this:

\documentclass{article}
\usepackage{nicematrix,tikz}
\usetikzlibrary{fit}

\begin{document}
$\begin{bNiceArray}{>{\strut}cccc}[margin,extra-margin = 1pt] 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ \CodeAfter \begin{tikzpicture} \node [draw=red, rounded corners=2pt, inner ysep = 0pt, rotate fit=-38, fit = (1-1) (4-4) ] {} ; \end{tikzpicture} \end{bNiceArray}$
\end{document}

• Nice work by you, actually i want to draw a single rectangle which covers all four elements please don't mind. Jan 1, 2020 at 8:02
• @Maths4Sandy, see if added example is what you looking for. Jan 1, 2020 at 9:12
• Sir, this is exactly what i was looking. Thanks a lot for giving response to my question for so many times, i hope that in future i will get benefit of your knowledge. Jan 1, 2020 at 9:38

Two solutions with pstricks: one with a simple frame around the diagonal elements and another which adds a coloured background. The pst-node module has a dedicated command for that – \ncbox:

    \documentclass[svgnames]{article}

\usepackage{amsmath, xcolor}
\usepackage{pst-node}
\usepackage{auto-pst-pdf}

\begin{document}

$A = \begin{bmatrix} \rnode{B}{\color{red}{1}} & 2 & 3 & 4\\ 1 & \color{red}{2} & 3 & 4\\ 1 & 2 & \textcolor{red}{3} & 4\\ 1 & 2 & 3 & \rnode{E}{\color{red}{4}} \end{bmatrix} \ncbox[linecolor =VioletRed, boxsize=5pt, linearc=0.05]{B}{E}$
$A = \begin{bmatrix} \rnode{B}{\color{red}{1}} & 2 & 3 & 4\\ 1 & \color{red}{2} & 3 & 4\\ 1 & 2 & \textcolor{red}{3} & 4\\ 1 & 2 & 3 & \rnode{E}{\color{red}{4}} \end{bmatrix} \ncbox[linecolor =IndianRed, fillstyle=solid, fillcolor=MistyRose, opacity=0.2, boxsize=5pt, linearc=0.05, nodesep=0.8pt]{B}{E}$

\end{document}


Edit:

If you do no want the diagonal frame to touch the brackets, you can, omong other possibilities, change the value of the \ncbox parameter linearc=0.18to have round extremities, or nest a simple matrix in bmatrix and add some spacingon each side, like this:

      $A = \begin{bmatrix} \:\begin{matrix} \rnode{B}{\color{red}{1}} & 2 & 3 & 4\\ 1 & \color{red}{2} & 3 & 4\\ 1 & 2 & \textcolor{red}{3} & 4\\ 1 & 2 & 3 & \rnode{E}{\color{red}{4}} \end{matrix}\: \end{bmatrix} \ncbox[linecolor =IndianRed, fillstyle=solid, fillcolor=MistyRose, opacity=0.2, boxsize=5pt, linearc=0.05, nodesep=0.8pt]{B}{E}$


• This is also great effort. This is also useful. But why does the rectangle is touching both brackets of matrix? Jan 1, 2020 at 12:50
• That is because of the geometry of the figure: pstricks figures are superimposed on an existing normal matrix, and the value of \arraycolsep is too small (5pt), That's why I added a small opacity in the version with background. You can change \arraycolsep beforehand or add empty columns at the beginning and end of the matrix, or nest a simple matrix environment with some spacing on the left and on the right in a bmatrix. Jan 1, 2020 at 13:14
• Agree with you comment. Now I can adjust spacing. Once again thanks for coming across my question and for valuable comment. Jan 1, 2020 at 13:22
• I've added two more possibilities. Jan 1, 2020 at 13:32
• I also have had problems with auto-pst-pdf for some times. There new options to compile pstricks code with pdflatex but I haven't tested them yet. The simplest would be to compile with xelatex (no need for auto-pst-pdf in this case). Sep 25, 2020 at 19:02