5

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
  • 603,163
SandyM
  • 2,757

2 Answers2

8

Like this?

Output of the first code

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:

enter image description here

\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}

F. Pantigny
  • 40,250
Zarko
  • 296,517
  • Nice work by you, actually i want to draw a single rectangle which covers all four elements please don't mind. – SandyM Jan 01 '20 at 08:02
  • @Maths4Sandy, see if added example is what you looking for. – Zarko Jan 01 '20 at 09: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. – SandyM Jan 01 '20 at 09:38
5

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} 

enter image description here

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}
    \]

enter image description here

Bernard
  • 271,350
  • This is also great effort. This is also useful. But why does the rectangle is touching both brackets of matrix? – SandyM Jan 01 '20 at 12:50
  • 1
    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. – Bernard Jan 01 '20 at 13:14
  • Agree with you comment. Now I can adjust spacing. Once again thanks for coming across my question and for valuable comment. – SandyM Jan 01 '20 at 13:22
  • I've added two more possibilities. – Bernard Jan 01 '20 at 13:32
  • @Bernard I am getting the error Package auto-pst-pdf Error: . Or turn off auto-pst-pdf.} on compiling your code. Any help? – Subhajit Paul Sep 25 '20 at 18:57
  • 1
    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). – Bernard Sep 25 '20 at 19:02
  • and if i want the last node in 4 appear open, i.e. start close in 1 and end in 4 open, how do this – user89940 Feb 06 '21 at 09:41