# Adding brackets above/below and left/right of array matrix

I'm trying to add brackets to portions of a matrix, like so:

where the arrowed bars are replaced with brackets, and the red bar would be moved to the bottom. If it helps, here's the portion I have now:

\left[
\begin{array}{c c c c|c c c}
1 & 0 & \cdots & 0 & 0 & \cdots & 0\\
0 & 1 & \ddots & \vdots & \vdots & \ddots & \vdots \\
\vdots & \ddots & \ddots & \vdots & \vdots & \ddots & \vdots\\
0 & \cdots & 0 & 1 & 0 & \cdots & 0\\
\hline
0 & \cdots & \cdots & 0 & 0 & \cdots & 0\\
\vdots & \ddots & \ddots & \vdots & \vdots & \ddots & \vdots  \\
0 & \cdots & \cdots & 0 & 0 &\cdots & 0
\end{array}
\right]
\begin{matrix*}[l]
\left. \vphantom{\begin{pmatrix}
0 \\
\vdots \\
\vdots\\
0\\
0\\
\vdots\\
0
\end{pmatrix} } \right\} \text{Rank } C_{p-1} = s_{p-1}\\
\end{matrix*}


Where I have the right-side bracket working, but I'm unsure how to get the others in place... I tried adding this before my matrix:

\begin{matrix*}[r]
b_{p-1} = \text{Rank } B_{p-1}\left\{\vphantom{\begin{pmatrix}{c}
1\\
0\\
\vdots\\
0\\
\hline
\end{pmatrix}}\right.
\left.
\vphantom{\begin{pmatrix}{c}
0\\
\vdots\\
0
\end{pmatrix}}\right.
\end{matrix*}


but that places a bracket in the middle of my matrix, instead of portion of diagonal 1's. Thanks!!

• Welcome to TeX.SX! I guess that TikZ would be your friend. – egreg Mar 20 '17 at 22:02
• @egreg Thanks! I was hoping to avoid TikZ, but I'm starting to think this might be my only option... Would you happen to know where I should start looking? – Brad Flynn Mar 20 '17 at 22:05
• I'm sure our TikZ experts will soon come to the rescue. – egreg Mar 20 '17 at 22:08

This is a possible TikZ solution

\documentclass[tikz,border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{mathtools}
\DeclareMathOperator{\rk}{rank}

\begin{document}

\begin{tikzpicture}
\matrix (A) [
matrix of math nodes]{
1 & 0 & \dots & 0 & 0 & \dots & 0\\
0 & 1 & \ddots & \vdots & \vdots & \ddots & \vdots \\
\vdots & \ddots &\ddots & \vdots & \vdots & \ddots & \vdots \\
0 & \dots & 0 & 1 & 0 & \dots & 0\\
0 & \dots & \dots & 0 & 0 & \dots & 0\\
\vdots & \ddots & \ddots & \vdots & \vdots & \ddots & \vdots \\
0 & \dots & \dots & 0& 0 & \dots & 0\\
};

\draw[gray] (A-1-1.north west) rectangle (A-4-4.south east);
\draw[gray] (A-4-4.south east) rectangle (A-7-7.south east);
\draw[shorten >=2pt, shorten <=2pt] (A-1-1.north west) --++(180:3mm)|-(A-4-1.south west) node[pos=.25, rotate=90, above]{$b_{p-1}=\rk B_{p-1}$};
\draw[shorten >=2pt, shorten <=2pt] (A-1-1.north west) --++(90:3mm)-|(A-1-7.north east) node[pos=.25, above]{$s_p=\rk C_p$};
\draw[shorten >=2pt, shorten <=2pt] (A-7-4.south west) --++(270:3mm)-|(A-7-7.south east) node[pos=.25, below]{$\rk Z_p=z_p$};
\draw[shorten >=2pt, shorten <=2pt] (A-1-7.north east) --++(0:3mm)|-(A-7-7.south east) node[pos=.25, rotate=270, above]{$\rk C_{p-1}=s_{p-1}$};
\end{tikzpicture}
\end{document}


• how would I go about editing the vertical alignment of this inside of an equation? Like if I were to have \text{SNF}([\partial_p]) = \begin{tikzpic...? – Brad Flynn Mar 21 '17 at 15:02
• @BradFlynn Try with \begin{tikzpicture}[baseline] and see what happens. If the default baseline for tikzpicture doesn't fit your needs, you can use any anchor or page reference as baseline. It's explained in page 124 in pgfmanual. – Ignasi Mar 21 '17 at 15:17
• That worked perfectly! Thanks again for your help! One more thing, if I were wanting to learn more about tikz, would you happen to know of a good place to start? Seems pretty useful, would love to know more! – Brad Flynn Mar 21 '17 at 15:32
• And, not to be nit-picky, but is there any way to shorten the bottom bar? seems like it extends past the column that I'd like. (EDIT never mind, I just edited the drawn location!) – Brad Flynn Mar 21 '17 at 15:34
• @BradFlynn To start learning TikZ my best suggestion is reading the tutorials at beginning of pgfmanual. Later on you can find more information in tex.stackexchange.com/a/9123/1952 – Ignasi Mar 21 '17 at 15:42

Here is a solution with pstricks: it consists in adding nodes at relevant places in the matrix, and joining them with node connections:

\documentclass[x11names, svgnames]{article}
\usepackage{mathtools}
\usepackage{pst-node}
\usepackage{auto-pst-pdf}
\DeclareMathOperator{\rk}{rank}
\pagestyle{empty}

\begin{document}

\begin{equation*}
\begin{postscript}\everypsbox{\footnotesize}
\begin{bmatrix*}%{c c c c|c c c}
\pnode[-0.3ex, 2.4ex]{r1}\Rnode{R1}{1}\rule{0pt}{3ex} & 0 & \cdots & \Rnode{S1}{0} & 0 & \cdots & \Rnode{T1}{0}\\
0 & 1 & \ddots & \vdots & \vdots & \ddots & \vdots \\
\vdots & \ddots & \ddots & \vdots & \vdots & \ddots & \vdots \\
\Rnode{R2}{0} & \cdots & 0 & \Rnode{S2}{1}\pnode[1.25ex, -0.8ex]{s2} & 0 & \cdots & 0\\
0 & \cdots & \cdots & 0 & 0 & \cdots & 0\\
\vdots & \ddots & \ddots & \vdots & \vdots & \ddots & \vdots \\
0 & \cdots & \cdots &0 & \Rnode{R3}{0} & \cdots & \Rnode{S3}{0}\pnode[0.2ex, -0.3ex]{s3}
\end{bmatrix*}
\psset{ linejoin=1, linearc=0.08, arm=1ex, nodesep=1.5ex}
\ncbar[angle=90]{R1}{T1}\naput{$s_p = \rk C_p$}
\ncbar{T1}{S3}\naput[nrot=:U]{$\rk C_{p-1} = s_{p-1}$}
\ncbar[angle=180]{R2}{R1}\naput[nrot=:U]{$b_{p-1} =\rk B_{p-1}$}
\ncbar[angle=-90]{R3}{S3}\nbput{$\rk Z_p = z_p$}
\psset{linecolor=LightSteelBlue3!50}
\psframe(r1)(s2)
\psframe[dimen=inner](s2)(s3)
\end{postscript}%
\end{equation*}

\end{document}