[Tex/LaTex] Typesetting sparse matrices with long entries

formattingmatrices

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{equation}
\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}
\end{equation}

\begin{equation}
\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}
\end{equation}

\begin{equation}
\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{equation}

\end{document}

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

enter image description here

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?

Best Answer

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{equation}
\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{equation}

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}
\begin{equation}
\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{equation}

\end{document}

enter image description here