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).

