# Overbrace and right braces to explain matrix

I am trying to achieve having overbraces and right braces to a matrix to explain columns and rows. This is what I achieved so far :

This is the code I have used :

\documentclass{article}
\usepackage{amsmath}

\newcommand\overmat[2]{%
\makebox[0pt][l]{$\smash{\color{white}\overbrace{\phantom{% \begin{matrix}#2\end{matrix}}}^{\text{\color{black}#1}}}$}#2}
\newcommand\bovermat[2]{%
\makebox[0pt][l]{$\smash{\overbrace{\phantom{% \begin{matrix}#2\end{matrix}}}^{\text{#1}}}$}#2}
\newcommand\partialphantom{\vphantom{\frac{\partial e_{P,M}}{\partial w_{1,1}}}}

\begin{document}

$\begin{matrix} \begin{pmatrix} \bovermat{$m_1$colonnes}{\lambda_1 & 0 & \ldots & 0 & 0} & \bovermat{$n - m_1\$ colonnes}{ & & & & } \0.5em] % 0 & \lambda_1 & \ldots & 0 & 0 & & & & & \\[0.5em] % 0 & 0 & \ddots & 0 & 0 & & & \mbox{\Huge U} & & \\[0.5em] % 0 & 0 & \ldots & \lambda_1 & 0 & & & & & \\[0.5em] % 0 & 0 & \ldots & 0 & \lambda_1 & & & & & \\[0.5em] % & & & & & & & & & \\[0.5em] % & & & & & & & & & \\[0.5em] % & & \mbox{\Huge O} & & & & & \mbox{\Huge V} & & \\[0.5em] % & & & & & & & & & \\[0.5em] % & & & & & & & & & \\[0.5em] \end{pmatrix} \begin{aligned} &\left.\begin{matrix} \partialphantom \\[0.5em] \partialphantom \\[0.5em] \partialphantom \\[0.5em] \partialphantom \\[0.5em] \partialphantom \\[0.5em] \end{matrix} \right\} % m_1 \text{ lignes}\\ &\left.\begin{matrix} \partialphantom \\[0.5em] \partialphantom \\[0.5em] \partialphantom \\[0.5em] \partialphantom \\[0.5em] \partialphantom \\[0.5em] \end{matrix}\right\}% n - m_1 \text{ lignes}\\ \end{aligned} \end{matrix} \end{document}  Now the only problem I have, is that I would want the first overprice to encompass up to the last zero on the first line of the matrix and the second brace should start at the end of the first and go up to the end of the matrix. I am however at a total loss on how I should be doing it. Any help would be appreciated. • What is this \partialphantom command? Jul 25, 2020 at 19:40 • To be honest, I don't know, I adapted my code from tex.stackexchange.com/a/108053/154126 to get somewhat what I wanted Jul 25, 2020 at 19:45 • @Sephya There, the definition of this macro is given as follows \newcommand\partialphantom{\vphantom{\frac{\frac{\partial e_{P,M}}}{\partial w_{1,1}}} update your code with the definition of this macro. Jul 25, 2020 at 20:07 • Ohhh thanks, I did, but it doesn't solve the fact that my 2 overbraces are still not aligned correctly Jul 25, 2020 at 20:12 ## 2 Answers Here is a solution with {NiceMatrix} of nicematrix (≥ 6.4 of 2021-11-23). \documentclass{article} \usepackage{nicematrix} \begin{document} \setcounter{MaxMatrixCols}{11} \[\begin{NiceMatrix}[columns-width=4mm] \lambda_1 & 0 & \cdots & 0 & 0 & \NotEmpty \Block{5-5}<\Huge>{U}& & & & \NotEmpty & \Block{5-1}{\quad m_1 \text{ lignes}}\\ 0 & \lambda_1 & \cdots & 0 & 0 & \\ 0 & 0 & \smash{\ddots} & 0 & 0 & \\ 0 & 0 & \cdots & \lambda_1 & 0 & \\ 0 & 0 & \cdots & 0 & \lambda_1 & \\ \Block{5-5}<\Huge>{O} & & & & & \Block{5-5}<\Huge>{V} & & & & & \Block{5-1}{\quad n-m_1 \text{ lignes}} \\ \\ \\ \\ \\ \CodeAfter \SubMatrix({1-1}{10-10}) \OverBrace[shorten,yshift=2mm]{1-1}{1-5}{m_1 \text{ colonnes}} \OverBrace[shorten,yshift=2mm]{1-6}{1-10}{n-m_1 \text{ colonnes}} \SubMatrix{.}{1-1}{5-10}{\}}[xshift=4mm] \SubMatrix{.}{6-1}{10-10}{\}}[xshift=4mm] \end{NiceMatrix}

\end{document}


You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Complicated, but doable.

\documentclass{article}
\usepackage{amsmath}

\begin{document}


\end{document}


You might enjoy studying it. There are two “guesses”: the amount by which the top line should be lowered and the amount by which the explanations on the right have to be pushed left.

I think it would be simpler to just write the block matrix and give the explanation of the sizes in the text.

• Wow, thank you so much 😊 this is exactly what I was looking for Jul 25, 2020 at 21:46
• @Sephya You're welcome! It was fun to do it. Jul 25, 2020 at 21:53