매트릭스에 여러 블록을 표시하는 TikZ

매트릭스에 여러 블록을 표시하는 TikZ

매트릭스에 몇 개의 블록을 표시하고 싶고 다른 유사한 질문의 지시를 따랐지만 이상한 TikZ 동작이 나타납니다.. 매트릭스에 표시된 블록을 두 개 이상 가질 수 없는 것 같나요? 내가 보여주고 싶은 것은 일부 열에서 시작하여 대각선 아래의 0이 아닌 요소를 소멸시키는 차단된 기븐스 회전의 스텐실입니다. 여기서 대각선 위의 블록(빨간색)을 업데이트한 다음 후행 열이나 행에서 작업할 수 있습니다. 오른쪽 블록(녹색).

업데이트: 답변을 입력으로 사용하여 최종 사용 사례를 구현했습니다.

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

여기에 이미지 설명을 입력하세요

관련 정보