6

Possible Duplicate:
Highlight elements in the matrix

\documentclass[12pt,a4paper]{mwrep}
\usepackage{amsmath}
\usepackage[nodisplayskipstretch]{setspace} \setstretch{1.5}
\begin{document}
\begin{equation}
P=
\begin{bmatrix} 0.3 & 0.1 & 0.6 & 0.0 & 0.0 & 0.0 & 0.0 \\
                0.2 & 0.1 & 0.7 & 0.7 & 0.0 & 0.0 & 0.0 \\
                0.3 & 0.4 & 0.3 & 0.0 & 0.0 & 0.0 & 0.0 \\
                0.0 & 0.0 & 0.0 & 0.2 & 0.8 & 0.0 & 0.0 \\
                0.0 & 0.0 & 0.0 & 0.3 & 0.7 & 0.0 & 0.0 \\
                0.0 & 0.4 & 0.1 & 0.0 & 0.0 & 0.2 & 0.3 \\
                0.0 & 0.0 & 0.1 & 0.0 & 0.3 & 0.1 & 0.5
\end{bmatrix}
\end{equation}
\end{document}

This produces nice looking matrix. I need two enhancements:
- A box around some elements in matrix with caption
- Labels above the matrix and on the left side (in my example 1, 2, 4, 3, 6, 5, 7).

My idea is to show and name 'submatrices' in my matrix. If there is better looking way to show what I want, you can change my design.

Pictute showing what I want to acomplish:

Edit:
There is a problem with \usepackage[nodisplayskipstretch]{setspace} \setstretch{1.5}. Vertical space in matrix is too big when I use it with code from accepted solution.

Ichibann
  • 8,381

1 Answers1

5

Below I have used bmatrix as per your MWE, and \brodermatrix from Where is the \matrix command?, along my earlier solution from Highlight elements in the matrix to yield the desired results.

Since you are using \setstretch{1.5} in your preamble, and this results in too much spacing in the matrices you can adjust that locally within the matrix, as shown below. Since this adjustment is done within a group, there should be no effect on this setting for the rest of your code.

enter image description here

Notes:

  • This does require two runs: the first to compute the positions of the box, and the second to draw it in the correct spot.
  • Since this is using tikz, you automatically get all the flexibility inherent in tikz, such as line styles, line thickness, line color, fill, etc, a few of which are illustrated in the output. These can be passed to the first parameter to the \DrawBox macro to customize each instance or provided as default options to maintain consistency.

References:

Code:

\documentclass[12pt,a4paper]{mwrep}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc}

\usepackage[nodisplayskipstretch]{setspace} \setstretch{1.5}

\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};} \newcommand{\DrawBox}[4][]{% \tikz[overlay,remember picture]{% \coordinate (TopLeft) at ($(#2)+(-0.2em,0.9em)$); \coordinate (BottomRight) at ($(#3)+(0.2em,-0.3em)$); % \path (TopLeft); \pgfgetlastxy{\XCoord}{\IgnoreCoord}; \path (BottomRight); \pgfgetlastxy{\IgnoreCoord}{\YCoord}; \coordinate (LabelPoint) at ($(\XCoord,\YCoord)!0.5!(BottomRight)$); % \draw [red,#1] (TopLeft) rectangle (BottomRight); \node [below, #1, fill=none, fill opacity=1] at (LabelPoint) {#4}; } }

\begin{document} \begin{equation}\setstretch{1.25} P= \begin{bmatrix} \tikzmark{left1}0.3 & 0.1 & 0.6 & 0.0 & 0.0 & 0.0 & 0.0 \ 0.2 & 0.1 & 0.7 & 0.7 & 0.0 & 0.0 & 0.0 \ 0.3 & 0.4 & 0.3\tikzmark{right1} & 0.0 & 0.0 & 0.0 & 0.0 \[12pt] 0.0 & 0.0 & 0.0 & \tikzmark{left2}0.2 & 0.8 & 0.0 & 0.0 \ 0.0 & 0.0 & 0.0 & 0.3 & 0.7\tikzmark{right2} & 0.0 & 0.0 \[12pt] 0.0 & 0.4 & 0.1 & 0.0 & 0.0 & 0.2 & 0.3 \ 0.0 & 0.0 & 0.1 & 0.0 & 0.3 & 0.1 & 0.5 \end{bmatrix} \end{equation} \DrawBox[thick, red ]{left1}{right1}{\textcolor{red}{\footnotesize$s^1$}} \DrawBox[thick, blue, dashed]{left2}{right2}{\textcolor{blue}{\footnotesize$s^2$}} %------------- [\setstretch{1.2} \bordermatrix{\text{corner} & c_1 & c_2 & c_3 & c_4 & c_5 & c_6 & c_7\cr 1&\tikzmark{left1} 0.3 & 0.1 & 0.6 & 0.0 & 0.0 & 0.0 & 0.0\cr 2& 0.2 & 0.1 & 0.7 & 0.7 & 0.0 & 0.0 & 0.0\cr 4& 0.3 & 0.4 & 0.3\tikzmark{right1} & 0.0 & 0.0 & 0.0 & 0.0\cr\cr 3& 0.0 & 0.0 & 0.0 & \tikzmark{left2}0.2 & 9.8 & 0.0 & 0.0\cr 6& 0.0 & 0.0 & 0.0 & 0.3 & 0.7\tikzmark{right2} & 0.0 & 0.0\cr\cr 5& 0.0 & 0.4 & 0.1 & 0.0 & 0.3 & 0.2 & 0.3\cr 7& 0.0 & 0.0 & 0.1 & 0.0 & 0.3 & 0.1 & 0.5 } ] \DrawBox[thick, red, dotted ]{left1}{right1}{\textcolor{red}{\footnotesize$s^1$}} \DrawBox[thick, blue,fill=yellow!10, fill opacity=0.3]{left2}{right2}{\textcolor{blue}{\footnotesize$s^2$}} \end{document}

Peter Grill
  • 223,288
  • One more thing. I use \usepackage[nodisplayskipstretch]{setspace} \setstretch{1.5}. This makes vertical space too big. How can I fix that? – Ichibann Mar 12 '12 at 19:57
  • 1
    @Ichibann: This is an easy fix. Have updated the solution to reflect this and also included your MWE. – Peter Grill Mar 13 '12 at 00:22