# Box inside matrix [duplicate]

I'm trying to do something like this:

The matrix was generated with the following code:

    $$\begingroup % keep the change local \setlength\arraycolsep{2pt} \begin{pmatrix} h_{11}&h_{12}&h_{13}&h_{14}&\cdots&h_{1n}&\cdots&\cdots&h_{1m}\\ h_{21}&h_{22}&h_{23}&h_{24}&\cdots&h_{2n}&\cdots&\cdots&h_{2m}\\ 0 & h_{32} &h_{33}&h_{34}&\cdots&h_{3n}&\cdots&\cdots&h_{3m}\\ 0 & 0 & h_{43}&h_{44}&\cdots&h_{4n}&\cdots&\cdots&h_{4m}\\ \vdots & \vdots & & \ddots & \ddots & \vdots & & & \vdots \\ 0 & 0 & & & \ddots & h_{n,n}& & & h_{nm} \\ 0 & 0 & & & & h_{n+1,n}& & & h_{n+1,m} \\ \vdots & \vdots & & & & & \ddots & \ddots & \vdots \\ 0 & 0 & \cdots & & \cdots & \cdots & 0 & h_{m,m-1} & h_{m,m} \\ \end{pmatrix} \endgroup$$


Any suggestion?

You can use tikzmark:

\documentclass{article}
\usepackage{mathtools}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\begin{document}
$$\begingroup % keep the change local \setlength\arraycolsep{2pt} \begin{pmatrix} \tikzmarknode{x}{h_{11}}&h_{12}&h_{13}&h_{14}&\cdots&h_{1n}&\cdots&\cdots&h_{1m}\\ h_{21}&h_{22}&h_{23}&h_{24}&\cdots&h_{2n}&\cdots&\cdots&h_{2m}\\ 0 & h_{32} &h_{33}&h_{34}&\cdots&h_{3n}&\cdots&\cdots&h_{3m}\\ 0 & 0 & h_{43}&h_{44}&\cdots&h_{4n}&\cdots&\cdots&h_{4m}\\ \vdots & \vdots & & \ddots & \ddots & \vdots & & & \vdots \\ 0 & 0 & & & \ddots & h_{n,n}& & & h_{nm} \\ 0 & 0 & & & & \tikzmarknode{y}{h_{n+1,n}}& & & h_{n+1,m} \\ \vdots & \vdots & & & & & \ddots & \ddots & \vdots \\ 0 & 0 & \cdots & & \cdots & \cdots & 0 & h_{m,m-1} & h_{m,m} \\ \end{pmatrix} \endgroup$$
\tikz[remember picture,overlay] \draw (x.north west) rectangle (y.south east);
\end{document}


If you want better spacing,

\documentclass{article}
\usepackage{mathtools}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\begin{document}
$$\begingroup % keep the change local \setlength\arraycolsep{2pt} \begin{pmatrix} \tikzmarknode[inner sep=.3em]{x}{h_{11}}&h_{12}&h_{13}&h_{14}&\cdots&h_{1n}&\cdots&\cdots&h_{1m}\\ h_{21}&h_{22}&h_{23}&h_{24}&\cdots&h_{2n}&\cdots&\cdots&h_{2m}\\ 0 & h_{32} &h_{33}&h_{34}&\cdots&h_{3n}&\cdots&\cdots&h_{3m}\\ 0 & 0 & h_{43}&h_{44}&\cdots&h_{4n}&\cdots&\cdots&h_{4m}\\ \vdots & \vdots & & \ddots & \ddots & \vdots & & & \vdots \\ 0 & 0 & & & \ddots & h_{n,n}& & & h_{nm} \\ 0 & 0 & & & & \tikzmarknode[inner sep=.3em]{y}{h_{n+1,n}}& & & h_{n+1,m} \\ \vdots & \vdots & & & & & \ddots & \ddots & \vdots \\ 0 & 0 & \cdots & & \cdots & \cdots & 0 & h_{m,m-1} & h_{m,m} \\ \end{pmatrix} \endgroup$$
\tikz[remember picture,overlay] \draw[red] (x.north west) rectangle (y.south east);
\end{document}


You can use nicematrix.

\documentclass{article}
\usepackage{amsmath}
\usepackage{nicematrix}
\NiceMatrixOptions{exterior-arraycolsep}
\begin{document}

$$\begingroup % keep the change local \setlength\arraycolsep{2pt} \begin{pNiceMatrix}[name=A,] h_{11}&h_{12}&h_{13}&h_{14}&\Cdots&h_{1n}&\Cdots&&h_{1m}\\ h_{21}&h_{22}&h_{23}&h_{24}&\Cdots&h_{2n}&\Cdots&&h_{2m}\\ 0 & h_{32} &h_{33}&h_{34}&\Cdots&h_{3n}&\Cdots&&h_{3m}\\ 0 & 0 & h_{43}&h_{44}&\Cdots&h_{4n}&\Cdots&&h_{4m}\\ \Vdots & \Vdots & & \Ddots & \Ddots & \Vdots & & & \Vdots \\ 0 & 0 & & & & h_{n,n}& \Cdots& & h_{nm} \\ 0 & 0 & & & & h_{n+1,n}& & & h_{n+1,m} \\ \Vdots & \Vdots & & & & & \Ddots & & \Vdots \\ 0 & 0 & \Cdots & & & & 0 & h_{m,m-1} & h_{m,m} \\ \end{pNiceMatrix} \begin{tikzpicture}[overlay,remember picture] \node [red,draw,fit=(A-1-1)(A-6-6),inner sep=1.5pt]{}; \end{tikzpicture} \endgroup$$
\end{document}


• Good solution. Thank you! – Pierrot Dec 21 '19 at 12:35

A solution with pstricks. I added some spacing improvements with cellspace:

    \documentclass[svgnames]{article}
\usepackage{mathtools}
\usepackage{pst-node}
\usepackage[math]{cellspace}
\setlength{\cellspacetoplimit}{3pt}
\setlength{\cellspacebottomlimit}{3pt}
\usepackage{auto-pst-pdf}

\begin{document}

$$\begin{pmatrix} \:\pnode[-2pt,2.5ex]{A} h_{11} &h_{12}&h_{13}&h_{14}&\cdots&h_{1n}&\cdots&\cdots&h_{1m}\\ \: h_{21}&h_{22}&h_{23}&h_{24}&\cdots&h_{2n}&\cdots&\cdots&h_{2m}\\ 0 & h_{32} &h_{33}&h_{34}&\cdots&h_{3n}&\cdots&\cdots&h_{3m}\\ 0 & 0 & h_{43}&h_{44}&\cdots&h_{4n}&\cdots&\cdots&h_{4m}\\ \vdots & \vdots & & \ddots & \ddots & \vdots & & & \vdots \\ 0 & 0 & & & \ddots & h_{n,n}& & & h_{nm} \\ 0 & 0 & & & & h_{n+1,n}\pnode[0.5ex,-1.5ex]{B}& & & h_{n+1,m} \\ \vdots & \vdots & & & & & \ddots & \ddots & \vdots \\ 0 & 0 & \cdots & & \cdots & \cdots & 0 & h_{m,m-1} & h_{m,m} \\ \end{pmatrix} \psframe[linecolor=Crimson](A)(B)$$

\end{document}


• Very nice pstricks solution. Thank you! – Pierrot Dec 21 '19 at 12:32