具有垂直线、水平线和对角线的矩阵

具有垂直线、水平线和对角线的矩阵

我怎样才能用 LaTeX 绘制这样的矩阵?

在此处输入图片描述

我四处寻找,但没有找到任何解决方案。

答案1

amsmath使用的矩阵和和 的pmatrix线进行运算:tikztikzmark

在此处输入图片描述

\documentclass{article}

\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{tikzmark,calc}
\newdimen\numht
\newdimen\numwd

\begin{document}
\pagenumbering{gobble}
\settowidth{\numwd}{0}
\settoheight{\numht}{(}

\begin{equation*}
  \begin{pmatrix}
    1\tikzmark{a} & & & \tikzmark{b}1\\
    0\tikzmark{c} & \\
    \\
    0\tikzmark{d} & & \tikzmark{e}0 & \tikzmark{f}1\\
  \end{pmatrix}
\end{equation*}

\begin{tikzpicture}[remember picture, overlay]
  \draw ($(pic cs:a)+(0,0.5\numht)$) -- ($(pic cs:b)+(0,0.5\numht)$);
  \draw ($(pic cs:a)+(0,0.0\numht)$) -- ($(pic cs:f)+(0,1 \numht)$);
  \draw ($(pic cs:c)+(0,0.0\numht)$) -- ($(pic cs:e)+(0,1 \numht)$);
  \draw ($(pic cs:c)+(-0.5\numwd,-1pt)$) -- ($(pic cs:d)+(-0.5\numwd,\numht)$);
  \draw ($(pic cs:d)+(0,0.5\numht)$) -- ($(pic cs:e)+(0,0.5\numht)$);
  \draw ($(pic cs:b)+(0.5\numwd,-1pt)$) -- ($(pic cs:f)+(0.5\numwd,\numht)$);
\end{tikzpicture}

\end{document}

或者,由土拨鼠先生大大简化:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\newcommand{\tikznode}[2]{%
\ifmmode%
\tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {$#2$};%
\else
\tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {#2};%
\fi}
\begin{document}

\begin{equation*}
  \begin{pmatrix}
    \tikznode{a}{1} & & & \tikznode{b}{1}\\
    \tikznode{c}{0} & \\
    \\
    \tikznode{d}{1} & & \tikznode{e}{0} & \tikznode{f}{1}\\
  \end{pmatrix}
\end{equation*}

\begin{tikzpicture}[remember picture, overlay,shorten >=1pt,shorten <=1pt]
  \draw (a) -- (b);
  \draw (c) -- (e);
  \draw (c) -- (d);
  \draw (d) -- (e);
  \draw (a) -- (f);
  \draw (b) -- (f);
\end{tikzpicture}

\end{document}

答案2

Phelype 的答案的替代方案是减少标签和计算:在 Ti 中绘制整个内容Z。

\documentclass{article}
\usepackage{amsmath,tikz}
\usetikzlibrary{matrix} 
\begin{document}

\[
  \begin{tikzpicture}[% from https://tex.stackexchange.com/a/330411/121799
    every left delimiter/.style={xshift=.75em},
    every right delimiter/.style={xshift=-.75em},
  ]
    \matrix[
      matrix of math nodes,
      left delimiter=(,
      right delimiter=),
      nodes in empty cells
    ] (m) {
      1 & ~~~ &  & 1 \\
      0 & & & \\
       & & & \\
      1 &  & 0& 1\\ 
    };
    \draw (m-1-1) -- (m-1-4);
    \draw (m-1-1) -- (m-4-4);
    \draw (m-2-1) -- (m-4-1);
    \draw (m-2-1.-20) -- (m-4-3);
    \draw (m-4-1) -- (m-4-3);
    \draw (m-1-4) -- (m-4-4);
  \end{tikzpicture}
\]

\end{document}

在此处输入图片描述

答案3

{pNiceMatrix}这是的解决方案nicematrix。您可以非常轻松地在(连续)虚线和实线之间切换。

\documentclass{article}
\usepackage{nicematrix}
\usepackage{tikz}

\begin{document}
\[
\begin{pNiceMatrix}
 1      & \Cdots &   & 1      \\
 0      & \Ddots &   & \Vdots \\
 \Vdots & \Ddots \\
 0      & \Cdots & 0 & 1  \\
\end{pNiceMatrix}
\hspace{1cm}
\begin{pNiceMatrix}[xdots/line-style=solid]
 1      & \Cdots &   & 1      \\
 0      & \Ddots &   & \Vdots \\
 \Vdots & \Ddots \\
 0      & \Cdots & 0 & 1  \\
\end{pNiceMatrix}
\]
\end{document} 

您需要进行多次编译(因为nicematrix在后台使用了 PGF/Tikz)。

上述代码的输出

答案4

仅使用 tikz(没有任何矩阵)的解决方案...我只是添加解决方案来展示一些关于缩放 tikzpictures 和考虑字体的好想法:

使用 \mscale 的值来看一下我的意思:

\documentclass{article}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{calc}


\def\bo{$\boldsymbol{1}$}
\def\bz{$\boldsymbol{0}$}

\begin{document}
\section{Test}
Text here

\xdef\mscale{2.2}% \mscale \in [0.4-2.3] %PLAY WITH THE VALUES IN THIS AREA 
\begin{tikzpicture}[scale =\mscale]
\pgfmathsetmacro\mysize{int(\mscale * 3.3)}
\pgfmathsetmacro\fixedpar{4.6*\mysize+1.6*\mysize*(0.8-\mscale)+.8*(\mysize*(1.3-\mscale)^3}
\foreach \mfont[count=\i from 1] in {\tiny,\footnotesize,\small,\normalsize,\large,\Large,\LARGE,\huge,\Huge,\HUGE}{\ifnum \mysize< \i \xdef\accsize{\expandafter\noexpand\mfont}\breakforeach\else\xdef\accsize{\expandafter\noexpand\mfont}\fi}
\tikzstyle{every node}=[font=\accsize]
\node (A)at (0,0) {\bo};
\node (B)at (0,-1) {\bz};
\node (C)at (3,0) {\bo};
\node (D)at (0,-3) {\bo};
\node (E)at (2,-3) {\bz};
\node (F)at (3,-3){\bo};
\draw[-] (A)--(F);
\draw[-] (C)--(F);
\draw[-] (B)--(D);
\draw[-] (B)--(E);
\draw[-] (A)--(C);
\draw[-] (D)--(E);
\node[yscale={\fixedpar},xscale={0.3*\fixedpar}] at (-0.5,-1.5){$($};
\node[yscale={\fixedpar},xscale={0.3*\fixedpar}] at (3.5,-1.5){$)$};
\end{tikzpicture}

\end{document}

仅改变比例的一些结果:

比例=0.4

在此处输入图片描述

比例=0.8

在此处输入图片描述

比例=1.8

在此处输入图片描述

虽然没有真正完美地调整,但它给出了一般的想法。(由于字体大小的非线性增加,二次函数对于校正是必须的)

相关内容