我想绘制一个具有相等列宽的大型稀疏矩阵。因为我不想明确地写出所有的零,所以我想画虚线来分隔所有的列和行。这是我的 MWE:
\documentclass[a4paper]{report}
\usepackage[T1]{fontenc}
\usepackage{amsmath,amsfonts,amssymb}
\usepackage{empheq,nccmath} % boxes in align environment
\usepackage[left=1.00in, right=1.00in, top=1.00in, bottom=1.00in]{geometry}
% draw vertical line down matrix
\makeatletter
\renewcommand*\env@matrix[1][*\c@MaxMatrixCols c]{%
\hskip -\arraycolsep
\let\@ifnextchar\new@ifnextchar
\array{#1}}
\makeatother
% fixed column width
\usepackage{tabularx}
\newcolumntype{C}[1]{>{\hspace{0pt}\centering\arraybackslash}p{#1}}
\begin{document}
\begin{equation}
A=
\resizebox{.99\hsize}{!}{\ensuremath{
\begin{bmatrix}[C{12mm}|C{12mm}|C{12mm}|C{12mm}|
C{12mm}|C{12mm}|C{12mm}|C{12mm}]%[cccc|cccc]
$\alpha$ & & & & & & & $1$ \\ \hline
$\alpha$ & & & & & & & \\ \hline
& & & & & & & \\ \hline
& & & & & & & \\ \hline
& & & $\alpha$ & & & & \\ \hline
& & & $1$ & & & & \\ \hline
& & & & & & & \\ \hline
& & & & & & $\beta$ & \\ \hline
$\alpha$ & & & & & $\alpha$ & & \\ \hline
& & & & & & &
\end{bmatrix}%
}}.
\end{equation}
\end{document}
我可以控制列宽并在矩阵中绘制实线。将这些实线变成点线/虚线(或者甚至只是将这些实线变成灰色)的最有效方法是什么?我的代码看起来很乱。
答案1
您可以使用 TikZ 矩阵。注意:如果您单独绘制每个节点,它们不会完全排成直线。
\documentclass[a4paper]{report}
\usepackage[T1]{fontenc}
\usepackage{amsmath,amsfonts,amssymb}
\usepackage{empheq,nccmath} % boxes in align environment
\usepackage[left=1.00in, right=1.00in, top=1.00in, bottom=1.00in]{geometry}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{equation}
\begin{tikzpicture}[baseline=(current bounding box.center)]
\matrix[matrix of math nodes,
nodes={minimum size=2em},
nodes in empty cells,
left delimiter=\lbrack,
right delimiter=\rbrack,
] (A) % matrix name
{
\alpha & \strut & & & & & & 1 \\
\alpha & & & & & & & \\
& & & & & & & \\
& & & & & & & \\
& & & \alpha & & & & \\
& & & 1 & & & & \\
& & & & & & & \\
& & & & & & \beta & \\
\alpha & & & & & \alpha & & \\
& & & & & & & \\
};
\foreach \i in {1,2,...,9} {\draw[dotted] (A-\i-1.south -| A.west) -- (A-\i-1.south -| A.east);}
\foreach \i in {1,2,...,7} {\draw[dotted] (A-1-\i.east |- A.north) -- (A-1-\i.east |- A.south);}
\end{tikzpicture}
\end{equation}
\end{document}