[Tex/LaTex] TikZ marking several blocks in a matrix

matricestikz-pgf

I would like to mark a few blocks in a matrix and I followed directions from other similar questions but I get a weird TikZ behavior .. seems that I can not have more than one marked block in a matrix? What I would like to show is the stencil of a blocked Givens rotations to annihilate non-zero elements under the diagonal starting from some column, where I update a block (red color) over the diagonal and then can work on the trailing columns or row block on the right (green color).

UPDATE: Using the answer as input I implemented my final use case.

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

enter image description here

Best Answer

Each block need its own pair of marks; I changed the definition of \tikzdrawbox; the first mandatory argument is a number used for the marks; the second optional argument allows you to pass options to \draw and the optional argument allows you to specify some "correction" to the block dimensions to prevent some borders to overlap:

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

enter image description here