Answering How to highlight portions of a matrix is a nice technique that isn't invasive, but requires manually numbering the nodes. I'd generalized it slightly:
\documentclass{article}
\usepackage[svgnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{calc}
%----------------------------------------------------------------------------------------
%
% https://tex.stackexchange.com/questions/40028/highlight-elements-in-the-matrix/40059#40059
%
% The following two macros allow marking left/upper, right/lower node positions for box drawing, and then drawing the box.
% unique node numbers must be passed to these.
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newcommand{\DrawNodeBox}[3][]{%
\tikz[overlay,remember picture]{%
\draw[DarkOliveGreen,#1]%
($(#2)+(-0.4em,0.9em)$) rectangle%
($(#3)+(0.5em,-0.3em)$);}%
}
% here's some logic to allow for three boxes to be drawn with preceding marks, with counter increments after
% the boxes are drawn.
\newcounter{FirstBoxCounter}
\newcounter{SecondBoxCounter}
\newcounter{ThirdBoxCounter}
\newcommand{\tikzLeftMark}[0]{\tikzmark{tbFirstLeft\theFirstBoxCounter}}
\newcommand{\tikzLeftMarkSecond}[0]{\tikzmark{tbSecondLeft\theSecondBoxCounter}}
\newcommand{\tikzLeftMarkThird}[0]{\tikzmark{tbThirdLeft\theThirdBoxCounter}}
\newcommand{\tikzRightMark}[0]{\tikzmark{tbFirstRight\theFirstBoxCounter}}
\newcommand{\tikzRightMarkSecond}[0]{\tikzmark{tbSecondRight\theSecondBoxCounter}}
\newcommand{\tikzRightMarkThird}[0]{\tikzmark{tbThirdRight\theThirdBoxCounter}}
\newcommand{\DrawFirstBox}[0]{%
\DrawNodeBox[thick]{tbFirstLeft\theFirstBoxCounter}{tbFirstRight\theFirstBoxCounter}%
\stepcounter{FirstBoxCounter}%
}
\newcommand{\DrawSecondBox}[0]{%
\DrawNodeBox[thick]{tbSecondLeft\theSecondBoxCounter}{tbSecondRight\theSecondBoxCounter}%
\stepcounter{SecondBoxCounter}%
}
\newcommand{\DrawThirdBox}[0]{%
\DrawNodeBox[thick]{tbThirdLeft\theThirdBoxCounter}{tbThirdRight\theThirdBoxCounter}%
\stepcounter{ThirdBoxCounter}%
}
%------------------------------------------------------------------------------------------------------
\begin{document}
\[
M = \left[\begin{array}{*{13}{c}}
\tikzLeftMarkSecond{}0 & 1 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 1 & 0 & 0 & 0 \\
0 & 1 & 0\tikzRightMarkSecond{} & 1 & 0 & 0 & 0 & 1 & 1 & 1 & 1 & 0 & 1 \\
0 & 0 & 1 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\
\tikzLeftMark{}1 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\
0 & 1 \tikzRightMark{} & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 & 1 \\
0 & 0 & 0 & 0 & 1 & 0 & \tikzLeftMarkThird{}0 & 0 & 0 & 0 & 0 & 1 & 0 \\
0 & 1 & 1 & 0 & 1 & 0 & 0 & 1 & 1 & 1 & 0 & 0 & 0 \\
0 & 0 & 1 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0\tikzRightMarkThird{} & 0 & 1 \\
\end{array}\right]
\]
\DrawFirstBox{}
\DrawSecondBox{}
\DrawThirdBox{}
\[
M = \left[\begin{array}{*{13}{c}}
0 & 0 & 0 & \tikzLeftMarkSecond{}-1 & 1 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 1 \\
0 & 0 & 0 & 0 & 1 & 0\tikzRightMarkSecond{} & 1 & 0 & 0 & 0 & 1 & 1 & 1 \\
0 & 0 & 1 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\
\tikzLeftMark{}-1 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\
0 & 0 & 1 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\
0 & 0 & 1 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\
0 \tikzRightMark{} & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 & 1 \\
0 & 0 & 0 & 0 & 1 & 0 & \tikzLeftMarkThird{}0 & 0 & 0 & 0 & 0 & 1 & 0 \\
0 & 0 & 1 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0\tikzRightMarkThird{} & 0 & 1 \\
0 & 0 & 1 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 1 & 0 & 0
\end{array}\right]
\]
\DrawFirstBox{}
\DrawSecondBox{}
\DrawThirdBox{}
\end{document}
This allows me to draw up to three boxes at a time (in matrices, or presumably in general equations), each time the Draw is executed the counters for the markers are incremented, and works nicely:

(there are issues with the edge detection in Peter's method and I had to hack the hardcoded numbers slightly, which generates better but not perfect box boundaries)
Is there a way to have an array of counters instead of the set of three manually named counters that I have used? I'd like to be able to use the DrawNodeBox with a numeric index for a node counter array, instead of having wrapper methods for this function that explicitly pass the node names (that way I can use the optional parameters of DrawNodeBox directly).
i.e. I'd like an interface that could be used something like this:
\[
M = \left[\begin{array}{*{13}{c}}
\tikzmark{left}{1}0 & 1 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 1 & 0 & 0 & 0 \\
0 & 1 & 0\tikzmark{right}{1} & 1 & 0 & 0 & 0 & 1 & 1 & 1 & 1 & 0 & 1 \\
0 & 0 & 1 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\
\tikzmark{left}{2}1 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\
0 & 1 \tikzmark{right}{2} & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 & 1 \\
0 & 0 & 0 & 0 & 1 & 0 & \tikzmark{left}{3}0 & 0 & 0 & 0 & 0 & 1 & 0 \\
0 & 1 & 1 & 0 & 1 & 0 & 0 & 1 & 1 & 1 & 0 & 0 & 0 \\
0 & 0 & 1 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0\tikzmark{right}{3} & 0 & 1 \\
\end{array}\right]
\]
\DrawNodeBox[thick]{1}
\DrawNodeBox[thick]{2}
\DrawNodeBox[thick]{3}
\[
M = \left[\begin{array}{*{13}{c}}
0 & 0 & 0 & \tikzmark{left}{2}-1 & 1 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 1 \\
0 & 0 & 0 & 0 & 1 & 0\tikzmark{right}{2} & 1 & 0 & 0 & 0 & 1 & 1 & 1 \\
0 & 0 & 1 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\
\tikzmark{left}{3}-1 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\
0 & 0 & 1 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\
0 & 0 & 1 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\
0 \tikzmark{right}{3} & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 & 1 \\
0 & 0 & 0 & 0 & 1 & 0 & \tikzmark{left}{1}0 & 0 & 0 & 0 & 0 & 1 & 0 \\
0 & 0 & 1 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0\tikzmark{right}{1} & 0 & 1 \\
0 & 0 & 1 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 1 & 0 & 0
\end{array}\right]
\]
\DrawNodeBox[thick]{1}
\DrawNodeBox[thick]{2}
\DrawNodeBox[thick]{3}
where \DrawNodeBox would increment an indexed counter for the node number as well as drawing the box, and \tikzmark would use the same indexed counter, using that counter to generate left and right node numbers automatically.


\mytikzmarkrather thantikzmarksince then you could just use a wrapper rather than needing to patch or redefine the original macro. – cfr Nov 19 '14 at 15:58