Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.
\documentclass{mwrep}
\usepackage{kbordermatrix}
\usepackage{amsmath}
\begin{document}
\begin{equation}
\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{equation}
\end{document}

This generates a nice matrix:

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

share|improve this question
add comment

2 Answers

up vote 12 down vote accepted

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}

\begin{equation}
\bar{P}=
\bordermatrix{ \mathrm{state} &   \tikzmark{left}{1} &   \tikzmark{right}{2}  \cr
                             1 & 0.3 & 0.1  \cr
                             2 & 0.3 & 0.2}
\end{equation}
\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

enter image description here

share|improve this answer
    
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
add comment

enter image description here

\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}



\begin{equation}
\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{equation}
\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}
\omit  &\multispan3\quad\downbracefill\quad&
   \multispan2 \quad\downbracefill\quad&  & \\
\hmmA
share|improve this answer
    
Thanks! Can a clip be set a little lower? –  Ichibann Mar 15 '12 at 10:03
    
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
    
Can you add a little explanation how this works? –  Ichibann Mar 15 '12 at 22:58
    
This doesn't look good with my \usepackage[nodisplayskipstretch]{setspace} \setstretch{1.5} –  Ichibann Mar 15 '12 at 23:14
    
I tried adding that and it made no difference as far as I can see? (Although I see you've accepted the tikz answer now, but I'm interested as to what went wrong here?) –  David Carlisle Mar 16 '12 at 9:15
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.