# Add Overbrace to describe a column of matrix

I want to add some over-braces to a matrix to get the following output:

However, this this what I managed to get:

I don't know how to get the neuron 1 and neuron 2 headings...I was thinking of overbraces but not sure how to use it in this case. The stuff on the right of the matrix is not aligned properly...My equation number is moving to the next line as well..Can anyone help me please.

My code is as follows (I am using amsmath package):

    \begin{matrix} J = \begin{bmatrix} \frac{\delta e_{1,1}}{\delta w_{1,1}} & \frac{\delta e_{1,1}}{\delta w_{1,2}} & \cdots & \frac{\delta e_{1,1}}{\delta w_{j,1}} & \cdots \\[0.5em] \frac{\delta e_{1,2}}{\delta w_{1,1}} & \frac{\delta e_{1,2}}{\delta w_{1,2}} & \cdots & \frac{\delta e_{1,2}}{\delta w_{j,1}} & \cdots \\[0.5em] \cdots & \cdots & \cdots & \cdots & \cdots \\[0.5em] \frac{\delta e_{1,M}}{\delta w_{1,1}} & \frac{\delta e_{1,M}}{\delta w_{1,2}} & \cdots & \frac{\delta e_{1,M}}{\delta w_{j,1}} & \cdots \\[0.5em] \cdots & \cdots & \cdots & \cdots & \cdots \\[0.5em] \frac{\delta e_{P,1}}{\delta w_{1,1}} & \frac{\delta e_{P,1}}{\delta w_{1,2}} & \cdots & \frac{\delta e_{P,1}}{\delta w_{j,1}} & \cdots \\[0.5em] \frac{\delta e_{P,1}}{\delta w_{1,1}} & \frac{\delta e_{np,2}}{\delta w_{1,2}} & \cdots & \frac{\delta e_{P,2}}{\delta w_{j,1}} & \cdots \\[0.5em] \cdots & \cdots & \cdots & \cdots & \cdots \\[0.5em] \frac{\delta e_{P,M}}{\delta w_{1,1}} & \frac{\delta e_{P,M}}{\delta w_{1,2}} & \cdots & \frac{\delta e_{P,M}}{\delta w_{j,1}} & \cdots \\[0.5em] \end{bmatrix} %\!\! \begin{aligned} &\left.\begin{matrix} m = 1 \\[0.5em] m = 2 \\[0.5em] \cdots \\[0.5em] m = M \\[0.5em] \end{matrix} \right\} % p = 1\\ &\begin{matrix} \phantom{\cdots}\cdots\\[0.5em] \end{matrix}\\ % &\left.\begin{matrix} m = 1 \\[0.5em] m = 2 \\[0.5em] \cdots \\[0.5em] m = M\\[0.5em] \end{matrix}\right\}% p = P\\ \end{aligned} \end{matrix}

-
Welcome to TeX.SE. This to me is a job for \tikzmark but I think there are already solutions here that illustrate how to use that. For instance see: Matrix with labels nested in braces. –  Peter Grill Apr 10 '13 at 20:58
I think you need \partial instead of \delta –  percusse Apr 10 '13 at 21:07
thanks for pointing this out...I didn't even notice this error.. –  Saed Apr 10 '13 at 21:21
@KevinC: Yeah, pretty sure there are non-Tikz solutions, even probably a pure TeX solution, but IMHO, I am not sure why they would be any better (except perhaps the package overhead). –  Peter Grill Apr 10 '13 at 23:28
@Saed: I think the answers in How do I label different rows or columns of a matrix using braces? will adequately address your problem. –  Kevin C Apr 10 '13 at 23:52

Here's one (TikZ-free) possibility; \overmat writes its first argument above the entries enclosed in the second argument; \bovermat (in the second example below) acts analogously, but showing an overbrace. I also fixed the alignment of the expressions to the right using some phantoms:

\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor}

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

\begin{document}

\begin{matrix} J = \begin{bmatrix} \overmat{neuron 1}{\frac{\partial e_{1,1}}{\partial w_{1,1}} & \frac{\partial e_{1,1}}{\partial w_{1,2}}} & \overmat{\mkern-3.5mu\cdots}{\cdots} & \overmat{neuron j}{\frac{\partial e_{1,1}}{\partial w_{j,1}} & \frac{\partial e_{1,1}}{\partial w_{j,1}}} & \cdots \\[0.5em] % \frac{\partial e_{1,2}}{\partial w_{1,1}} & \frac{\partial e_{1,2}}{\partial w_{1,2}} & \cdots & \frac{\partial e_{1,2}}{\partial w_{j,1}} & \cdots \\[0.5em] % \cdots & \cdots & \cdots & \cdots & \cdots \\[0.5em] % \frac{\partial e_{1,M}}{\partial w_{1,1}} & \frac{\partial e_{1,M}}{\partial w_{1,2}} & \cdots & \frac{\partial e_{1,M}}{\partial w_{j,1}} & \cdots \\[0.5em] % \cdots & \cdots & \cdots & \cdots & \cdots \\[0.5em] % \frac{\partial e_{P,1}}{\partial w_{1,1}} & \frac{\partial e_{P,1}}{\partial w_{1,2}} & \cdots & \frac{\partial e_{P,1}}{\partial w_{j,1}} & \cdots \\[0.5em] % \frac{\partial e_{P,1}}{\partial w_{1,1}} & \frac{\partial e_{np,2}}{\partial w_{1,2}} & \cdots & \frac{\partial e_{P,2}}{\partial w_{j,1}} & \cdots \\[0.5em] % \cdots & \cdots & \cdots & \cdots & \cdots \\[0.5em] % \frac{\partial e_{P,M}}{\partial w_{1,1}} & \frac{\partial e_{P,M}}{\partial w_{1,2}} & \cdots & \frac{\partial e_{P,M}}{\partial w_{j,1}} & \cdots \\[0.5em] \end{bmatrix} \begin{aligned} &\left.\begin{matrix} \partialphantom m = 1 \\[0.5em] \partialphantom m = 2 \\[0.5em] \cdots \\[0.5em] \partialphantom m = M \\[0.5em] \end{matrix} \right\} % p = 1\\ &\begin{matrix} \\[-1.67em]\phantom{\cdots}\cdots \end{matrix}\\ % &\left.\begin{matrix} \partialphantom m = 1 \\[0.5em] \partialphantom m = 2 \\[0.5em] \cdots \\[0.5em] \partialphantom m = M\\[0.5em] \end{matrix}\right\}% p = P\\ \end{aligned} \end{matrix}

\end{document}


And a variation with braces:

\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor}

\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} J = \begin{bmatrix} \bovermat{neuron 1}{\frac{\partial e_{1,1}}{\partial w_{1,1}} & \frac{\partial e_{1,1}}{\partial w_{1,2}}} & \overmat{\mkern-3.5mu\cdots}{\cdots} & \bovermat{neuron j}{\frac{\partial e_{1,1}}{\partial w_{j,1}} & \frac{\partial e_{1,1}}{\partial w_{j,1}}} & \cdots \\[0.5em] % \frac{\partial e_{1,2}}{\partial w_{1,1}} & \frac{\partial e_{1,2}}{\partial w_{1,2}} & \cdots & \frac{\partial e_{1,2}}{\partial w_{j,1}} & \cdots \\[0.5em] % \cdots & \cdots & \cdots & \cdots & \cdots \\[0.5em] % \frac{\partial e_{1,M}}{\partial w_{1,1}} & \frac{\partial e_{1,M}}{\partial w_{1,2}} & \cdots & \frac{\partial e_{1,M}}{\partial w_{j,1}} & \cdots \\[0.5em] % \cdots & \cdots & \cdots & \cdots & \cdots \\[0.5em] % \frac{\partial e_{P,1}}{\partial w_{1,1}} & \frac{\partial e_{P,1}}{\partial w_{1,2}} & \cdots & \frac{\partial e_{P,1}}{\partial w_{j,1}} & \cdots \\[0.5em] % \frac{\partial e_{P,1}}{\partial w_{1,1}} & \frac{\partial e_{np,2}}{\partial w_{1,2}} & \cdots & \frac{\partial e_{P,2}}{\partial w_{j,1}} & \cdots \\[0.5em] % \cdots & \cdots & \cdots & \cdots & \cdots \\[0.5em] % \frac{\partial e_{P,M}}{\partial w_{1,1}} & \frac{\partial e_{P,M}}{\partial w_{1,2}} & \cdots & \frac{\partial e_{P,M}}{\partial w_{j,1}} & \cdots \\[0.5em] \end{bmatrix} \begin{aligned} &\left.\begin{matrix} \partialphantom m = 1 \\[0.5em] \partialphantom m = 2 \\[0.5em] \cdots \\[0.5em] \partialphantom m = M \\[0.5em] \end{matrix} \right\} % p = 1\\ &\begin{matrix} \\[-1.67em]\phantom{\cdots}\cdots \end{matrix}\\ % &\left.\begin{matrix} \partialphantom m = 1 \\[0.5em] \partialphantom m = 2 \\[0.5em] \cdots \\[0.5em] \partialphantom m = M\\[0.5em] \end{matrix}\right\}% p = P\\ \end{aligned} \end{matrix}

\end{document}


-
This is great...Just what I was looking for in the first place... I already managed to solve it using TikZ based on the comments on the question and How to Specify two level row and column labels of a matrix by braces?...But I will mark this as answer...not many solutions out there without TikZ...This will be useful to people like me..who never used TikZ before..Thanks again. –  Saed Apr 11 '13 at 19:30
Quick question...What does the xcolor package do here? –  Saed Apr 11 '13 at 19:39
@Saed You're welcome! Yes, TikZ is powerful, but some things can be done without it :-) Regarding your question, the difference between \overmat and \bovermat is simply that the former typesets the overbace in white color. –  Gonzalo Medina Apr 11 '13 at 19:48