如何将 Tikz 绘图的尺寸添加到方程的尺寸中?(第 2 部分)

如何将 Tikz 绘图的尺寸添加到方程的尺寸中?(第 2 部分)

相关:
-我如何将 Tikz 绘图的尺寸添加到方程的尺寸中?
下面的代码是结合以下解决方案的代码的结果:
-如何在矩阵上方添加剪辑?
-我怎样才能给矩阵添加一些增强功能

\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}

我特意设置below=1.4exDrawBox,这样效果就很明显了。通常我使用-0.1ex

我怎样才能将方程的大小扩展到底部?问题是P_2文本中出现了这个问题。
我知道在剪辑的情况下如何扩展它的大小,但我不知道在子矩阵突出显示的情况下如何扩展它(突出显示位于矩阵最底部的情况)。

答案1

彼得可能会修复它,因此这里是替代方案的延续,具有正确的边界框计算,并且没有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}

在此处输入图片描述

相关内容