Related to:
- How can I add size of Tikz drawing into size of equation?
Code presented below is the result of combining code from solutions to:
- How can I add clips above matrix?
- How can I add some enhancement to the matrix
\documentclass{mwrep}
\usepackage{amsmath}
\usepackage{kbordermatrix}
\usepackage[nodisplayskipstretch]{setspace} \setstretch{1.5}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{decorations.pathreplacing}
\newcommand{\tzm}[1]{\tikz[overlay,remember picture,baseline] \node [anchor=base] (#1) {};}
\newcommand{\tzend}{\tikz[remember picture,baseline=0pt] \path (ClipLabel.north);}
\newcommand{\DrawBox}[4][]{%
\tikz[overlay,remember picture, rounded corners]{%
\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.4ex, #1, fill=none, fill opacity=1] at (LabelPoint) {\footnotesize $#4$};
}
}
\newcommand{\DrawClip}[4][]{%
\tikz[overlay,remember picture]{%
\coordinate (Left) at ($(#2)+(-0.1em,0.65em)$);
\coordinate (Right) at ($(#3)+(+0.1em,0.65em)$);
%
\path (Left); \pgfgetlastxy{\XCoord}{\IgnoreCoord};
\path (Right); \pgfgetlastxy{\IgnoreCoord}{\YCoord};
%
\draw[decoration=brace,decorate,black, #1] (Left.north west) -- (Right.north east)
node[midway,above=1pt, #1] (ClipLabel) {\footnotesize $#4$};
}
}
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Proin nibh augue, suscipit a, scelerisque sed, lacinia in, mi.
\begin{equation}
P=
\kbordermatrix{ \mathrm{state} & \tzm{L1} 1 & 2\tzm{R1} & \tzm{L2}3 & 4\tzm{R2} \\
1 & \tzm{TL1}0.5 & 0.5 & 0 & 0 \\
2 & 0.2 & 0.8\tzm{BR1}& 0 & 0 \\[0.9em]
3 & 0 & 0 & \tzm{TL2}0.1 & 0.9 \\
4 & 0 & 0 & 0.3 & 0.7\tzm{BR2}}
\DrawBox{TL1}{BR1}{P_1}
\DrawBox{TL2}{BR2}{P_2}
\DrawClip{L1}{R1}{S_1}
\DrawClip{L2}{R2}{S_2}
\tzend
\end{equation}
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Proin nibh augue, suscipit a, scelerisque sed, lacinia in, mi.
\end{document}
I set below=1.4ex in DrawBox on purpose so the effect is clear. Normally I use -0.1ex.
How can I extend size of the equation to the bottom? The problem is that P_2 appears on the text.
I know how to extend size it in case of clips but I don't know how to to it in case of submatrices highlight (case when highlight is at the very bottom of matrix).

\draw [fill=black!20] (M-6-1.north west) rectangle (M-7-5.south east);Add this line to the code. This doesn't look good. How can it be fixed? How can I raisestatea little? Your solution is great :) – Ichibann Mar 16 '12 at 21:25\node[below = 0.1pt of M-7-7.south west] {$s_3$};for better centering :) – Ichibann Mar 16 '12 at 21:43Sdoesn't look right so I think\node[below left = 1ex of M-7-7.south] {$s^3$};would make it more text aware. Second, the rectangle overlaps came to my mind but I didn't pay attention too much considering your previous examples. Just put[2 mm]next to any ampersand and it would simulate a column seperator. Third, replaceat (M-1-1.north west)withat ([yshift=1mm]M-1-1.north west)and adjust accordingly for the desired elevation. See manual p.251 (ver.2.10). – percusse Mar 16 '12 at 21:54