6

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.

6

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.

  • 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
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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.