# How to properly align matrices with an overbrace?

I tried to solve this question by myself and now I'm looking for some help. How can I make the last two matrices parallel with the first matrix?

Here is my code:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T2A,T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage{titletoc}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{mathtools}
\begin{document}

$A = \begin{pmatrix} 2 & 0 & 0 & 0\\ 0 & 1 & 3 & 0\\ 0 & 0 & 1 & 0\\ 0 & 0 & 0 & -1\\ \end{pmatrix}$
=$\begin{matrix} \overbrace{ \begin{pmatrix} 2 & 0 & 0 & 0\\ 0 & 1 & 3 & 0\\ 0 & 0 & 1 & 0\\ 0 & 0 & 0 & -1\\ \end{pmatrix}}^{D:=} \end{matrix}$
+$\begin{matrix} \overbrace{ \begin{pmatrix} 0 & 0 & 0 & 0\\ 0 & 0 & 3 & 0\\ 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0\\ \end{pmatrix}}^{N:=} \end{matrix}$
\end{document}


• Welcome to TeX.SE. Provide your preamble or full code with the question.
– topu
Nov 28 '21 at 6:03
• Why do you use 3 separate inline-math instances? Why do you encase 2 of the 3 pmatrix environments in matrix environments?
– Mico
Nov 28 '21 at 6:17
• thanks for the answer, i just learned latex, so some parts are still a bit complicated for me, i watched some videos on youtube but i still cant solve the problem....
– nus
Nov 28 '21 at 6:37

Just use pmatrix alone.

\documentclass{amsart}

\begin{document}

\begin{equation*}
A =
\begin{pmatrix}
2 & 0 & 0 & 0\\
0 & 1 & 3 & 0\\
0 & 0 & 1 & 0\\
0 & 0 & 0 & -1\\
\end{pmatrix}
=
\overbrace{
\begin{pmatrix}
2 & 0 & 0 & 0\\
0 & 1 & 3 & 0\\
0 & 0 & 1 & 0\\
0 & 0 & 0 & -1\\
\end{pmatrix}
}^{D:=}
+
\overbrace{
\begin{pmatrix}
0 & 0 & 0 & 0\\
0 & 0 & 3 & 0\\
0 & 0 & 0 & 0\\
0 & 0 & 0 & 0\\
\end{pmatrix}
}^{N:=}
\end{equation*}

\end{document}

• thank you very very much!
– nus
Nov 28 '21 at 6:33

There are two separate issues with your code; both are causing the misalignment.

• You use three three separate (inline) math instances instead of single math instance.

• You are inappropriately encasing the 2nd and 3rd pmatrix environments in matrix environments.

In addition to fixing these two issues, you should probably also be looking into using display math rather than inline math, in order to give the formula more visual prominence.

\documentclass{article}
\usepackage{amsmath}      % for 'pmatrix' and 'matrix' environments
% and '\overbrace' macro
\usepackage{showframe}    % draw framelines around textblock
\setlength\parindent{0pt} % just for this example
\begin{document}
before:

$A = \begin{pmatrix} 2 & 0 & 0 & 0\\ 0 & 1 & 3 & 0\\ 0 & 0 & 1 & 0\\ 0 & 0 & 0 & -1\\ \end{pmatrix}$
=
$\begin{matrix} \overbrace{ \begin{pmatrix} 2 & 0 & 0 & 0\\ 0 & 1 & 3 & 0\\ 0 & 0 & 1 & 0\\ 0 & 0 & 0 & -1\\ \end{pmatrix}}^{D:=} \end{matrix}$
+
$\begin{matrix} \overbrace{ \begin{pmatrix} 0 & 0 & 0 & 0\\ 0 & 0 & 3 & 0\\ 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0\\ \end{pmatrix}}^{N:=} \end{matrix}$

\bigskip
inline math mode:

$A = \begin{pmatrix} 2 & 0 & 0 & 0\\ 0 & 1 & 3 & 0\\ 0 & 0 & 1 & 0\\ 0 & 0 & 0 & -1 \end{pmatrix} = \overbrace{ \begin{pmatrix} 2 & 0 & 0 & 0\\ 0 & 1 & 3 & 0\\ 0 & 0 & 1 & 0\\ 0 & 0 & 0 & -1 \end{pmatrix}}^{D:=} + \overbrace{ \begin{pmatrix} 0 & 0 & 0 & 0\\ 0 & 0 & 3 & 0\\ 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 \end{pmatrix}}^{N:=}$

\bigskip\medskip
display math mode
$A = \begin{pmatrix} 2 & 0 & 0 & 0\\ 0 & 1 & 3 & 0\\ 0 & 0 & 1 & 0\\ 0 & 0 & 0 & -1 \end{pmatrix} = \overbrace{ \begin{pmatrix} 2 & 0 & 0 & 0\\ 0 & 1 & 3 & 0\\ 0 & 0 & 1 & 0\\ 0 & 0 & 0 & -1 \end{pmatrix}}^{D:=} + \overbrace{ \begin{pmatrix} 0 & 0 & 0 & 0\\ 0 & 0 & 3 & 0\\ 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 \end{pmatrix}}^{N:=}$

\end{document}


Addendum: If you want to reduce the width of the curly braces generated by the \overbrace instructions, you could do so along the lines of the following code. The matrix on the left is as in the code above; the one on the right (a) places the tall parentheses outside the scope of the \overbrace directive and (b) uses a \smash directive and a tall typographic strut to determine the height of the tall parentheses. (Hint: the tall parentheses must be tall enough to encase a four-row matrix, but no taller.)

\documentclass{article}
\usepackage{amsmath}
%% Define a (typographic) strut that's as tall as a four-row matrix:
\newcommand\bigstrut{\vphantom{\begin{matrix} 0\\0\\0\\0 \end{matrix}}}
\usepackage{mleftright} % for '\mleft' and '\mright' macros

\begin{document}
$\overbrace{ \begin{pmatrix} 0 & 0 & 0 & 0\\ 0 & 0 & 3 & 0\\ 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 \end{pmatrix}}^{N:=} \quad\text{vs.}\quad \mleft(\, % tall opening parenthesis, followed by thinspace \smash{\overbrace{ \begin{matrix} 0 & 0 & 0 & 0\\ 0 & 0 & 3 & 0\\ 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 \end{matrix}}^{N:=}} \bigstrut % insert a tall typographic strut \,\mright) % thinspace, followed by tall closing parenthesis$
\end{document}

• Thank you very much for your answer, I will continue to work hard on the road of learning latex
– nus
Nov 28 '21 at 6:43
• @nus - You're most welcome. Please also note the addendum to my initial answer, where I discuss how to reduce the widths of the curly braces created by the \overbrace directives.
– Mico
Nov 28 '21 at 11:47

If you want a solution with a reduced width of the curly braces, as suggested by Mico in its answer, you can also try nicematrix (≥ 6.4 of 2021-11-23):

\documentclass{amsart}
\usepackage{nicematrix}

\begin{document}

\begin{equation*}
A =
\begin{pmatrix}
2 & 0 & 0 & 0\\
0 & 1 & 3 & 0\\
0 & 0 & 1 & 0\\
0 & 0 & 0 & -1\\
\end{pmatrix}
=
\begin{pNiceMatrix}
2 & 0 & 0 & 0\\
0 & 1 & 3 & 0\\
0 & 0 & 1 & 0\\
0 & 0 & 0 & -1\\
\CodeAfter
\OverBrace[shorten,yshift=1mm]{1-1}{4-4}{\scriptstyle D:=}
\end{pNiceMatrix}
+
\begin{pNiceMatrix}
0 & 0 & 0 & 0\\
0 & 0 & 3 & 0\\
0 & 0 & 0 & 0\\
0 & 0 & 0 & 0\\
\CodeAfter
\OverBrace[shorten,yshift=1mm]{1-1}{4-4}{\scriptstyle N:=}
\end{pNiceMatrix}
\end{equation*}

\end{document}


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

Be aware that the braces and their labels are in an overlapping position.