如何在数组中添加垂直线和水平线

如何在数组中添加垂直线和水平线

我有以下代码:

\documentclass{article}
\begin{document}
    \[
    \left( \begin{array}{cccccccc|cccccccc}
    6 & 0 & 0 & 0 & 0 & 0 & 0 & 0 &   0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
    0 & 6 & 0 & 0 & 0 & 0 & 0 & 0 &   0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
    0 & 0 & 6 & 0 & 0 & 0 & 0 & 0 &   0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
    0 & 0 & 0 & 6 & 0 & 0 & 0 & 0 &   0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
    0 & 0 & 0 & 0 & 6 & 0 & 0 & 0 &   0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
    0 & 0 & 0 & 0 & 0 & 6 & 0 & 0 &   0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
    0 & 0 & 0 & 0 & 0 & 0 & 6 & 3 &   0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
    0 & 0 & 0 & 0 & 0 & 0 & 3 & 6 &   0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
    \hline
    0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 &   6 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
    0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 &   0 & 6 & 0 & 0 & 0 & 0 & 0 & 0 \\
    0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 &   0 & 0 & 6 & 0 & 0 & 0 & 0 & 0 \\
    0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 &   0 & 0 & 0 & 6 & 0 & 0 & 0 & 0 \\
    0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 &   0 & 0 & 0 & 0 & 6 & 0 & 0 & 0 \\
    0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 &   0 & 0 & 0 & 0 & 0 & 6 & 0 & 0 \\
    0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 &   0 & 0 & 0 & 0 & 0 & 0 & 6 & -3 \\
    0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 &   0 & 0 & 0 & 0 & 0 & 0 & -3 & 6 
    \end{array}\right) 
    \]
\end{document}

结果是:

在此处输入图片描述

有没有想过如何获得这个:

在此处输入图片描述

答案1

blkarray和 的解决方案booktabs

\documentclass{article}
\usepackage{blkarray}
\usepackage{booktabs}
\usepackage{mathtools}

\begin{document}

    \[ \aboverulesep=0pt \belowrulesep=0pt
    \begin{pmatrix} \begin{blockarray}{@{}*{8}{c}|*{8}{c}@{}}
        \begin{block}{*{6}{c}|cc|*{8}{c}}
        6 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
        0 & 6 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
        0 & 0 & 6 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
        0 & 0 & 0 & 6 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
        0 & 0 & 0 & 0 & 6 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
        0 & 0 & 0 & 0 & 0 & 6 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
        \cmidrule{1-8}
        0 & 0 & 0 & 0 & 0 & 0 & 6 & 3 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
        0 & 0 & 0 & 0 & 0 & 0 & 3 & 6 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
        \end{block}
    \midrule
        \begin{block}{*{8}{c}|*{6}{c}|cc}
        0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 6 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
        0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 6 & 0 & 0 & 0 & 0 & 0 & 0 \\
        0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 6 & 0 & 0 & 0 & 0 & 0 \\
        0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 6 & 0 & 0 & 0 & 0 \\
        0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 6 & 0 & 0 & 0 \\
        0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 6 & 0 & 0 \\
        \cmidrule{9-16}
        0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 6 &-3 \\
        0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -3 & 6 \\[-2ex]
        \end{block}
    \end{blockarray}\end{pmatrix}
    \]

\end{document} 

在此处输入图片描述

答案2

{NiceTabular}这是的解决方案nicematrix。该环境类似于经典的{tabular}array但在单元格、行和列下创建了 PGF/Tikz 节点。在构建矩阵后,您可以使用这些节点用 Tikz 绘制您想要的任何规则。

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


\begin{document}
    \[
    \setcounter{MaxMatrixCols}{16}
    \begin{pNiceMatrix}[margin]
    6 & 0 & 0 & 0 & 0 & 0 & 0 & 0 &   0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
    0 & 6 & 0 & 0 & 0 & 0 & 0 & 0 &   0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
    0 & 0 & 6 & 0 & 0 & 0 & 0 & 0 &   0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
    0 & 0 & 0 & 6 & 0 & 0 & 0 & 0 &   0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
    0 & 0 & 0 & 0 & 6 & 0 & 0 & 0 &   0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
    0 & 0 & 0 & 0 & 0 & 6 & 0 & 0 &   0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
    0 & 0 & 0 & 0 & 0 & 0 & 6 & 3 &   0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
    0 & 0 & 0 & 0 & 0 & 0 & 3 & 6 &   0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
    0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 &   6 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
    0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 &   0 & 6 & 0 & 0 & 0 & 0 & 0 & 0 \\
    0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 &   0 & 0 & 6 & 0 & 0 & 0 & 0 & 0 \\
    0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 &   0 & 0 & 0 & 6 & 0 & 0 & 0 & 0 \\
    0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 &   0 & 0 & 0 & 0 & 6 & 0 & 0 & 0 \\
    0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 &   0 & 0 & 0 & 0 & 0 & 6 & 0 & 0 \\
    0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 &   0 & 0 & 0 & 0 & 0 & 0 & 6 & -3 \\
    0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 &   0 & 0 & 0 & 0 & 0 & 0 & -3 & 6 
    \CodeAfter
    \begin{tikzpicture}
       \draw [shorten > = 2pt] (7-|1) -- (7-|9) ;
       \draw [shorten > = 2pt] (1-|7) -- (9-|7) ;
       \draw (9-|1) -- (9-|last) (1-|9) -- (last-|9) ;
       \draw [shorten < = 2pt] (15-|9) -- (15-|last) ;
       \draw [shorten < = 2pt] (9-|15) -- (last-|15) ;
    \end{tikzpicture}
    \end{pNiceMatrix}
    \]
\end{document}

上述代码的输出

相关内容