# How to sidebrace and overbrace a matrix?

I am trying to make a matrix with side braces and over braces like this:

Any ideas? Thanks :)

I use into my code the package kbordermatrix .....I think that [1.5em] (the distance between two rows) is abundant...but you can choose to changed it, increase it or decrease it.

\documentclass[a4paper,12pt]{article}
\usepackage{mathtools,amssymb}
\usepackage{kbordermatrix}

\renewcommand{\kbldelim}{(}
\renewcommand{\kbrdelim}{)}

\begin{document}
$\kbordermatrix{ & \overbrace{}^{p} & \overbrace{}^{q} \cr r\begin{cases} & \\ \end{cases} \mkern-35mu & \mathrm{even} & \mathrm{odd} \\[1.5em] s\begin{cases} & \\ \end{cases} \mkern-35mu & \mathrm{odd} & \mathrm{even} }$
\end{document}


Use \matrix, \phantom:

$$\def\p{\vphantom{\Bigm\{}\rm} \def\q{\overbrace{\hphantom{\rm even}}} \matrix{r \Bigl\{\cr\cr s \Bigl\{} \mathop{\pmatrix{\p even & \p odd \cr\cr \p odd & \p even}}^ {\matrix{\q^p & \q^q}}$$


The following echo's wipet's approach of \vphantoms to construct the display from left-to-right using an array for each of the columns (first = r & s braces, second = left delimiter, third = even & odd plus p-overbrace, fourth = odd & even plus q-overbrace, fifth = right delimiter).

\documentclass{article}

\usepackage{amsmath,lipsum}

\begin{document}

\lipsum*[1]
$\newcommand{\vphB}{\vphantom{\Big\{}} \newcommand{\addlinegap}[1][\jot]{\\\\[\dimexpr-\normalbaselineskip+#1]} \begin{array}{ r @{}} r \Bigl\{ \addlinegap s \Bigl\{ \end{array} \left( \begin{array}{@{}c@{}} \vphB \addlinegap \vphB \end{array} \right.\kern-\nulldelimiterspace \overbrace{\begin{array}{@{}c@{}} \vphB \text{even} \addlinegap \vphB \text{odd} \end{array}}^p \hspace{\arraycolsep} \overbrace{\begin{array}{@{}c@{}} \vphB \text{odd} \addlinegap \vphB \text{even} \end{array}}^q \kern-\nulldelimiterspace\left.\begin{array}{@{}c@{}} \vphB \addlinegap \vphB \end{array} \right)$
\lipsum*[2]
\end{document}


And one solution with nicematrix :-)

\documentclass{article}
\usepackage{nicematrix}
\usepackage{tikz}   % needed after version 5.0
\usetikzlibrary{decorations.pathreplacing,
calligraphy,
fit}
\tikzset{
B/.style = {decorate,
decoration={calligraphic brace, amplitude=2pt,
pre =moveto, pre  length=1pt,
post=moveto, post length=1pt,
raise=#1},
thick,
pen colour=black},
B/.default=4pt,
arr/.style = {draw=blue}
}

\begin{document}
$\begin{pNiceArray}{*{2}{>{\rule[-1ex]{0pt}{3ex}}c} } \mathrm{even} & \mathrm{odd} \\[1ex] \mathrm{odd} & \mathrm{even} \\ \CodeAfter \begin{tikzpicture} \draw[B] (1-1.west |- 1-2.north) -- node[above=5pt] {p} (1-1.east |- 1-2.north); \draw[B] (2-2.west |- 1-2.north) -- node[above=5pt] {q} (2-2.east |- 1-2.north); \draw[B=7pt] (1-1.west |- 1-1.south) -- node[left =8pt] {r} (1-1.west |- 1-2.north); \draw[B=7pt] (1-1.west |- 2-1.south) -- node[left =8pt] {s} (1-1.west |- 2-1.north); \end{tikzpicture} \end{pNiceArray}$
\end{document}