矩阵元素的均匀间距

矩阵元素的均匀间距

有人能告诉我如何在矩阵元素之间获得均匀的间距吗?这是我正在使用的代码,但它没有像我预期的那样工作。

\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}

但它看起来像这样:在此处输入图片描述我发现相邻元素之间的间距不一样。

我将非常感激任何建议!

答案1

column sep您可以通过调整矩阵和节点的值来实现所需的间距text width。请参阅下面的示例。

\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}

在此处输入图片描述

答案2

稍微修改了@nidhin 的回答(+1)... 不同之处在于

  • 节点样式
  • 列间距和行间距
  • 在节点之间绘制虚线

在此处输入图片描述

\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}

相关内容