TikZ 在矩阵中标记几个块

TikZ 在矩阵中标记几个块

我想在矩阵中标记几个块,我按照其他类似问题的指示操作,但 TikZ 的行为却很奇怪……似乎矩阵中不能有多个标记块?我想展示的是阻断 Givens 旋转的模板,用于从某列开始消除对角线下的非零元素,我更新对角线上的块(红色),然后可以处理右侧的尾随列或行块(绿色)。

更新:使用答案作为输入我实现了我的最终用例。

\usepackage{pgf}
\usepackage{tikz}
\usetikzlibrary{calc,fit,matrix,arrows,automata,positioning}

\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newcommand{\tikzdrawbox}[3][]{%
    \tikz[overlay,remember picture]{
    \draw[#3,#1]
      ($(left#2)+(-0.2em,0.9em)$) rectangle
      ($(right#2)+(0.2em,-0.3em)$);}
}

\newcommand\x{\times}

\begin{equation}\label{eq:blockedgivens}
\newcommand\y{\colorbox{myred}{$\times$}}
\newcommand\z{\colorbox{mygray}{$\times$}}
  \left(\begin{array}{ccccccccccccccc}
    \x & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x \\
     0 & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x \\
     0 &  0 & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x \\
     0 &  0 &  0 & \tikzmark{left1}\x & \x & \x & \x & \tikzmark{left3}\x & \x & \x & \x & \x & \x & \x & \x \\
     0 &  0 &  0 & \z & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x \\
     0 &  0 &  0 &  0 & \z & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x \\
     0 &  0 &  0 &  0 &  0 & \z & \x & \x & \x & \x & \x & \x & \x & \x & \x \\
     0 &  0 &  0 &  0 &  0 &  0 & \z & \tikzmark{left2} \x \tikzmark{right1} & \x & \x & \x & \tikzmark{left4} \x & \x & \x & \x \tikzmark{right3}\\
     0 &  0 &  0 &  0 &  0 &  0 &  0 & \z & \x & \x & \x & \x & \x & \x & \x \\
     0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 & \z & \x & \x & \x & \x & \x & \x \\
     0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 & \z & \x & \x & \x & \x & \x \\
     0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 & \z & \x \tikzmark{right2} & \x & \x & \x \tikzmark{right4} \\
     0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 & \z & \x & \x & \x \\
     0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 & \z & \x & \x \\
     0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 & \z & \x \\
  \end{array}\right)
  \tikzdrawbox[thick]{1}{red}
  \tikzdrawbox[thick]{2}{red}
  \tikzdrawbox[thick]{3}{green}
  \tikzdrawbox[thick]{4}{green}
\end{equation}

在此处输入图片描述

答案1

每个块都需要自己的一对标记;我改变了定义\tikzdrawbox;第一个强制参数是用于标记的数字;第二个可选参数允许您传递选项,\draw可选参数允许您对块尺寸指定一些“校正”以防止某些边框重叠:

\documentclass{article}
\usepackage{pgf}
\usepackage{tikz}
\usetikzlibrary{calc,fit,matrix,arrows,automata,positioning}

\newcommand\x{\times}
\newcommand\y{\colorbox{red}{$\times$}}
\newcommand\z{\colorbox{gray}{$\times$}}


\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newcommand{\tikzdrawbox}[3][(0pt,0pt)]{%
    \tikz[overlay,remember picture]{
    \draw[#3]
      ($(left#2)+(-0.3em,0.9em) + #1$) rectangle
      ($(right#2)+(0.2em,-0.4em) - #1$);}
}

\begin{document}

\begin{equation}\label{eq:blockedgivens}
  \left(\begin{array}{ccccccccccccccc}
    \x & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x \\
     0 & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x \\
     0 &  0 & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x \\
     0 &  0 &  0 & \tikzmark{left1}\x & \x & \x & \x & \tikzmark{left3}\x & \x & \x & \x & \x & \x & \x & \x \\
     0 &  0 &  0 & \z & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x \\
     0 &  0 &  0 &  0 & \z & \x & \x & \x & \x & \x & \x & \x & \x & \x & \x \\
     0 &  0 &  0 &  0 &  0 & \z & \x & \x & \x & \x & \x & \x & \x & \x & \x \\
     0 &  0 &  0 &  0 &  0 &  0 & \z & \tikzmark{left2} \x \tikzmark{right1} & \x & \x & \x & \tikzmark{left4} \x & \x & \x & \x \tikzmark{right3} \\
     0 &  0 &  0 &  0 &  0 &  0 &  0 & \z & \x & \x & \x & \x & \x & \x & \x \\
     0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 & \z & \x & \x & \x & \x & \x & \x \\
     0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 & \z & \x & \x & \x & \x & \x \\
     0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 & \z & \x \tikzmark{right2} & \x & \x & \x \tikzmark{right4} \\
     0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 & \z & \x & \x & \x \\
     0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 & \z & \x & \x \\
     0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 &  0 & \z & \x \\
  \end{array}\right)
  \tikzdrawbox{1}{thick,red}
  \tikzdrawbox{2}{thick,red}
  \tikzdrawbox[(-1pt,2pt)]{3}{thick,green}
  \tikzdrawbox[(-4pt,2pt)]{4}{thick,green}
\end{equation}

\end{document}

在此处输入图片描述

答案2

如果您没有太多特殊条目(您可以包含更多 if 情况),您可以稍微缩短代码。

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{equation}\label{eq:blockedgivens}
\left( 
\begin{tikzpicture}[baseline=(current bounding box.center)] 
\begin{scope}[xscale=0.6,yscale=0.4]
\foreach \x in {1,...,14}
    \foreach \y in {1,...,14}
    {
    \ifnum\x<\y
    \node (my-\y-\x) at (\x,-\y) {$0$};
    \else
    \node (my-\y-\x) at (\x,-\y) {$\times$};
    \fi
    }
\end{scope}
\draw[red] (my-4-4.north west) rectangle (my-8-8.south east);
\draw[red] (my-8-8.north west) rectangle (my-12-12.south east);
\end{tikzpicture}
\right)
\end{equation}
\end{document}

在此处输入图片描述

相关内容