自訂矩陣中繪圖框的大小

自訂矩陣中繪圖框的大小
\documentclass{beamer}
\usetheme{Madrid}
\usepackage{amsmath,tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{frame}{Matrix}

This is an example of a matrix.

\begin{tikzpicture}
\matrix [matrix of math nodes,left delimiter=(,right delimiter=),ampersand replacement=\&] (m)
{
  8 \& 8 \& 1 \& 6 \\
  3 \& 8 \& 5 \& 7 \\
  4 \& 8 \& 9 \& 5 \\
};
\draw[color=red, thick] (m-1-1.north west) -- (m-1-3.north east) -- (m-2-3.south east) -- (m-2-1.south west) -- (m-1-1.north west);
\draw[color=blue, thick] (m-2-1.north west) -- (m-2-3.north east) -- (m-3-3.south east) -- (m-3-1.south west) -- (m-2-1.north west);
\end{tikzpicture}
\end{frame}
\end{document}

我在矩陣中畫了兩個盒子(在 Beamer 中)。它可以工作,只是兩個盒子部分重疊,因此藍色盒子部分覆蓋在紅色盒子上,這不是很好。

有沒有辦法讓藍色框更大或更小,這樣它們就不會重疊? (也歡迎其他避免重疊的方法。)

答案1

假設您期望這樣的事情:

在此輸入影像描述

\documentclass{beamer}
\usetheme{Madrid}
\usepackage{amsmath,tikz}
\usetikzlibrary{fit,matrix}
\tikzset{
mN/.style = {%myNode
    draw=#1, thick, inner sep=0pt}
             }

\begin{document}
\begin{frame}{Matrix}

This is an example of a matrix.

    \begin{tikzpicture}
\matrix [matrix of math nodes, column sep=1pt,
         left delimiter=(,right delimiter=),ampersand replacement=\&] (m)
{
  8 \& 8 \& 1 \& 6 \\
  3 \& 8 \& 5 \& 7 \\
  4 \& 8 \& 9 \& 5 \\
};
\node[mN=red, xshift= 2pt, fit=(m-1-1) (m-2-3)] {};
\node[mN=blue,xshift=-2pt, fit=(m-2-1) (m-3-3)] {};
\end{tikzpicture}
\end{frame}
    \end{document}

編輯:我稍微優化了程式碼。

相關內容