28

I'm filling in large sparse matrices, for example

\begin{bmatrix}
0  & -2     & 1      &        & -1     & 2  \\
2  & \ddots & \ddots & \ddots &        & -1 \\
-1 & \ddots & \ddots & \ddots & \ddots &    \\
   & \ddots & \ddots & \ddots & \ddots & 1  \\
1  &        & \ddots & \ddots & \ddots & -2 \\
-2 & 1      &        & -1     & 2      & 0
\end{bmatrix}

enter image description here

I do not like the way the dots are spaced. I would like them to be equally spaced across the diagonals. I guess this can be done using TikZ but is there an easier way?

3
  • 1
    Welcome to TeX.sx! As new user without image posting privileges simply include the image as normal and remove the ! in front of it to turn it into a link. A moderator or another user with edit privileges can then reinsert the ! to turn it into an image again. Dec 19, 2011 at 22:15
  • Are the entries that are not specified supposed to be zero? Dec 19, 2011 at 22:45
  • @PeterGrill: Yes. Dec 19, 2011 at 23:01

5 Answers 5

13

It's not difficult with TikZ. I have just removed the ddots

\documentclass{article}
\usepackage{amsmath,tikz}
\usetikzlibrary{matrix}
\begin{document}
Following quadratic form involves a skew symmetric matrix. 
\begin{equation}
\begin{pmatrix}
    a\\b\\\vdots\\z
\end{pmatrix}^T
\begin{tikzpicture}[baseline=(current bounding box.center)]
\matrix (m) [matrix of math nodes,nodes in empty cells,right delimiter={]},left delimiter={[} ]{
0  & -2 & 1  &  & -1 & 2  \\
2  & & & & & -1 \\
-1 & & & & &    \\
   & & & & & 1  \\
1  & & & & & -2 \\
-2 &1 & & -1 & 2 & 0\\
} ;
\draw[loosely dotted] (m-1-1)-- (m-6-6);
\draw[loosely dotted] (m-1-2)-- (m-5-6);
\draw[loosely dotted] (m-2-1)-- (m-6-5);
\end{tikzpicture}\begin{pmatrix}
    a\\b\\\vdots\\z
\end{pmatrix}=0
\label{eq:eqq1}
\end{equation}

\end{document}

enter image description here

It looks slightly faint in the picture but if you wish you can add the option thick to the draw commands.

1
  • I've chosen this method because it requires the least modification. Dec 20, 2011 at 13:36
10

Such diagonals are definitely possible using graphic packages like tikz/pgf or pstricks. However, here's one using TeX leaders:

enter image description here

\documentclass{article}

\usepackage{amsmath,graphicx}

\newcommand{\diagdots}[3][-25]{%
  \rotatebox{#1}{\makebox[0pt]{\makebox[#2]{\xleaders\hbox{$\cdot$\hskip#3}\hfill\kern0pt}}}%
}

\begin{document}

\[
  \begin{bmatrix}
     0 & -2 & 1 &    & -1 &  2 \\
     2 &    &   &    &    & -1 \\
    -1 &    &   &    &    &    \\
       &    &  \multicolumn{2}{c}{\smash{\raisebox{.5\normalbaselineskip}{\diagdots{8em}{.5em}}}} &    &  1 \\
     1 &    &   &    &    & -2 \\
    -2 &  1 & \phantom{-2}  & -1 &  2 &  0
  \end{bmatrix}
\]

\end{document}

The minimal example provides \diagdots[<angle>]{<len>}{<skip>} that draws a diagonal array of dots (actually \cdots) of length <len> at an angle of <angle> (default is -25). The <skip> defines the approximate length between dots.

I've placed \diagdots in the middle of your bmatrix (horizontally by using \multicolumn{2}{c}{...} and vertically by using \raisebox{.5\normalbaselineskip}{...}), and \smashed it to remove any vertical height distortion. The \diagdots output has zero width (by virtue of \makebox[0pt]).

You can play around with the lengths and angles so see what suits you.

9

Ellipses are usually used to indicate that something is repeated. In this case you want to indicate that the 0 entry is repeated, and hence should show that there is a 0 on either end of the ellipses.

So, assuming that there are a fixed number of rows (as your example seems to indicate), then I would just use \cdots in either one column or both:

enter image description here

If the number of rows is not fixed, then I would use one of the following. My preference would be the last one as then there is no confusion as to where the 0s are.

enter image description here

\documentclass{article}
\usepackage{amsmath}

\begin{document}
\[
\begin{bmatrix}
 0 & -2 & 1 & 0      & -1 &  2 \\
 2 &  0 &   & \cdots &  0 & -1 \\
-1 &  0 &   & \cdots &  0 &  0 \\
 0 &  0 &   & \cdots &  0 &  1 \\
 1 &  0 &   & \cdots &  0 & -2 \\
-2 &  1 & 0 & -1     &  2 &  0
\end{bmatrix}
%
\begin{bmatrix}
 0 & -2 &  1     &  0     & -1 &  2 \\
 2 &  0 & \cdots & \cdots &  0 & -1 \\
-1 &  0 & \cdots & \cdots &  0 &  0 \\
 0 &  0 & \cdots & \cdots &  0 &  1 \\
 1 &  0 & \cdots & \cdots &  0 & -2 \\
-2 &  1 &  0     & -1     &  2 &  0
\end{bmatrix}
\]

If the number of rows is not fixed:
\[
\begin{bmatrix}
 0 & -2 & 1 & 0      & -1 &  2 \\
 2 &  0 &   & \cdots &  0 & -1 \\
-1 &  0 &   & \cdots &  0 &  0 \\
 0 &  0 &   & \cdots &  0 &  0 \\
 \vdots &  \vdots &   & \vdots &  \vdots &  \vdots \\
 0 &  0 &   & \cdots &  0 &  0 \\
 0 &  0 &   & \cdots &  0 &  1 \\
 1 &  0 &   & \cdots &  0 & -2 \\
-2 &  1 & 0 & -1     &  2 &  0
\end{bmatrix}
%
\begin{bmatrix}
 0      & -2     &  1     & 0      & -1     &  2 \\
 2      &  0     & \cdots & \cdots &  0     & -1 \\
-1      &  0     & \cdots & \cdots &  0     &  0 \\
 0      &  0     & \cdots & \cdots &  0     &  0 \\
 \vdots & \vdots & \ddots &        &  \vdots &  \vdots \\
 \vdots & \vdots &        & \ddots &  \vdots &  \vdots \\
 0      &  0     & \cdots & \cdots &  0     &  0 \\
 0      &  0     & \cdots & \cdots &  0     &  1 \\
 1      &  0     & \cdots & \cdots &  0     & -2 \\
-2      &  1     &  0     & -1     &  2     &  0
\end{bmatrix}
\]

\end{document}
1
  • I think we misunderstood each other. The blank entries are assumed to be zero, but the nonzero entries are repeated diagonally. Dec 20, 2011 at 13:38
3

I suggest to use the package nicematrix which has functionalities dedicated to his problem. The dotted lines are drawn with Tikz.

\documentclass{article}
\usepackage{nicematrix}
\NiceMatrixOptions{nullify-dots}
\begin{document}
\[\begin{bNiceMatrix}[columns-width = 5mm]
0  & -2     & 1      &        & -1 & 2  \\
2  & \Ddots & \Ddots & \Ddots &    & -1 \\
-1 & \Ddots &        &        &    &    \\
   & \Ddots &        &        &    & 1  \\
1  &        &        &        &    & -2 \\
-2 & 1      &        & -1     & 2  & 0
\end{bNiceMatrix}\]
\end{document}

enter image description here

2

runt it with xelatex

\documentclass{article}
\usepackage{mathtools}
\usepackage{pst-node}
\begin{document}
Following quadratic form involves a skew symmetric matrix. 
\begin{equation}\label{eq:eqq1}
\begin{pmatrix*}[r]
    a\\b\\c\\\vdots\\z
\end{pmatrix*}^T
\begin{bmatrix*}[r]
\rnode[rb]{C}{0}  & \rnode[rb]{B}{-2} & \rnode[rb]{A}{1}  &  & -1 & 2  \\
\rnode[rb]{D}{2}  & & & & & -1 \\
\rnode[rb]{E}{-1} & & & & &    \\
                  & & & & & \rnode[l]{a}{1}  \\
               1  & & & & & -\rnode[l]{b}{2} \\
              -2  &1& & -\rnode[l]{e}{1} & \rnode[rb]{d}{2} & \rnode[l]{c}{0}\\
\end{bmatrix*}
\begin{pmatrix*}[r]
    a\\b\\c\\\vdots\\z
\end{pmatrix*}=0
\psset{linestyle=dotted,nodesep=2mm}
\ncline{A}{a}\ncline{B}{b}\ncline{C}{c}\ncline{D}{d}\ncline{E}{e}
\end{equation}

\end{document}

enter image description here

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.