2

can somebody tell me how to get homogeneous spacing between the elements of my matrix? Here is the code I'm using but it is not working as I was expecting.

\documentclass[12pt]{scrartcl}  
\usepackage{amsmath,tikz}
\usepackage{tabstackengine}
\usetikzlibrary{matrix}
\setcounter{MaxMatrixCols}{20} 

\begin{document}

\begin{equation}
\fixTABwidth{T}
\begin{tikzpicture}
\matrix (m) [baseline=(current bounding box.center),matrix of math nodes,nodes in empty cells,right delimiter={]},left delimiter={[} ]
{-4 & 1 & 0 & 0 & .. & 0 & 1 & 0 & 0 & 0 & 0 & ... &  & &&&&\\
1 & -4 & 1 & 0 & .. & 0 & 0 & 1 & 0 & 0 & 0 & ... &  & &&&&\\
&    &   &   &    &   &   &   &   &   &   &     &  & &&&&\\
&    &   &   &    &   &   &   &   &   &   &     &  & &&&&\\
&    &   &   &    &   &   &   &   &   &   &     &  & &&&&\\
&    &   &   &    &   &   &   &   &   &   &     &  & &&&&\\
1 & 0 & 0 & 0 & .. & 0 & -4 & 1 & 0 & 0 & 0&...&0&1&0&0&0&...\\
0 & 1 & 0 & 0 & .. & 0 &  1 & -4 & 1 & 0& 0&...&0&0&1&0&0&...\\      
0 & 0 & 1 & 0 & .. & 0 &  0 & 1 & -4 & 1& 0&...&0&0&0&1&0&...\\ 
&    &   &   &    &   &   &   &   &   &   &     &  & &&&&\\
&    &   &   &    &   &   &   &   &   &   &     &  & &&&&\\
&    &   &   &    &   &   &   &   &   &   &     &  & &&&&\\
&    &   &   &    &   &   &   &   &   &   &     &  & &&&&\\
0 & 0 & 0 & 0 & .. & 0 &  0 & 1 & 0 & 0& ...&0&1&-4&1&0&0&...\\      
0 & 0 & 0 & 0 & .. & 0 &  0 & 0 & 1 & 0& ...&0&0&1&-4&1&0&...\\} ;
\draw[line width=0.4mm, loosely dotted] (m-2-2)-- (m-7-7);
\draw[line width=0.4mm,loosely dotted] (m-2-3)-- (m-7-8);
\draw[line width=0.4mm,loosely dotted] (m-2-1)-- (m-7-6);
\draw[line width=0.4mm,loosely dotted] (m-2-8)-- (m-7-14);
\draw[line width=0.4mm,loosely dotted] (m-9-8)-- (m-14-13);
\draw[line width=0.4mm,loosely dotted] (m-9-9)-- (m-14-14);
\draw[line width=0.4mm,loosely dotted] (m-9-10)-- (m-14-15);
\draw[line width=0.4mm,loosely dotted] (m-9-3)-- (m-14-8);
\end{tikzpicture}
\end{equation}

\end{document}

But it looks like this:enter image description here And I see that the spacing between neighbouring elements is not the same.

I'd appreciate any suggestion!

1
  • 2
    You may better served by the nicematrix package.
    – egreg
    Nov 3, 2019 at 15:40

2 Answers 2

2

You can achieve required spacing by adjusting the values of column sep of matrix and text width of node. See an example below.

\documentclass{scrartcl}  
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}
\matrix (m) [baseline=(current bounding box.center),matrix of math nodes,
column sep=0pt, nodes={text width=1.7em, align=right, anchor=east},
nodes in empty cells,right delimiter={]},left delimiter={[} ]
{-4 & 1 & 0 & 0 & .. & 0 & 1 & 0 & 0 & 0 & 0 & ... &  & &&&&\\
1 & -4 & 1 & 0 & .. & 0 & 0 & 1 & 0 & 0 & 0 & ... &  & &&&&\\
&    &   &   &    &   &   &   &   &   &   &     &  & &&&&\\
&    &   &   &    &   &   &   &   &   &   &     &  & &&&&\\
&    &   &   &    &   &   &   &   &   &   &     &  & &&&&\\
&    &   &   &    &   &   &   &   &   &   &     &  & &&&&\\
1 & 0 & 0 & 0 & .. & 0 & -4 & 1 & 0 & 0 & 0&...&0&1&0&0&0&...\\
0 & 1 & 0 & 0 & .. & 0 &  1 & -4 & 1 & 0& 0&...&0&0&1&0&0&...\\      
0 & 0 & 1 & 0 & .. & 0 &  0 & 1 & -4 & 1& 0&...&0&0&0&1&0&...\\ 
&    &   &   &    &   &   &   &   &   &   &     &  & &&&&\\
&    &   &   &    &   &   &   &   &   &   &     &  & &&&&\\
&    &   &   &    &   &   &   &   &   &   &     &  & &&&&\\
&    &   &   &    &   &   &   &   &   &   &     &  & &&&&\\
0 & 0 & 0 & 0 & .. & 0 &  0 & 1 & 0 & 0& ...&0&1&-4&1&0&0&...\\      
0 & 0 & 0 & 0 & .. & 0 &  0 & 0 & 1 & 0& ...&0&0&1&-4&1&0&...\\} ;
\draw[line width=0.4mm, loosely dotted] (m-2-2)-- (m-7-7);
\draw[line width=0.4mm,loosely dotted] (m-2-3)-- (m-7-8);
\draw[line width=0.4mm,loosely dotted] (m-2-1)-- (m-7-6);
\draw[line width=0.4mm,loosely dotted] (m-2-8)-- (m-7-14);
\draw[line width=0.4mm,loosely dotted] (m-9-8)-- (m-14-13);
\draw[line width=0.4mm,loosely dotted] (m-9-9)-- (m-14-14);
\draw[line width=0.4mm,loosely dotted] (m-9-10)-- (m-14-15);
\draw[line width=0.4mm,loosely dotted] (m-9-3)-- (m-14-8);
\end{tikzpicture}

\end{document}

enter image description here

1

A little bit modified @nidhin answer (+1) ... Differences are in

  • nodes style
  • columns and rows spacing
  • drawing dotted lines between nodes

enter image description here

\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
    \begin{tikzpicture}[every path/.style = {thick, loosely dotted}]
\matrix (m) [matrix of math nodes,
             nodes in empty cells,
             nodes={text width=1.2em, minimum height=2ex,
                    inner sep=0pt, align=right, anchor=south},
             right delimiter={]},left delimiter={[},
             column sep=1pt,
             column 5/.style  = {column sep=-0.3em},
             row 14 column 12/.style = {nodes={text width=0.5em}},
             row 15 column 12/.style = {nodes={text width=0.5em}},
             column 12/.style = {column sep=-0.3em},
             row sep=1pt
             ]
{
-4  & 1  & 0 & 0 & ... & 0 & 1  & 0  & 0  & 0 & 0   & ... &   &    &    &   &   &   \\
1   & -4 & 1 & 0 & ... & 0 & 0  & 1  & 0  & 0 & 0   & ... &   &    &    &   &   &   \\
    &    &   &   &     &   &    &    &    &   &     &     &   &    &    &   &   &   \\
    &    &   &   &     &   &    &    &    &   &     &     &   &    &    &   &   &   \\
    &    &   &   &     &   &    &    &    &   &     &     &   &    &    &   &   &   \\
    &    &   &   &     &   &    &    &    &   &     &     &   &    &    &   &   &   \\
1   & 0  & 0 & 0 & ... & 0 & -4 & 1  & 0  & 0 & 0   & ... & 0 & 1  &  0 & 0 & 0 &...\\
0   & 1  & 0 & 0 & ... & 0 &  1 & -4 & 1  & 0 & 0   & ... & 0 & 0  &  1 & 0 & 0 &...\\
0   & 0  & 1 & 0 & ... & 0 &  0 & 1  & -4 & 1 & 0   & ... & 0 & 0  &  0 & 1 & 0 &...\\
    &    &   &   &     &   &    &    &    &   &     &     &   &    &    &   &   &   \\
    &    &   &   &     &   &    &    &    &   &     &     &   &    &    &   &   &   \\
    &    &   &   &     &   &    &    &    &   &     &     &   &    &    &   &   &   \\
    &    &   &   &     &   &    &    &    &   &     &     &   &    &    &   &   &   \\
0   & 0  & 0 & 0 & ... & 0 &  0 & 1  & 0  & 0 & ... & 0   & 1 & -4 &  1 & 0 & 0 &...\\
0   & 0  & 0 & 0 & ... & 0 &  0 & 0  & 1  & 0 & ... & 0   & 0 & 1  & -4 & 1 & 0 &...\\
};
\draw   (m-2-1) -- (m-7-6.north);
\draw   (m-2-2) -- (m-7-7.north);
\draw   (m-2-3) -- (m-7-8.north);
\draw   (m-2-8) -- (m-7-14.north);
%
\draw   (m-9-3)-- (m-14-8.north);
\draw   (m-9-8) -- (m-14-13.north);
\draw   (m-9-9) -- (m-14-14.north);
\draw   (m-9-10)-- (m-14-15.north);
    \end{tikzpicture}
\end{document}

You must log in to answer this question.

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