7

I am using this command

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

to display a 9x9 matrix.

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

2 Answers 2

7

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}
\begin{equation}
W =  \left(\mathcode`0=\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{equation}
\end{document}

enter image description here

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.

2
  • 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. Commented May 24, 2019 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. Commented May 24, 2019 at 19:54
5

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

enter image description here

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .