# How do I partition a matrx into blocks and replace zeros with dots?

I am using this command

$$W = \left( \begin{array}{ccccccccc} 1 & 0 & 0 & 0 & -1 & 0 & 0 & 0 & -1 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ -1 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & -1 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\ -1 & 0 & 0 & 0 & -1 & 0 & 0 & 0 & 1 \\ \end{array} \right)$$


to display a 9x9 matrix.

I would like to show this matrix as partitioned into 3x3 blocks, and also the zeros replaced with dots.

Doing a “search and replace” would not be a big problem, but if there's a dirty trick available…

\documentclass{article}
\usepackage{amsmath}

\begin{document}
$$W = \left(\mathcode0=\cdot \begin{array}{ *{3}{c} | *{3}{c} | *{3}{c} } 1 & 0 & 0 & 0 & -1 & 0 & 0 & 0 & -1 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\\hline 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ -1 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & -1 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ \hline 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\ -1 & 0 & 0 & 0 & -1 & 0 & 0 & 0 & 1 \\ \end{array} \right)$$
\end{document}


For the duration of the group initiate by \left(, I'm telling TeX that 0 in math mode (that is, in every cell) should be interpreted as \cdot.

• Your answers are always elegant. My upvote. – Sebastiano May 24 at 15:37
• Thanks! I got this to work--but not entirely so, trying to adapt it to a 16 x 16 into 4 x 4 blocks, as just added to my original question. – Paul B. Slater May 24 at 19:25
• My apologies--I have to add the \hline commands--which I hadn't noticed before--so I'll delete that addendum to my question. – Paul B. Slater May 24 at 19:54

You can use string replacement within LaTeX (xstring and collcell packages). For the blocks, I use dashed lines from arydshln (: in header and \hdashline in rows).

\documentclass{article}
\usepackage{array,arydshln}
\usepackage{xstring,collcell}

\newcommand{\ToDot}[1]{\StrSubstitute{#1}{0}{\cdot}}

\begin{document}
$$W = \left( \begin{array}{ *{3}{>{\collectcell\ToDot}c<{\endcollectcell}} : *{3}{>{\collectcell\ToDot}c<{\endcollectcell}} : *{3}{>{\collectcell\ToDot}c<{\endcollectcell}}} 1 & 0 & 0 & 0 & -1 & 0 & 0 & 0 & -1 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\\hdashline 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ -1 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & -1 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ \hdashline 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\ -1 & 0 & 0 & 0 & -1 & 0 & 0 & 0 & 1 \\ \end{array} \right)$$
\end{document}
`

• Very nice and my compliments. – Sebastiano May 24 at 15:37