14

For a report I need to print some sparse matrices as full matrices, so that means that there are a lot of zeroes. If I print them in the same color as the meaningful entries, it doesn't look very organized. And when I don't print the zeroes at all, it also doesn't look that good. So I'd like to print the zeroes in a lighter color.

Of course I could define a color (e.g. \definecolor{lightgrey}{RGB}{200,200,200}, and then for each 0 write \textcolor{lightgrey}{0}. But this is not very efficient. It is also possible to define a new command, preferably with a short name, that just inserts the grey colored 0. But I wonder, is there another, cleaner way to accomplish this?

MWE for a relatively small matrix:

\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\begin{document}

\begin{align}
\left(\begin{array}{ccccccccc} 
1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\ 
0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\ 
0 & \frac{1}{2} & \frac{1}{2} & 0 & 0 & 0 & 0 & 0 & 0\\ 
0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\\ 
0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0\\ 
0 & 0 & 0 & 0 & \frac{1}{2} & \frac{1}{2} & 0 & 0 & 0\\ 
0 & 0 & 0 & \frac{1}{2} & 0 & 0 & \frac{1}{2} & 0 & 0\\ 
0 & 0 & 0 & 0 & \frac{1}{2} & 0 & 0 & \frac{1}{2} & 0\\ 
0 & 0 & 0 & 0 & \frac{1}{4} & \frac{1}{4} & 0 & \frac{1}{4} & \frac{1}{4} 
\end{array}\right)
\end{align}

\end{document}
4
  • 3
    \newcommand{\0}{\textcolor{lightgrey}{0}} and then \0 for the matrix entry you want in grey.
    – egreg
    Commented Apr 24, 2012 at 10:17
  • 1
    A separate hint: since you're already loading the amsmath package, you can replace \left(\begin{array}{ccccccccc} with \begin{pmatrix} and \end{array}\right) with \end{pmatrix}. Besides simplifying the input code, you get better spacing of the parentheses surrounding the array.
    – Mico
    Commented Apr 24, 2012 at 10:34
  • Thanks @Mico, I abstained from using it because some time ago I read something about a maximum number of columns.
    – Ailurus
    Commented Apr 24, 2012 at 11:53
  • 2
    @Ailurus: amsmath has the counter parameter MaxMatrixCols, whose default value is 10. (The matrix in your example has 9 columns.) If need be, this parameter can be reset rather easily to a larger number, e.g., \setcounter{MaxMatrixCols}{30}.
    – Mico
    Commented Apr 24, 2012 at 11:59

2 Answers 2

19

I basically type too many matrices with lots of figures etc. so it's kind of natural to me to use TikZ for this, but understandably, you might not want to use this. However, there is a particular option in matrix library that you can decide on what to do when a particular entry is empty. Here is an example

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}

I like lower triangular matrices so does Gauss while eliminating.
\[
\begin{tikzpicture}[baseline=(current bounding box.center)]% To center the matrix in the equation
\matrix[matrix of math nodes,execute at empty cell={\node[black!20]{0};},%Node color and text "0"
        every left delimiter/.style={xshift=1ex},%tighter delimiter spacing
        every right delimiter/.style={xshift=-1ex},
                left delimiter={(},right delimiter={)}
                ] {
1 &  &  &  &  &  &  &  & \\ 
 & 1 &  &  &  &  &  &  & \\ 
 & \frac{1}{2} & \frac{1}{2} &  &  &  &  &  & \\ 
 &  &  & 1 &  &  &  &  & \\ 
 &  &  &  & 1 &  &  &  & \\ 
 &  &  &  & \frac{1}{2} & \frac{1}{2} &  &  &\\ 
 &  &  & \frac{1}{2} &  &  & \frac{1}{2} &  & \\ 
 &  &  &  & \frac{1}{2} &  &  & \frac{1}{2} & \\ 
 &  &  &  & \frac{1}{4} & \frac{1}{4} &  & \frac{1}{4} & \frac{1}{4} \\
};
\end{tikzpicture}
\]
\end{document}

enter image description here

4
  • 5
    That's a great use of execute at empty cell to do the zeros. Not only makes the output clearer but makes the input clearer as well. Commented Apr 24, 2012 at 10:40
  • Very nice, I never thought of using TikZ for matrices. You really can do everything with it! Can I just make a new environment out of this (e.g. \sparsematrix{}), having a single argument for the actual matrix?
    – Ailurus
    Commented Apr 24, 2012 at 12:03
  • 1
    @Ailurus You can but actually you might not want to do that. You can define a new style such as \tikzset{sparsematrix/.style={ all those stuff here} } and put in the preamble. Then you need to type \tikz[baseline=...] \matrix[sparsematrix] {....};.
    – percusse
    Commented Apr 24, 2012 at 12:25
  • 1
    @Ailurus Or alternatively you change the column seperator & character to something else by ampersand replacement= option. and use something that doesn't upset TeX engine. I would still go with the first option.
    – percusse
    Commented Apr 24, 2012 at 12:26
17

If you want to avoid recoding your document, then you can use array package to drop in the colour change:

enter image description here

\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{color,array}
\def\zerotest{0}
\newcommand\greytest[2]{%
  \def\test{#2}%
  \ifx\test\zerotest
     \def\next{\textcolor[gray]{0.7}{0}}%
  \else
     \def\next{#1#2}%
  \fi
  \next}

\begin{document}

\begin{align}
\left(\begin{array}{*{9}{>{\greytest}c}} 
1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\ 
0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\ 
0 & \frac{1}{2} & \frac{1}{2} & 0 & 0 & 0 & 0 & 0 & 0\\ 
0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\\ 
0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0\\ 
0 & 0 & 0 & 0 & \frac{1}{2} & \frac{1}{2} & 0 & 0 & 0\\ 
0 & 0 & 0 & \frac{1}{2} & 0 & 0 & \frac{1}{2} & 0 & 0\\ 
0 & 0 & 0 & 0 & \frac{1}{2} & 0 & 0 & \frac{1}{2} & 0\\ 
0 & 0 & 0 & 0 & \frac{1}{4} & \frac{1}{4} & 0 & \frac{1}{4} & \frac{1}{4} 
\end{array}\right)
\end{align}

\end{document}
1
  • Also a very nice solution, it seems to be a little bit faster than the other one. Thanks!
    – Ailurus
    Commented Apr 25, 2012 at 7:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .