# How can I add clips above matrix?

\documentclass{mwrep}
\usepackage{kbordermatrix}
\usepackage{amsmath}
\begin{document}
$$\bar{P}= \kbordermatrix{ \mathrm{state} & 1 & 2 & 4 & 3 & 6 & 5 & 7 \\ 1 & 0.3 & 0.1 & 0.6 & 0.0 & 0.0 & 0.0 & 0.0 \\ 2 & 0.2 & 0.1 & 0.7 & 0.0 & 0.0 & 0.0 & 0.0 \\ 4 & 0.3 & 0.4 & 0.3 & 0.0 & 0.0 & 0.0 & 0.0 \\ 3 & 0.0 & 0.0 & 0.0 & 0.2 & 0.8 & 0.0 & 0.0 \\ 6 & 0.0 & 0.0 & 0.0 & 0.3 & 0.7 & 0.0 & 0.0 \\ 5 & 0.0 & 0.4 & 0.1 & 0.0 & 0.0 & 0.2 & 0.3 \\ 7 & 0.0 & 0.0 & 0.1 & 0.0 & 0.3 & 0.1 & 0.5}$$
\end{document}


This generates a nice matrix:

How can I add clips above column descriptions as shown in image below?

-

If you are ready to use TikZ, you can take a look at this question two level matrix

or you can look at the following code:

\documentclass{mwrep}

\usepackage{amsmath}

\usepackage{tikz}
\newcommand{\tikzmark}[2]{\tikz[overlay,remember picture,baseline] \node [anchor=base] (#1) {$#2$};}
\usetikzlibrary{decorations.pathreplacing}

\begin{document}

$$\bar{P}= \bordermatrix{ \mathrm{state} & \tikzmark{left}{1} & \tikzmark{right}{2} \cr 1 & 0.3 & 0.1 \cr 2 & 0.3 & 0.2}$$
\tikz[overlay,remember picture]{\draw[decoration=brace,decorate,blue] (left.north west) -- (right.north east) node[midway,above=3pt] {$s_1$};}
\end{document}


The result is

-
I like this solution as much as David's one, but try to compile it with text above equation. – Ichibann Mar 15 '12 at 22:56

\documentclass{mwrep}
\usepackage{kbordermatrix}
\usepackage{amsmath}
\makeatletter
\def\hmmA{\noalign{\global\let\@kbrowstyle\scriptstyle}}
\def\hmmB{\noalign{\kern.2ex\global\let\@kbrowstyle\relax}}
\setlength{\kbrowsep}{20pt}
\makeatother
\begin{document}

\bar{P}= \kbordermatrix{ &\multispan3\hfill\scriptstyle S_1\hfill& \multispan2\hfill\scriptstyle S_2\hfill& & \\ \noalign{\kern-\kbrowsep\kern-.2ex} &\multispan3\quad\downbracefill\quad& \multispan2\quad\downbracefill\quad& & \\ \hmmA \mathrm{state} & 1 & 2 & 4 & 3 & 6 & 5 & 7 \\ \hmmB 1 & 0.3 & 0.1 & 0.6 & 0.0 & 0.0 & 0.0 & 0.0 \\ 2 & 0.2 & 0.1 & 0.7 & 0.0 & 0.0 & 0.0 & 0.0 \\ 4 & 0.3 & 0.4 & 0.3 & 0.0 & 0.0 & 0.0 & 0.0 \\ 3 & 0.0 & 0.0 & 0.0 & 0.2 & 0.8 & 0.0 & 0.0 \\ 6 & 0.0 & 0.0 & 0.0 & 0.3 & 0.7 & 0.0 & 0.0 \\ 5 & 0.0 & 0.4 & 0.1 & 0.0 & 0.0 & 0.2 & 0.3 \\ 7 & 0.0 & 0.0 & 0.1 & 0.0 & 0.3 & 0.1 & 0.5}
\end{document}


As noted in the comments the space adjustments used here don't automatically adjust to changes in baseline stretch. The following modification addresses that, the two additional omit in the rows adding the over-braces, stop LaTeX spacing these lines to the baseline spacing, and as that is then a little too tight, a small vertical kern is added (in place of the negative kern in the version above)

\kbordermatrix{
\omit  &\multispan3\hfill$\scriptstyle S_1$\hfill&
\multispan2\hfill$\scriptstyle S_2$\hfill&
& \\
\noalign{\kern-\kbrowsep\kern1ex}

yes sure: the space between the brace and the column label is governed by the 20pt and the space between the brace and the S_1 label is governed by the \kern-.2ex fiddle with those numbers to suit your requirements. – David Carlisle Mar 15 '12 at 10:37
This doesn't look good with my \usepackage[nodisplayskipstretch]{setspace} \setstretch{1.5} – Ichibann Mar 15 '12 at 23:14