TikZ markiert mehrere Blöcke in einer Matrix

TikZ markiert mehrere Blöcke in einer Matrix

Ich möchte einige Blöcke in einer Matrix markieren und bin den Anweisungen aus anderen ähnlichen Fragen gefolgt, aber ich erhalte ein seltsames TikZ-Verhalten. Scheint, als ob ich nicht mehr als einen markierten Block in einer Matrix haben kann? Was ich zeigen möchte, ist die Schablone einer blockierten Givens-Rotation, um von Null verschiedene Elemente unter der Diagonale zu vernichten, beginnend bei einer Spalte, wobei ich einen Block (rote Farbe) über der Diagonale aktualisiere und dann an den nachfolgenden Spalten oder dem Zeilenblock auf der rechten Seite (grüne Farbe) arbeiten kann.

UPDATE: Mit der Antwort als Eingabe habe ich meinen endgültigen Anwendungsfall implementiert.

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

Bildbeschreibung hier eingeben

Antwort1

Jeder Block benötigt sein eigenes Markierungspaar. Ich habe die Definition von geändert \tikzdrawbox. Das erste obligatorische Argument ist eine für die Markierungen verwendete Zahl. Mit dem zweiten optionalen Argument können Sie Optionen übergeben \drawund mit dem optionalen Argument können Sie einige „Korrekturen“ an den Blockabmessungen angeben, um zu verhindern, dass sich einige Ränder überlappen:

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

Bildbeschreibung hier eingeben

Antwort2

Wenn Sie nicht zu viele spezielle Einträge haben (für die Sie weitere if-Fälle einbauen können), können Sie den Code etwas kürzen.

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

Bildbeschreibung hier eingeben

verwandte Informationen