[Tex/LaTex] How to get the explanation borders with pmatrix

matrices

I have asked this question in chat. How can I add the explanation borders to the below matrix?

\begin{align*} 
C_{D_{1\succ 2}, \mathrm{increasing}}    &= \underbrace{\begin{pmatrix} 1   &   -1  &   1 & -1  &   1   &   -1 &    1 & -1  \\ \end{pmatrix}}_{1\times 8\text{ matrix}}  \\ 
A_{D_{1\succ 2}, \mathrm{increasing}}    &= 
\underbrace{\begin{pmatrix} 
1   &   0   &   1   &   0   &   1   &   0   &   1   &   0   \\ 
0   &   1   &   0   &   1   &   0   &   1   &   0   &   1   \\ 
1   &   1   &   0   &   0   &   1   &   1   &   0   &   0   \\ 
0   &   0   &   1   &   1   &   0   &   0   &   1   &   1   \\ 
1   &   1   &   1   &   1   &   0   &   0   &   0   &   0   \\ 
1   &   1   &   1   &   1   &   0   &   0   &   0   &   0   \\ 
0   &   0   &   0   &   0   &   1   &   1   &   1   &   1   \\ 
\end{pmatrix}}_{7\times 8 \text{ matrix}}. 
\end{align*}

I want this kind of $p_1, p_2, …$ borders to the matrix above. I have been able to add them with blockarray and block here but now I need to add them to the existing pmatrix data-structure. How can I get it with the pmatrix?

enter image description here

Best Answer

You can use the kbordermatrix package which is for some reason not CTAN but you can download it from http://www.hss.caltech.edu/~kcb/LaTeX.shtml and there is a short documentation there too. Just save the .sty file to the same folder with your actual document.

\documentclass{article}
\usepackage{mathtools,kbordematrix}


\begin{document}

% Change the default brackets to parentheses
\renewcommand{\kbldelim}{(}
\renewcommand{\kbrdelim}{)}
\[
\underbrace{\kbordermatrix{
    & t_1& t_2  &\cdots & t_n & \cdots & t_h &\cdots & t_{2^n}\\
p_1 &   1   &   0   &   1   &   0   &   1   &   0   &   1   &   0   \\ 
p_2 &   0   &   1   &   0   &   1   &   0   &   1   &   0   &   1   \\ 
p_3 &   1   &   1   &   0   &   0   &   1   &   1   &   0   &   0   \\ 
p_4 &   0   &   0   &   1   &   1   &   0   &   0   &   1   &   1   \\ \hline
p_5 &   1   &   1   &   1   &   1   &   0   &   0   &   0   &   0   \\ 
p_6 &   1   &   1   &   1   &   1   &   0   &   0   &   0   &   0   \\ 
p_7 &   0   &   0   &   0   &   0   &   1   &   1   &   1   &   1   \\ 
}}_{7\times 8 \text{ matrix}}
\]
\end{document}

enter image description here