# How to create elegant stripped regions in matrix along with numbering of the rows and columns

I want to create a matrix that will consist of regions with stripped lines (so as to denote that it is full banded) and other regions that will be all zero. I have seen some rows & columns numbering in other posts, but I find the size of the "numbers" is usually as big as the elements of the matrix itself, which is appalling. A preview of what I want to create is portrayed in the picture below.

I find it extremely difficult to create something like this, but I have understood by now that everything is possible in Latex, the point is to know how.

• Can yo uinclude the bad output for us to start with? – percusse Apr 10 '14 at 14:43

which achieves:

Note that you can overwrite existing entries with fill=white (as in the first example) or just leave them blank as in the second example.

## Notes:

• This does require two runs: the first to compute the positions of the box, and the second to draw it in the correct spot.
• Since this is using tikz, you automatically get all the flexibility inherent in tikz, such as line styles, line thickness, line color, fill, etc. These can be passed to the \DrawBox macro to customize each instance or provided as default options to maintain consistency.

## Code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{patterns}

%% https://tex.stackexchange.com/questions/54358/custom-and-built-in-tikz-fill-patterns
% defining the new dimensions and parameters
\newlength{\hatchthickness}
\newlength{\hatchshift}
\newcommand{\hatchcolor}{}
% declaring the keys in tikz
hatchthickness/.code={\setlength{\hatchthickness}{#1}},
hatchshift/.code={\setlength{\hatchshift}{#1}},% must be >= 0
hatchcolor/.code={\renewcommand{\hatchcolor}{#1}}}
% setting the default values
hatchthickness=0.4pt,
hatchshift=0pt,% must be >= 0
hatchcolor=black}
% declaring the pattern
{custom north west lines}% name
{\pgfqpoint{\dimexpr-2\hatchthickness}{\dimexpr-2\hatchthickness}}% lower left corner
{% shape description
\pgfsetlinewidth{\hatchthickness}
\ifdim \hatchshift > 0pt
\pgfpathmoveto{\pgfqpoint{0pt}{\hatchshift}}
\pgfpathlineto{\pgfqpoint{\dimexpr0.15pt+\hatchshift}{-0.15pt}}
\fi
\pgfsetstrokecolor{\hatchcolor}
%    \pgfsetdash{{1pt}{1pt}}{0pt}% dashing cannot work correctly in all situation this way
\pgfusepath{stroke}
}

%% https://tex.stackexchange.com/questions/40028/highlight-elements-in-the-matrix
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newcommand{\DrawBox}[4][]{%
\tikz[overlay,remember picture]{
\draw[draw=gray,  #1]
($(#2)+(-0.2em,0.9em)$) rectangle
($(#3)+(0.2em,-0.3em)$);
\node at ($(#2)!0.5!(#3)$) {#4};
}%
}

\begin{document}
$M = \left[\begin{array}{*{13}{c}} 0 & 1 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 1 & 0 & 0 & 0 \\ 1 & 0 & 1 & 0 & 0 & 0 & 1 & 1 & 0 & 1 & 0 & 1 & 0 \\ 0 & \tikzmark{left 1}1 & 0 & 1 & 0 & 0 & 0 & 1 & 1 & 1 & 1 & 0 & 1 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 1 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 1 & 1 & 0 & 0 & 0 & 0\tikzmark{right 1} & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 1 & 1 & 0 & 0 & 0 & \tikzmark{left 2} \\ 0 & 0 & 0 & 0 & 1 & 0 & \\ 1 & 1 & 1 & 0 & 0 & 0 & \\ 0 & 0 & 1 & 0 & 0 & 0 & \\ 0 & 1 & 1 & 0 & 1 & 0 & \\ 0 & 0 & 1 & 0 & 0 & 0 & & & & & & & \tikzmark{right 2} \end{array}\right]$
\DrawBox[fill=white]{left 1}{right 1}{$\emptyset$}
\DrawBox[pattern=custom north west lines,]{left 2}{right 2}{}
\end{document}

• Nice one but using empty set for zero matrix is really really strange :) – percusse Apr 10 '14 at 18:45
• @percusse: Yep, but that is what the OP showed in the image. – Peter Grill Apr 11 '14 at 0:56