# Insert a 2x2 matrix into a 5x5 matrix

I need to insert a 2x2 matrix evenly into the 4th and 5th rows and the 4th and 5th columns of a 5x5 matrix. My code needs tweaking.

\begin{equation*}
a_1=
\begin{bmatrix}
1 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & & {\begin{bmatrix} & b & \end{bmatrix}}\\
0 & 0 & 0 &
\end{bmatrix}
\end{equation*}


Or maybe with nicematrix. It does require an extra package (which in turn loads other stuff, most prominently TikZ), yet the spacing between the columns does not get modified and you have full control over all aspects of the appearance.

\documentclass{article}
\usepackage{amsmath}
\usepackage{nicematrix}
\begin{document}
\begin{equation*}
a_1=\begin{bNiceMatrix}[name=mymatrix]
1 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & \phantom{0} & \phantom{0}\\
0 & 0 & 0 & \phantom{0} & \phantom{0}
\end{bNiceMatrix}
\begin{tikzpicture}[remember picture,overlay]
\node[fit=(mymatrix-4-4)(mymatrix-5-5),inner sep=-0.2ex,text height=1.2em] (f){$b$};
\draw[thick] ([xshift=0.5ex]f.south west)-| (f.north west) -- ++ (0.5ex,0)
([xshift=-0.5ex]f.south east)-| (f.north east) -- ++ (-0.5ex,0);
\end{tikzpicture}
\end{equation*}
\end{document}


Instead of bmatrix for the 2x2 matrix I used matrix. But it is only a personal preference and can be exchanged without problems.

\documentclass{article}

\usepackage{amsmath}

\begin{document}

\begin{equation*}
a_1=
\begin{bmatrix}
\begin{matrix}
1 & 0 & 0\\
0 & 0 & 0 \\
0 & 0 & 0
\end{matrix} &
\begin{matrix}
0 & 0\\
0 & 0 \\
0 & 0
\end{matrix}\\
\begin{matrix}
0 & 0 & 0\\
0 & 0 & 0
\end{matrix} &
\begin{matrix} & b & \end{matrix}\\
\end{bmatrix}
\end{equation*}

\end{document}


Result with matrix:

Result with bmatrix:

A solution with blkarray:

\documentclass{article}
\usepackage{multirow}
\usepackage{blkarray, bigstrut}
\usepackage{amsmath}

\begin{document}

\begin{equation*}
a_1=
\left[
\begin{blockarray}{@{\,}ccccc@{\:}}
\bigstrut[t]
1 & 0 & 0 & 0 & 0 & \\
0 & 0 & 0 & 0 & 0 & \\
0 & 0 & 0 & 0 & 0 & \\
\begin{block}{@{\,}ccc[\BAmulticolumn{2}{!{}c!{}}@{\:}]}
0 & 0 & 0 & \multirow{2}{*}{b}\\
0 & 0 & 0 & \\
\end{block}
\BAnoalign{\vskip -7ex}
\end{blockarray}\right]
\end{equation*}

\end{document}