排版具有长条目的稀疏矩阵

排版具有长条目的稀疏矩阵

当排版具有任意维度但重复条目(特别是空条目)的(方阵)矩阵时,我使用\cdots\vdots\ddots命令。当矩阵的条目足够短时,这种方法效果很好。但是,一旦某些条目很长,点就会变得混乱,矩阵的结构会变得难以阅读(不是说点没有正确连接,但这是一个小问题)。以下是一个例子:

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

在这个例子中,矩阵 (1) 很好,矩阵 (2) 的情况开始变得糟糕,而矩阵 (3) 甚至更糟:

在此处输入图片描述

我想如果我能够强制每行的高度(和每列的宽度)等于列的最大宽度,那么矩阵就没问题了。你知道怎么做吗?

答案1

我的回答仅涉及 MWE 中的第三个矩阵,因为它是最难以视觉吸引力的方式排版的矩阵。就像其他一些答案和评论已经指出的那样,没有必要(而且我认为相当丑陋)使列高等于列宽。因此,我下面的回答仅在行之间添加了少量空白(通过参数\extrarowheight)。但是,列宽都被强制为相同。此外,似乎(至少对于当前的情况)最好将单元格的内容左对齐而不是居中。

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

在此处输入图片描述

答案2

我会使用更少的点并使零三角形更加清晰:

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

结果

当然,整个矩阵也可以变成二次矩阵,例如通过设置适当的\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}

日志文件消息:

数组拉伸=5.1 -> (428.8665pt,428.4004pt)

二次函数

对我来说它已经看起来相当丑了,因此我节省时间来使列宽相等。

答案3

为了完整性,这里是仅基于对角点的替代解决方案,灵感来自这个帖子上面已经引用过。

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

\end{document}

输出结果如下:

在此处输入图片描述

答案4

与。{bNiceMatrix}nicematrix

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

\usepackage{nicematrix}

\begin{document}

\begin{equation}
\renewcommand{\arraystretch}{1.5}
\begin{bNiceMatrix}[nullify-dots,xdots/shorten = 2mm]
A_{-n,0}^{-n}  & A_{-n+1,-1}^{-n}  & 0 & \Cdots &&&& 0 \\
A_{-n,1}^{-n+1}  & A_{-n+1,0}^{-n+1}  & A_{-n+2,-1}^{-n+1}  & & & & & \Vdots \\
0 & A_{-n+1,1}^{-n+2}  & A_{-n+2,0}^{-n+2} & A_{-n+3,-1}^{-n+2} \\
\Vdots & \Ddots & \Ddots & \Ddots & \Ddots & \Ddots & & \\
 & & & & & & & \\
 & & & & 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 & \Cdots & & & & 0 & A_{n-1,1}^{n} & A_{n,0}^{n}  \\
\end{bNiceMatrix}
\end{equation}

\end{document}

上述代码的输出

相关内容