# Drawing dotted lines between elements of sparse matrix

I want to draw a large sparse matrix with equal column widths. Because I don't want to explicitly write all the zeros, I want to draw dotted/dashed lines to separate all columns and rows. This is my 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}

$$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{document}


I can control the column widths and draw solid lines in the matrix. What is the most efficient way to turn those solid lines into dotted/dashed lines (or even just make those solid lines gray)? My code looks quite messy as it is.

You could use a TikZ matrix. Note: if you draw each node individually, they don't quite line up in straight lines.

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


• This works great! FYI nodes={minimum size=2em} can be changed to nodes={minimum width=4em,minimum height=2em} in order to adjust the width/height of the cells. Oct 6 '17 at 6:46