1

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.

enter image description here

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.

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

Here is an adaptation of:

  1. Highlight elements in the matrix and
  2. Custom and built in TikZ fill patterns

which achieves:

enter image description here

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.

References:

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{\hatchspread}
\newlength{\hatchthickness}
\newlength{\hatchshift}
\newcommand{\hatchcolor}{}
% declaring the keys in tikz
\tikzset{hatchspread/.code={\setlength{\hatchspread}{#1}},
         hatchthickness/.code={\setlength{\hatchthickness}{#1}},
         hatchshift/.code={\setlength{\hatchshift}{#1}},% must be >= 0
         hatchcolor/.code={\renewcommand{\hatchcolor}{#1}}}
% setting the default values
\tikzset{hatchspread=20pt,
         hatchthickness=0.4pt,
         hatchshift=0pt,% must be >= 0
         hatchcolor=black}
% declaring the pattern
\pgfdeclarepatternformonly[\hatchspread,\hatchthickness,\hatchshift,\hatchcolor]% variables
   {custom north west lines}% name
   {\pgfqpoint{\dimexpr-2\hatchthickness}{\dimexpr-2\hatchthickness}}% lower left corner
   {\pgfqpoint{\dimexpr\hatchspread+2\hatchthickness}{\dimexpr\hatchspread+2\hatchthickness}}% upper right corner
   {\pgfqpoint{\dimexpr\hatchspread}{\dimexpr\hatchspread}}% tile size
   {% shape description
    \pgfsetlinewidth{\hatchthickness}
    \pgfpathmoveto{\pgfqpoint{0pt}{\dimexpr\hatchspread+\hatchshift}}
    \pgfpathlineto{\pgfqpoint{\dimexpr\hatchspread+0.15pt+\hatchshift}{-0.15pt}}
    \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}
| improve this answer | |
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.