如何在 LaTeX 中绘制以下矩阵?

如何在 LaTeX 中绘制以下矩阵?

我想在 LaTeX 中绘制以下矩阵:

在此处输入图片描述

我发现我应该使用 TiZ 包,我花了很多时间查看这个包。但是我之前没有使用过这个包,也不知道怎么画。我该怎么办?

答案1

只是数组:

\documentclass{article}
\usepackage{amsmath,array}

\begin{document}

\[
\newcommand{\blocke}{
  \begin{matrix}
  \hphantom{0} & \hphantom{0} & \hphantom{0} & \hphantom{0} \\
  \\ \\
  \end{matrix}
}
\newcommand{\blockr}{
  \begin{matrix}
  \hphantom{0} & \hphantom{0} & \hphantom{0} &
  \hat{N}\smash[b]{\vphantom{\Big|}} \\ &&& \vdots \\ &&& 2 \\ &&& 1
  \end{matrix}
}
\newcommand{\blockl}{
  \begin{matrix}
  1 & \hphantom{0} & \hphantom{0} & \hphantom{0} \\
  2 \\ \vdots \\ \hat{N}\smash[b]{\vphantom{\Big|}}
  \end{matrix}
}
\newcommand{\dddots}{
  \begin{matrix}
  \hphantom{0} & \hphantom{0} & \ddots \\
  \\
  \\
  \ddots
  \end{matrix}
}
\begin{pmatrix}
\hspace*{0.3em}
\begin{array}{|*{5}{@{\,}c@{\!}|}}
\hline
& \blockl & & & \blocke \\
\hline
\blockr & & \blockl & & \\
\hline
& \blockr & & \blockl & \\
\hline
& & & \dddots & \\
\hline
& & & \blockr & \\
\hline
\end{array}
\hspace*{0.3em}
\end{pmatrix}
\]

\end{document}

我本地定义了一些命令以简化输入。

在此处输入图片描述

答案2

欢迎来到 TeX.SE!其实你不需要 Ti为此,Z。

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[\left(\begin{array}{|*{5}{ccccc|}}
\hline
& & & & & 1 & & & & & & & & & & & & & & & & & & &  \\
& & & & & 2 & & & & & & & & & & & & & & & & & & &  \\
& & & & & \vdots & & & & & & & & & & & & & & & & & & &  \\
& & & & & \vdots & & & & & & & & & & & & & & & & & & &  \\
& & & & & \widehat{N} & & & & & & & & & & & & & & & & & & &  \\
\hline
& & & & \widehat{N} & & & & & & 1 & & & & & & & & & & & & & &  \\
& & & & \vdots & & & & & & 2 & & & & & & & & & & & & & &  \\
& & & & \vdots & & & & & & \vdots & & & & & & & & & & & & & &  \\
& & & & 2 & & & & & & \vdots & & & & & & & & & & & & & &  \\
& & & & 1 & & & & & & \widehat{N} & & & & & & & & & & & & & &  \\
\hline
& & & & & & & & & \widehat{N} & & & & & & 1 & & & & & & & & &   \\
& & & & & & & & & \vdots & & & & & & 2 & & & & & & & & &    \\
& & & & & & & & & \vdots & & & & & & \vdots & & & & & & & & &   \\
& & & & & & & & & 2 & & & & & & \vdots & & & & & & & & &   \\
& & & & & & & & & 1 & & & & & & \widehat{N} & & & & & & & & &   \\
\hline
& & & & & & & & & & & & & & & & & \bullet & & & & & & &  \\
& & & & & & & & & & & & & & & & & & \bullet & & & & & &  \\
& & & & & & & & & & & & & & & \bullet & & & & \bullet & & & & &  \\
& & & & & & & & & & & & & & & & \bullet & & & & & & & &  \\
& & & & & & & & & & & & & & & & & \bullet & & & & & & &  \\
\hline
& & & & & & & & & & & & & & & & & & & \widehat{N} & & & & &  \\
& & & & & & & & & & & & & & & & & & & \vdots & & & & &  \\
& & & & & & & & & & & & & & & & & & & \vdots & & & & &  \\
& & & & & & & & & & & & & & & & & & & 2 & & & & &  \\
& & & & & & & & & & & & & & & & & & & 1 & & & & &  \\
\hline
\end{array}\right)\]
\end{document}

在此处输入图片描述

然而,你可以使用钛Z 来改善外观。

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\tikzset{% https://tex.stackexchange.com/a/156990/121799
  dots/.style args={#1per #2}{%
    line cap=round,
    dash pattern=on 0 off #2/#1
  }
}
\begin{document}
\[\left(\begin{array}{|*{5}{ccccc|}}
\hline
& & & & & 1 & & & & & & & & & & & & & & & & & & &  \\
& & & & & \tikzmarknode{21}{2} & & & & & & & & & & & & & & & & & & &  \\
& & & & &  & & & & & & & & & & & & & & & & & & &  \\
& & & & &  & & & & & & & & & & & & & & & & & & &  \\
& & & & & \tikzmarknode{N1}{\widehat{N}} & & & & & & & & & & & & & & & & & & &  \\
\hline
& & & & \tikzmarknode{N2}{\widehat{N}} & & & & & & 1 & & & & & & & & & & & & & &  \\
& & & & & & & & & & \tikzmarknode{23}{2} & & & & & & & & & & & & & &  \\
& & & &  & & & & & &  & & & & & & & & & & & & & &  \\
& & & & \tikzmarknode{22}{2} & & & & & &  & & & & & & & & & & & & & &  \\
& & & & 1 & & & & & & \tikzmarknode{N3}{\widehat{N}} & & & & & & & & & & & & & &  \\
\hline
& & & & & & & & & \tikzmarknode{N4}{\widehat{N}} & & & & & & 1 & & & & & & & & &   \\
& & & & & & & & &  & & & & & & \tikzmarknode{25}{2} & & & & & & & & &    \\
& & & & & & & & &  & & & & & &  & & & & & & & & &   \\
& & & & & & & & & \tikzmarknode{24}{2} & & & & & &  & & & & & & & & &   \\
& & & & & & & & & 1 & & & & & & \tikzmarknode{N5}{\widehat{N}} & & & & & & & & &   \\
\hline
& & & & & & & & & & & & & & & & & \bullet & & & & & & &  \\
& & & & & & & & & & & & & & & & & & \bullet & & & & & &  \\
& & & & & & & & & & & & & & & \bullet & & & & \bullet & & & & &  \\
& & & & & & & & & & & & & & & & \bullet & & & & & & & &  \\
& & & & & & & & & & & & & & & & & \bullet & & & & & & &  \\
\hline
& & & & & & & & & & & & & & & & & & & \tikzmarknode{N6}{\widehat{N}} & & & & &  \\
& & & & & & & & & & & & & & & & & & &  & & & & &  \\
& & & & & & & & & & & & & & & & & & &  & & & & &  \\
& & & & & & & & & & & & & & & & & & & \tikzmarknode{26}{2} & & & & &  \\
& & & & & & & & & & & & & & & & & & & 1 & & & & &  \\
\hline
\end{array}\right)\]
\begin{tikzpicture}[overlay,remember picture]
\foreach \X in {1,...,6}
{\draw[very thick, dots=8 per 1cm,shorten >=3pt,shorten <=3pt] (2\X) -- (N\X);}
\end{tikzpicture}
\end{document}

在此处输入图片描述

附录:为了让 Ryan Reich 高兴一点,这里有一个版本,您只需指定此稀疏数组的非空条目。这些条目由列表确定{6/1/1,6/2/2,6/5/\widehat{N},... },其中每个条目都具有结构<X>/<Y>/<entry>XY指定位置。请注意,如果您真的只有一种类型的列,则可以进一步缩短它,但这里的重点是允许构建该类型的任意稀疏矩阵。此代码使用这个答案

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,calc}
\usepackage{etoolbox}
\tikzset{% https://tex.stackexchange.com/a/156990/121799
  dots/.style args={#1per #2}{%
    line cap=round,
    dash pattern=on 0 off #2/#1
  }
}
\begin{document}
\edef\mylist{6/1/1,6/2/2,6/5/\widehat{N},%
5/6/\widehat{N},5/9/2,5/10/1,%
11/6/1,11/7/2,11/10/\widehat{N},%
10/11/\widehat{N},10/12/2,10/15/1,%
16/11/1,16/12/2,16/15/\widehat{N},%
20/21/\widehat{N},20/22/2,20/25/1%
}
\begin{tikzpicture}
    \let\mymatrixcontent\empty
    \foreach \Y in {1,...,25}{%
        \foreach \X in {1,...,25}
        {\xdef\tmp{{}}
         \foreach \XX/\YY/\ZZ in \mylist
          {\ifnum\XX=\X
           \ifnum\YY=\Y
            \xdef\tmp{\ZZ}
           \fi
          \fi}
          \ifnum\X=25
             \expandafter\gappto\expandafter\mymatrixcontent\expandafter{\tmp\space\\}% 
          \else
            \expandafter\gappto\expandafter\mymatrixcontent\expandafter{\tmp\space\&\space}%
          \fi   
        }
    }
    \matrix [matrix of math nodes,ampersand replacement=\&,left delimiter=(,
    right delimiter=),draw,row sep=3pt] (mat) {
        \mymatrixcontent
    };
    \foreach \X [evaluate=\X as \Y using {int(\X+1)}]in {5,10,...,20}
    {\coordinate (aux1) at ($(mat-1-\Y.west)!0.5!(mat-1-\X.east)$);
    \coordinate (aux2) at (mat-\X-1.south);
    \draw (mat.north -| aux1) -- (mat.south -| aux1)
    (mat.west |- aux2) -- (mat.east |- aux2);}
    \foreach \X/\Y [evaluate=\Y as \Z using {int(\Y+3)}] in
    {6/2,5/6,11/7,10/12,16/12,20/22} 
    {\draw[very thick, dots=8 per 1cm,shorten >=3pt,shorten <=3pt]
    (mat-\Y-\X) -- (mat-\Z-\X);}
    \draw[line width=1.2mm, dots=4 per 1cm]
    (mat-16-18.center) -- (mat-18-20.center)
    (mat-18-16.center) -- (mat-20-18.center);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容