# Typesetting sparse matrices with long entries

When typesetting a (square) matrix with arbitrary dimension but repeated entries (in particular null entries), I make use of the \cdots, \vdots and \ddots commands. When the entries of the matrix are short enough, this works quite well. However, as soon as some entries are long, the dots get messed up and the structure of the matrix is much less readable (not speaking of the dots not joining up properly, but that is a minor problem here). Here is an example:

\documentclass{article}
\usepackage{amsmath}
\begin{document}

$$\begin{bmatrix} a_{11} & a_{12} & 0 & \cdots & \cdots & \cdots & \cdots & 0 \\ a_{21} & a_{22} & a_{23} & \ddots & && & \vdots \\ 0 & a_{32} & a_{33} & a_{34} & \ddots & & & \vdots \\ \vdots & \ddots & \ddots & \ddots & \ddots & \ddots & & \vdots \\ \vdots & & \ddots & \ddots & \ddots & \ddots & \ddots& \vdots\\ \vdots & & & \ddots & a_{76} & a_{77} & a_{78} & 0\\ \vdots & & & & \ddots & a_{87} & a_{88} & a_{89}\\ 0 & \cdots & \cdots & \cdots & \cdots & 0 & a_{98} & a_{99} \\ \end{bmatrix}$$

$$\begin{bmatrix} a_{11} & a_{12} & 0 & \cdots & \cdots & \cdots & \cdots & 0 \\ a_{21} & a_{22} & a_{23} & \ddots & && & \vdots \\ 0 & a_{32} & a_{33} & a_{34} & \ddots & & & \vdots \\ \vdots & \ddots & \ddots & \ddots & \ddots & \ddots & & \vdots \\ \vdots & & \ddots & \ddots & \ddots & \ddots & \ddots& \vdots\\ \vdots & & & \ddots & a_{n-2n-3} & a_{n-2n-2} & a_{n-2n-1} & 0\\ \vdots & && & \ddots & a_{n-1n-2} & a_{n-1n-1} & a_{n-1n}\\ 0 & \cdots & \cdots & \cdots & \cdots & 0 & a_{nn-1} & a_{nn} \\ \end{bmatrix}$$

$$\begin{bmatrix} A_{-n,0}^{-n} & A_{-n+1,-1}^{-n} & 0 & \cdots & \cdots & \cdots & \cdots & 0 \\ A_{-n,1}^{-n+1} & A_{-n+1,0}^{-n+1} & A_{-n+2,-1}^{-n+1} & \ddots & && & \vdots \\ 0 & A_{-n+1,1}^{-n+2} & A_{-n+2,0}^{-n+2} & A_{-n+3,-1}^{-n+2} & \ddots & & & \vdots \\ \vdots & \ddots & \ddots & \ddots & \ddots & \ddots & & \vdots \\ \vdots & & \ddots & \ddots & \ddots & \ddots & \ddots& \vdots\\ \vdots & & & \ddots & A_{n-3,1}^{n-2} & A_{n-2,0}^{n-2} & A_{n-1,-1}^{n-2} & 0\\ \vdots & && & \ddots & A_{n-2,1}^{n-1} & A_{n-1,0}^{n-1} & A_{n,-1}^{n-1}\\ 0 & \cdots & \cdots & \cdots & \cdots & 0 & A_{n-1,1}^{n} & A_{n,0}^{n} \\ \end{bmatrix}$$

\end{document}


In this example, matrix (1) is fine, things start to get ugly with matrix (2), and matrix (3) is even worse:

I guess if I was able to force the height of each row (and width of each column) to be equal to the maximum width of the columns, the matrix would like fine. Do you know how to do that?

-
–  percusse Sep 7 '12 at 14:47
Thanks for the pointer. This solves the problem of dots not joining up properly, but my question is slighly different: I would really like to try to force the matrix to have equal row height and column width (one reason is that I would like it to be clear that it is a square matrix). –  Corentin Sep 7 '12 at 14:56
With some confidence I can say that the result would be too sparse and rather a little uglier :) –  percusse Sep 7 '12 at 15:07

My answer only deals with the third matrix in your MWE, as it's the most challenging one to typeset in a visually appealing way. Like some of the other answers and comments have already noted, it's not necessary (as well as, I'd say, rather ugly) to make the column heights equal to the column widths. My answer below therefore adds only a small amount of whitespace between the rows (via the \extrarowheight parameter). The column widths, however, are all forced to be the same. Furthermore, it would seem (at least for the case at hand) that it's preferable to left-align rather than to center the cells' contents.

\documentclass{article}
\usepackage{amsmath}
\usepackage[margin=1in]{geometry}

\newlength{\mycolwidth}
\settowidth{\mycolwidth}{$A_{n-1,-1}^{n-2}$} % widest entry

\usepackage{array}
\newcolumntype{Z}{>{$}p{\mycolwidth}<{$}}

\begin{document}
First, the plain (bmatrix) solution:
$$\begin{bmatrix} A_{-n,0}^{-n} & A_{-n+1,-1}^{-n} & 0 & \cdots & \cdots & \cdots & \cdots & 0 \\ A_{-n,1}^{-n+1} & A_{-n+1,0}^{-n+1} & A_{-n+2,-1}^{-n+1} & \ddots & && & \vdots \\ 0 & A_{-n+1,1}^{-n+2} & A_{-n+2,0}^{-n+2} & A_{-n+3,-1}^{-n+2} & \ddots & & & \vdots \\ \vdots & \ddots & \ddots & \ddots & \ddots & \ddots & & \vdots \\ \vdots & & \ddots & \ddots & \ddots & \ddots & \ddots& \vdots\\ \vdots & & & \ddots & A_{n-3,1}^{n-2} & A_{n-2,0}^{n-2} & A_{n-1,-1}^{n-2} & 0\\ \vdots & && & \ddots & A_{n-2,1}^{n-1} & A_{n-1,0}^{n-1} & A_{n,-1}^{n-1}\\ 0 & \cdots & \cdots & \cdots & \cdots & 0 & A_{n-1,1}^{n} & A_{n,0}^{n} \\ \end{bmatrix}$$

Second, a solution that forces all columnwidths to be the
same, left-aligns the cells' contents, and increases the
distances between rows:

\setlength{\extrarowheight}{1.5\baselineskip}
$$\left[ \begin{array}{*{8}{Z}} A_{-n,0}^{-n} & A_{-n+1,-1}^{-n} & 0 & \cdots & \cdots & \cdots & \cdots & 0 \\ A_{-n,1}^{-n+1} & A_{-n+1,0}^{-n+1} & A_{-n+2,-1}^{-n+1} & \ddots & && & \vdots \\ 0 & A_{-n+1,1}^{-n+2} & A_{-n+2,0}^{-n+2} & A_{-n+3,-1}^{-n+2} & \ddots & & & \vdots \\ \vdots & \ddots & \ddots & \ddots & \ddots & \ddots & & \vdots \\ \vdots & & \ddots & \ddots & \ddots & \ddots & \ddots& \vdots\\ \vdots & & & \ddots & A_{n-3,1}^{n-2} & A_{n-2,0}^{n-2} & A_{n-1,-1}^{n-2} & 0\\ \vdots & && & \ddots & A_{n-2,1}^{n-1} & A_{n-1,0}^{n-1} & A_{n,-1}^{n-1}\\ 0 & \cdots & \cdots & \cdots & \cdots & 0 & A_{n-1,1}^{n} & A_{n,0}^{n} \\[5ex] \end{array} \right]$$

\end{document}


-
Thank you very much for your answer. Your second matrix looks reasonably good, and I have learnt the \extrarowheight parameter. –  Corentin Sep 8 '12 at 14:50

I would use less dots and make the triangles of zeroes more clear:

\documentclass{article}
\usepackage[hmargin=1in]{geometry}
\usepackage{amsmath}
\setcounter{MaxMatrixCols}{20}
\begin{document}
\begin{gather}
\begin{bmatrix}
a_{11} & a_{12} & 0 & \cdots &&&&&& \cdots & 0
\\
a_{21} & a_{22} & a_{23} & 0 & \cdots &&&&& \cdots & 0
\\
0 & a_{32} & a_{33} & a_{34} & 0 & \cdots &&&& \cdots & 0
\\
\vdots &&&&& \ddots &&&&& \vdots
\\
0 & \cdots &&&& \cdots & 0 & a_{76} & a_{77} & a_{78} & 0
\\
0 & \cdots &&&&& \cdots & 0 & a_{87} & a_{88} & a_{89}
\\
0 & \cdots &&&&&& \cdots & 0 & a_{98} & a_{99}
\end{bmatrix}
\\
\begin{bmatrix}
a_{11} & a_{12} & 0 & \cdots &&&&&& \cdots & 0
\\
a_{21} & a_{22} & a_{23} & 0 & \cdots &&&&& \cdots & 0
\\
0 & a_{32} & a_{33} & a_{34} & 0 & \cdots &&&& \cdots & 0
\\
\vdots &&&&& \ddots &&&&& \vdots
\\
0 & \cdots &&&& \cdots & 0 & a_{n-2,n-3} & a_{n-2,n-2} & a_{n-2,n-1} & 0
\\
0 & \cdots &&&&& \cdots & 0 & a_{n-1,n-2} & a_{n-1,n-1} & a_{n-1,n}
\\
0 & \cdots &&&&&& \cdots & 0 & a_{n,n-1} & a_{n,n}
\end{bmatrix}
\\
\begin{bmatrix}
A^{-n}_{-n,0} & A^{-n}_{-n+1,-1} & 0 & \cdots &&&&&& \cdots & 0
\\
A^{-n+1}_{-n,1} & A^{-n+1}_{-n+1,0} & A^{-n+1}_{-n+2,-1} & 0 & \cdots &&&&& \cdots & 0
\\
0 & A^{-n+2}_{-n+1,1} & A^{-n+2}_{-n+2,0} & A^{-n+2}_{-n+3,-1} & 0 & \cdots &&&& \cdots & 0
\\
\vdots &&&&& \ddots &&&&& \vdots
\\
0 & \cdots &&&& \cdots & 0 & A^{n-2}_{n-3,1} & A^{n-2}_{n-2,0} & A^{n-2}_{n-1,-1} & 0
\\
0 & \cdots &&&&& \cdots & 0 & A^{n-1}_{n-2,1} & A^{n-1}_{n-1,0} & A^{n-1}_{n,-1}
\\
0 & \cdots &&&&&& \cdots & 0 & A^n_{n-1,1} & A^n_{n,0}
\end{bmatrix}
\end{gather}
\end{document}


Of course the whole matrix could be made quadratic, e.g. by setting an appropriate \arraystretch:

\documentclass{article}
\usepackage[hmargin=1in]{geometry}
\usepackage{amsmath}
\setcounter{MaxMatrixCols}{20}
\begin{document}
\newcommand*{\matrixC}[1][2]{%
\begingroup
\renewcommand*{\arraystretch}{#1}%
\begin{bmatrix}
A^{-n}_{-n,0} & A^{-n}_{-n+1,-1} & 0 & \cdots &&&&&& \cdots & 0
\\
A^{-n+1}_{-n,1} & A^{-n+1}_{-n+1,0} & A^{-n+1}_{-n+2,-1} & 0 & \cdots &&&&& \cdots & 0
\\
0 & A^{-n+2}_{-n+1,1} & A^{-n+2}_{-n+2,0} & A^{-n+2}_{-n+3,-1} & 0 & \cdots &&&& \cdots & 0
\\
\vdots &&&&& \ddots &&&&& \vdots
\\
0 & \cdots &&&& \cdots & 0 & A^{n-2}_{n-3,1} & A^{n-2}_{n-2,0} & A^{n-2}_{n-1,-1} & 0
\\
0 & \cdots &&&&& \cdots & 0 & A^{n-1}_{n-2,1} & A^{n-1}_{n-1,0} & A^{n-1}_{n,-1}
\\
0 & \cdots &&&&&& \cdots & 0 & A^n_{n-1,1} & A^n_{n,0}
\end{bmatrix}%
\endgroup
}
\newcommand*{\test}[1]{%
\begingroup
\sbox0{$\displaystyle\matrixC[#1]$}%
\typeout{arraystretch=#1 -> (\the\wd0,\the\dimexpr\ht0+\dp0)}%
\endgroup
}
\test{5.1}
\begin{gather}
\matrixC[5.1]
\end{gather}
\end{document}


Log file message:

arraystretch=5.1 -> (428.8665pt,428.4004pt)

It already looks quite ugly for me, therefore I save my time to make equal column widths.

-
Thanks a lot for your answer. I agree that equal row height and column width is maybe a little excessive. Nevertheless, from the point of view of clarity rather than pure aesthetics (which is sometimes subjective), your last matrix is easier to read than the other ones (in fact, although they look better, they are slightly misleading as they don't make it clear that the diagonal elements are indeed on the same diagonal...) –  Corentin Sep 8 '12 at 14:44

For completeness, here is an alternative solution based only on diagonal dots, inspired from this post already cited above.

\documentclass{article}

\usepackage{amsmath}
\usepackage[margin=1in]{geometry}
\usepackage{graphicx}

\newcommand{\diagdots}[3][-25]{%
\rotatebox{#1}{\makebox[0pt]{\makebox[#2]{\xleaders\hbox{$\cdot$\hskip#3}\hfill\kern0pt}}}%
}

\begin{document}

$$\begin{bmatrix} A_{-n,0}^{-n} & A_{-n+1,-1}^{-n} & 0 & \multicolumn{4}{c}{\diagdots[0]{19em}{.4em}} & 0 \\ A_{-n,1}^{-n+1} & A_{-n+1,0}^{-n+1} & A_{-n+2,-1}^{-n+1} & & && & \\ 0 & A_{-n+1,1}^{-n+2} & A_{-n+2,0}^{-n+2} & A_{-n+3,-1}^{-n+2} & & & & \\ & & & & \multicolumn{2}{c}{\raisebox{0.25\normalbaselineskip}{\diagdots[-18]{20em}{.4em}}} & & \diagdots[-90]{6em}{.4em} \\ & & & \multicolumn{2}{c}{\diagdots[-18]{10em}{.4em}} & & & \\ \diagdots[-90]{6em}{.4em} & & \multicolumn{2}{c}{\raisebox{-0.5\normalbaselineskip}{\diagdots[-18]{20em}{.4em}}} & & & & \\ & & & & A_{n-3,1}^{n-2} & A_{n-2,0}^{n-2} & A_{n-1,-1}^{n-2} & 0\\ & && & & A_{n-2,1}^{n-1} & A_{n-1,0}^{n-1} & A_{n,-1}^{n-1}\\ 0 & \multicolumn{4}{c}{\diagdots[0]{19em}{.4em}} & 0 & A_{n-1,1}^{n} & A_{n,0}^{n} \\ \end{bmatrix}$$

\end{document}


And the output:

-