[Tex/LaTex] How to put a box around a column and a row in a matrix in latex

matrices

I have a matrix in latex and I want to put a box around the first column and another box around the first row to highlight them. Additionally it should be possible to give the highlighted fields a name.
The names should be displayed on the right side for the highlited first row and below the highlighted column.

\begin{align}
m =
\left( \begin{array}{ccc}
1 & \dots & 3 \\
\vdots & ~ & \vdots \\
4 & \dots & 5 \\
\end{array} \right) 
\end{align}

Best Answer

You can use nicematrix which will create the Tikz nodes for you. Then, you draw the box around the cells with the Tikz library fit.

\documentclass{article}
\usepackage{nicematrix,tikz}
\usetikzlibrary{fit}
\begin{document}
\[m =
\begin{pNiceMatrix}[right-margin = 4pt] 
1 & \Ldots & 3 \\
\Vdots & ~ & \Vdots \\
4 & \Ldots & 5 \\
\CodeAfter
  \tikz \node [draw, rounded corners, fit = (1-3) (last-3)] { } ; 
\end{pNiceMatrix}\]
\end{document}

The option right-margin is used to add space before the right parenthese.

Result of the above code

In recent version of nicematrix, it's also possible to use the built-in command \Block (and you don't have to use explicitely Tikz).

\documentclass{article}
\usepackage{nicematrix}
\begin{document}
\[m =
\begin{pNiceMatrix}[right-margin = 5pt] 
1 & \Ldots & \Block[draw,rounded-corners]{*-1}{}3 \\
\Vdots & ~ & \Vdots \\
4 & \Ldots & 5 \\
\end{pNiceMatrix}\]
\end{document}

The output is not exactly the same.

Output of the above code