11
\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?

Ichibann
  • 8,381

3 Answers3

13

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

Frédéric
  • 11,087
  • 3
  • 37
  • 43
13

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
David Carlisle
  • 757,742
  • 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 09:15
  • Your solution worked and is ok. With \usepackage[nodisplayskipstretch]{setspace} \setstretch{1.5} I experienced that space between clip and label is too big. I already use tikz and tikzmark to highlight submatrices so Frédéric's answer is better for me. I didn't include submatrices highlighting in this question to keep example minimal. – Ichibann Mar 16 '12 at 12:17
  • ah sorry I added the package but didn't add the stretch, will add a note in the answer with a code update. (It's perfectly OK for you to accept the other answer though:-) – David Carlisle Mar 16 '12 at 12:57
  • It is hard task to choose one answer out of two great ones ;) – Ichibann Mar 16 '12 at 15:15
2

With {bNiceMatrix} of nicematrix.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

[\bar{P}= \begin{bNiceMatrix}[first-col,first-row] \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 \CodeAfter \OverBrace[shorten,yshift=1mm]{0-1}{0-3}{S_1} \OverBrace[shorten,yshift=1mm]{0-4}{0-5}{S_2} \end{bNiceMatrix}]

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

F. Pantigny
  • 40,250