# How can I add size of Tikz drawing into size of equation? (PART 2)

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}{\tikz[overlay,remember picture,baseline] \node [anchor=base] (#1) {};}
\newcommand{\tzend}{\tikz[remember picture,baseline=0pt] \path (ClipLabel.north);}

\newcommand{\DrawBox}[]{%
\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}[]{%
\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).

Peter would probably fix it so here is a continuation of the alternative with correct bounding box computations and without kbordermatrix.

\documentclass{mwrep}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{matrix,decorations.pathreplacing,backgrounds,positioning}
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Proin nibh augue,
suscipit a, scelerisque sed, lacinia in, mi.
\begin{equation}
\bar{P}=
\begin{tikzpicture}[baseline = (M.center),% center with respect to the matrix center
every left delimiter/.style={xshift=1ex},%tighter delimiter spacing
every right delimiter/.style={xshift=-1ex}]
\matrix (M) [matrix of math nodes,left delimiter={[},right delimiter={]}
]{
0.3 & 0.1 & 0.6 & 0.0 & 0.0 & 0.0 & 0.0 \\                                  0.2 & 0.1 & 0.7 & 0.0 & 0.0 & 0.0 & 0.0 \\                                  0.3 & 0.4 & 0.3 & 0.0 & 0.0 & 0.0 & 0.0 \\[1.2em]
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 \\[1.2em]
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 \\    };
\node[anchor=south east] (cornernode) at (M-1-1.north west) {state}; %Position this more
%precisely if desired
\foreach[count=\xi] \x in {1,2,4,3,6,5,7}{ %\xi is the counter \x is the value
\node (M-\xi-0) at (cornernode |- M-\xi-1) {\x}; %Gets the left most column
\node (M-0-\xi) at (cornernode -| M-1-\xi) {\x}; % Gets the top row
}
\node[below = 0.1pt of M-3-2.south] {$s^1$};
\node[below left = 0.1pt of M-5-5.south] {$s^2$};
\node[below left = 0.1pt of M-7-7.south] {$s^3$};

\draw[decoration=brace,decorate,blue] (M-0-1.north west) -- (M-0-3.north east)%
node[midway,above] {$S_1$};
\draw[decoration=brace,decorate,blue] (M-0-4.north west) -- (M-0-5.north east)%
node[midway,above] {$S_2$};

\begin{scope}[on background layer]
\draw [fill=black!20] (M-1-1.north west) rectangle (M-3-3.south east);
\draw [fill=blue!20,draw=blue,dashed] (M-4-4.north west) rectangle (M-5-5.south east);
\draw [fill=red!20,draw=red,rounded corners] (M-6-6.north west) rectangle (M-7-7.south east);
\end{scope}
\end{tikzpicture}
\end{equation}
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Proin nibh augue,
suscipit a, scelerisque sed, lacinia in, mi.
\end{document} • \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 raise state a little? Your solution is great :) – Ichibann Mar 16 '12 at 21:25
• Try \node[below = 0.1pt of M-7-7.south west] {$s_3$}; for better centering :) – Ichibann Mar 16 '12 at 21:43
• @Ichibann I have to admit I am having some difficulty processing your ideas. :) First, you are right making the text S doesn'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, replace at (M-1-1.north west) with at ([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
• Despite those difficulties, you processed my idea very well ;). Thanks. – Ichibann Mar 17 '12 at 9:17