使用 tikz 绘制虚线边框矩阵

使用 tikz 绘制虚线边框矩阵

我需要绘制一个矩阵,使得每列和每行之间都有虚线。这是我的入门代码,但结果并不好。如您所见,第二条水平线不直接,垂直线没有到达终点

\begin{tikzpicture}
\matrix (mymatrix) [matrix of math nodes,left delimiter={(},right
delimiter={)}]
{ A(1)  &  B_{1,2} & B_{1,3}&  \cdots  &  B_{1,r-1} & B_{1,r} \\
    B_{1,2}^T  &  A(2)  &  0 & \cdots &  0  &  0 \\
    B_{1,3}^T &  0  &  A(3)  &  \cdots  &  0 & 0\\
    \vdots & 0 &  0  &   \ddots  &  0  &  0\\
    B_{1,r-1}^T  &  0  &  0  &  0  &  0  &  0\\
    B_{1,r}^T  &  0  &  0  &  0  &  0  &  0\\
};
\draw[black,dashed] (mymatrix-1-1.south west) -- (mymatrix-1-6.south east);
\draw[black,dashed] (mymatrix-2-1.south west) -- (mymatrix-2-6.south east);
\draw[black,dashed] (mymatrix-1-4.north east) -- (mymatrix-6-6.north east -| mymatrix-1-4.south east);
\end{tikzpicture}

在此处输入图片描述

答案1

MWE 以 开头\documentclass并结束\end{document},如下所示:

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{matrix,calc}
\begin{document}
\begin{tikzpicture}
\matrix (mymatrix) [matrix of math nodes,left delimiter={(},right
delimiter={)}]
{ A(1)  &  B_{1,2} & B_{1,3}&  \cdots  &  B_{1,r-1} & B_{1,r} \\
    B_{1,2}^T  &  A(2)  &  0 & \cdots &  0  &  0 \\
    B_{1,3}^T &  0  &  A(3)  &  \cdots  &  0 & 0\\
    \vdots & 0 &  0  &   \ddots  &  0  &  0\\
    B_{1,r-1}^T  &  0  &  0  &  0  &  0  &  0\\
    B_{1,r}^T  &  0  &  0  &  0  &  0  &  0\\
};
\foreach \X [evaluate=\X as \Y using {int(\X+1)}]in {1,...,2}
{\path ($(mymatrix-\X-1.south west)!0.5!(mymatrix-\Y-1.north west)$) coordinate
(aux\X);
\draw[black,dashed] (mymatrix.west |- aux\X) -- (mymatrix.east |- aux\X);
}
\foreach \X [evaluate=\X as \Y using {int(\X+1)}]in {1,...,5}
{\path ($(mymatrix-1-\X.east)!0.5!(mymatrix-1-\Y.west)$) coordinate
(auy\X);
\draw[black,dashed] (mymatrix.north -| auy\X) -- (mymatrix.south -| auy\X);
}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

供参考,{pNiceArray}结构nicematrixTikZ 节点矩阵并提供在数组中绘制虚线规则(或具有其他样式的 TikZ 的规则)的工具。

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

\begin{document}

\renewcommand{\arraystretch}{1.3}

$\begin{pNiceArray}{cccc|[tikz=dashed]cc}[margin=3pt]
    A(1)  &  B_{1,2} & B_{1,3}&  \cdots  &  B_{1,r-1} & B_{1,r} \\
\Hline[tikz=dashed]
    B_{1,2}^T  &  A(2)  &  0 & \cdots &  0  &  0 \\
\Hline[tikz=dashed]
    B_{1,3}^T &  0  &  A(3)  &  \cdots  &  0 & 0\\
    \vdots & 0 &  0  &   \ddots  &  0  &  0\\
    B_{1,r-1}^T  &  0  &  0  &  0  &  0  &  0\\
    B_{1,r}^T  &  0  &  0  &  0  &  0  &  0\\
\end{pNiceArray}$

\end{document}

上述代码的输出

相关内容