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.

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.

share|improve this question

marked as duplicate by Peter Grill, egreg, cmhughes, Mico, percusse Mar 16 '12 at 19:34

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
Duplicate: Box around sub matrix is covered in: Highlight elements in the matrix, and labels is covered in Where is the \matrix command?. –  Peter Grill Mar 12 '12 at 18:16
    
Thanks. tex.stackexchange.com/questions/40028/… does not cover labelling highlighted area. How can I do that? Yur answer from this question that uses \tikzmark is great! What if I want two or more boxes inside matrix? –  Ichibann Mar 12 '12 at 18:46
    
Please, don't close yet! Thanks to @Peter Grill, I know how to make labels on the sides of a matrix and how to make highlights inside a matrix, but I still don't know how to label those highlights. –  Ichibann Mar 12 '12 at 19:14
    
I have voted to close it since it seems that you have moved to a new question and fixed the issues with this one. –  percusse Mar 16 '12 at 19:34

1 Answer 1

up vote 5 down vote accepted

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}
share|improve this answer
    
Thats exactly what I needed. Thanks! –  Ichibann Mar 12 '12 at 19:39
    
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 0:22
    
Thanks! That helped a lot. –  Ichibann Mar 13 '12 at 0:54

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