2

I need to add overbrace on columns of a matrix. I tried How do I label different rows or columns of a matrix using braces? and Build a matrix with overbrace on some columns , They both work until no subscript or entry with different length is added to entries, after adding subscript it is not working properly. The following images shows the problem

This is the code

\[ \vphantom{% phantom stuff for correct box dimensions
    \begin{matrix}
    \overbrace{XYZ}^{\mbox{$R$}}\\ \\ \\ \\ \\ \\
    \underbrace{pqr}_{\mbox{$S$}}
    \end{matrix}}%
\begin{matrix}% matrix for left braces
\vphantom{a}\\
\coolleftbrace{A}{e \\ y\\ y}\\
\coolleftbrace{B}{y \\i \\ m}
\end{matrix}%
\begin{bmatrix}
a & \coolover{R}{b & c & d & \cdots & y & h}\\
e & a & g & h & x & x & x \\
y & y & a & y & y & y & y \\
y & y & y & y & y & y & y \\
y & y & y & y & y & y & y \\
i & j & k & l & x & x & x \\
m &  \coolunder{S}{n & o}  & \coolunder{W}{p & x & x} & x
\end{bmatrix}%
\begin{matrix}% matrix for right braces
\coolrightbrace{x \\ x \\ y\\ y}{T}\\
\coolrightbrace{y \\ y \\ x }{U}
\end{matrix}\]

enter image description here

and after subscript the overbrace R is not on h anymore

enter image description here

This is the code

\[ \vphantom{% phantom stuff for correct box dimensions
    \begin{matrix}
    \overbrace{XYZ}^{\mbox{$R$}}\\ \\ \\ \\ \\ \\
    \underbrace{pqr}_{\mbox{$S$}}
    \end{matrix}}%
\begin{matrix}% matrix for left braces
\vphantom{a}\\
\coolleftbrace{A}{e \\ y\\ y}\\
\coolleftbrace{B}{y \\i \\ m}
\end{matrix}%
\begin{bmatrix}
a_1 & \coolover{R}{b_{1,2} & b_{2,s_1+1} & 0 & \cdots & 0 & h}\\
b_{1,2} & a_2 & b_{2,s_2+1} & h & x & x & x \\
b_{2,s_2+4} & b_{2,s_2+1} & a_3 & y & y & y & y \\
y & y & y & a_{s_{r+1}} & y & y & y \\
y & y & y & y & y & y & y \\
i & j & k & l & x & x & x \\
m &  \coolunder{S}{n & o}  & \coolunder{W}{p & x & x} & x
\end{bmatrix}%
\begin{matrix}% matrix for right braces
\coolrightbrace{x \\ x \\ y\\ y}{T}\\
\coolrightbrace{y \\ y \\ x }{U}
\end{matrix}\]
M a m a D
  • 191
  • please extend your code snippet to complete document with necessary preamble, beginning with \documentclass{...} and ending with \end{document}. – Zarko Aug 18 '18 at 16:05

1 Answers1

2

Here is some manual manipulation of the matrix content to place the braces. eqparbox is used to place boxes of equal size under the same \eqmathbox[<tag>] - this helps for the horizontal placement/spacing. Vertical alignment is done by placing an array before and after the bmatrix.

enter image description here

\documentclass{article}

\usepackage{amsmath,eqparbox,xparse,lipsum}

% https://tex.stackexchange.com/a/34412/5764
\makeatletter
\NewDocumentCommand{\eqmathbox}{o O{c} m}{%
  \IfValueTF{#1}
    {\def\eqmathbox@##1##2{\eqmakebox[#1][#2]{$##1##2$}}}
    {\def\eqmathbox@##1##2{\eqmakebox{$##1##2$}}}
  \mathpalette\eqmathbox@{#3}
}
\makeatother

\begin{document}

\lipsum*[1]
\[
  \begin{array}{@{} r @{}}
    \mathstrut \\ % For \overbrace{..}^{R}
    \mathstrut \\
    {\scriptstyle A}\left\{\begin{array}{@{} c @{}}
      \mathstrut \\ \mathstrut \\ \mathstrut
    \end{array}\right.\kern-\nulldelimiterspace \\
    {\scriptstyle B}\left\{\begin{array}{@{} c @{}}
      \mathstrut \\ \mathstrut \\ \mathstrut
    \end{array}\right.\kern-\nulldelimiterspace \\
    \mathstrut % For \underbrace{..}_{S} and \underbrace{..}_{W}
  \end{array}
  \mkern-5mu
  \begin{bmatrix}% matrix for left braces
    a_1 & \multicolumn{6}{@{} c }{
      \smash{
        \overbrace{
          \begin{array}{ *{5}{c} c @{} }
            \eqmathbox[tb1]{b_{1,2}} &
              \eqmathbox[tb2]{b_{2,s_1+1}} &
              \eqmathbox[tb3]{0} &
              \eqmathbox[tb4]{\cdots} &
              \eqmathbox[tb5]{0} &
              \eqmathbox[tb6]{h}
          \end{array}
        }^{R}
      }
    } \\
    b_{1,2} & a_2 & \eqmathbox[tb2]{b_{2,s_2+1}} & h & \eqmathbox[tb4]{x} & \eqmathbox[tb5]{x} & \eqmathbox[tb6]{x} \\
    b_{2,s_2+4} & \eqmathbox[tb1]{b_{2,s_2+1}} & a_3 & y & y & y & y \\
    y & y & y & \eqmathbox[tb3]{a_{s_{r+1}}} & y & y & y \\
    y & y & y & y & y & y & y \\
    i & j & k & l & x & x & x \\
    m & \multicolumn{2}{@{} c @{}}{
      \smash{
        \underbrace{
          \begin{array}{c c}
            \eqmathbox[tb1]{n} & 
              \eqmathbox[tb2]{o}
          \end{array}
        }_{S}
      }
    } & \multicolumn{3}{@{} c @{}}{
      \smash{
        \underbrace{
          \begin{array}{c c c}
            \eqmathbox[tb3]{p} & 
              \eqmathbox[tb4]{x} & 
              \eqmathbox[tb5]{x}
          \end{array}
        }_{W}
      }
    } & x
  \end{bmatrix}
  \mkern-5mu
  \begin{array}{@{} l @{}}
    \left.\kern-\nulldelimiterspace\begin{array}{@{} c @{}}
      \mathstrut \\ \mathstrut \\ \mathstrut \\ \mathstrut
    \end{array}\right\}{\scriptstyle T} \\
    \left.\kern-\nulldelimiterspace\begin{array}{@{} c @{}}
      \mathstrut \\ \mathstrut \\ \mathstrut
    \end{array}\right\}{\scriptstyle U}
  \end{array}
\]
\lipsum*[2]

\end{document}
Werner
  • 603,163
  • It is perfect. thanks. How can I extend this to more than 6 columns inside the brace? – M a m a D Aug 18 '18 at 16:57
  • @Drupalist: I use \multicolumn{6} to make a cell that spans 6 columns. Inside that cell, I use an array with 6 columns (*{5}{c} c) and place every item inside an \eqmathbox[<tag>] with a unique <tag>. The idea is to capture the widest item in the entire matrix on a per-column basis for the span of the array. That way the column spacing will match within the bmatrix and the individual cell arrays. Some fiddling with column separation (the @{}) was necessary, since bmatrix removes that from the left-/right-most columns. – Werner Aug 19 '18 at 15:03