I'm trying to write the following block matrix but i have some points i need a help to fix it. first, in the block matrix where i write some headings (k_1, k_2 n-k_1-k_2) there is between them vertical line i don't want to keep it. i need it only in the block matrix inside and for the diagonal matrix \lambda_1 \ddots \lambda_1 and the others i don't want the space before the first row i.e. before the \lambda_1 and \lambda_2.

Hope someone can help me. Here is the code that i write it:

$\begin{blockarray}{cc|c|c} & k_{1}&k_2&n-k_{1}-k_{2}\\ \begin{block}{c(c|c|c)} &\lambda_1 & & \\ k_1 & \hspace{0.75cm} \ddots \hspace{0.75cm} & & \\ & \hspace{2cm} \lambda_1 & & \\\cline{2-4} & &\lambda_2 & \\ k_2 & \Phi &\hspace{0.75cm} \ddots \hspace{0.75cm} & \\ & & \hspace{1.5cm} \lambda_2 & \\ \cline{2-4} & & &\\ n-k_1-k_2 & \Phi & \Phi & \bar{\bar{M}}_3\\ & & &\\ \end{block} \end{blockarray}$


Here is a solution. We have to use 10 columns and booktabs to have horizontal rules that do not get across the parentheses:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fourier}
\usepackage{mathtools, blkarray, booktabs}

\begin{document}

$\aboverulesep = 0pt \belowrulesep = 0pt \begin{blockarray}{r*{3}{>{\scriptstyle}c>{\scriptstyle}c@{\enspace}>{\scriptstyle\quad}c}} & & {k_{1}} & & & k_2& & & \mathclap{n-k_{1}-k_{2}} \\% \begin{block}{>{\scriptstyle}r(*{2}{ccc|}cc>{\enspace}c<{\enspace})} &\rule{0pt}{3ex}\:\lambda_1 & & & & & & & & \\ k_1 & & \ddots & & & & & & & \\ & & & \lambda_1 & & & & & & \\ \cmidrule(lr){2-10} & & & &\lambda_2 & & & & & \\ k_2 & & \Phi & & & \ddots \hspace{0.75cm} & & & & \\ & & & & & & \lambda_2 & \\ \cmidrule(lr){2-10} & & & & & & & & & \\ n-k_1-k_2 & & \Phi & & & \Phi & &\enspace &\overline{\overline{M}}_3 & \\[0.5ex] \end{block} \end{blockarray}$

\end{document}


You can use the package nicematrix which has been designed to draw such matrices.

\documentclass{article}
\usepackage{nicematrix}
\NiceMatrixOptions{code-for-first-row = \scriptstyle , code-for-first-col = \scriptstyle}
\begin{document}
$\begin{pNiceArray}{CCC|CCC|C}[first-row,first-col] & & k_1 & & & k_2 & & n-k_1-k_2 \\ & \lambda_1 & & & & & & \hspace*{2cm} \\ k_1 & & \Ddots & & & & \\ & & & \lambda_1 & & & \\ \hline & & & & \lambda_2 & & \\ k_2 & & \Phi & & & \Ddots & &\\ & & & & & & \lambda_2 & \\ \hline & & & & & & & \\ n-k_1-k_2 & & \Phi & & & \Phi & & \overline{\overline{M}}_3 \\ & & & & & & & \\ \end{pNiceArray}$
\end{document}