可以在 NiceMatrix 中排版上括号和下括号吗?

可以在 NiceMatrix 中排版上括号和下括号吗?

我正在尝试绘制带有上括号和下括号的矩阵。这可以借助节点编号来完成,并\tikzset处理书法括号的一些选项。

根据我的做法,输出符合要求。但是,这样的选项需要多个括号宽度和不同长度的括号幅度。我宁愿选择一种类似于\SubMatrix调整括号宽度的优雅解决方案。

相关话题

是否可以使用计数器iRowjCol来突出显示每个单元格NiceTabular

Tikz-在单元格矩阵下方绘制括号

在此处输入图片描述

这个 MWE 有助于展示我需要什么

\documentclass{amsart}

\usepackage[x11names]{xcolor}

\usepackage{booktabs}

\usepackage{nicematrix}
\usepackage{tikz}

\usetikzlibrary{decorations.pathreplacing,calligraphy}

\tikzset{
        BraceThick/.style = {decorate,
            decoration={calligraphic brace, amplitude=3.7pt,
                pre =moveto, pre  length=1.1pt,
                post=moveto, post length=1.1pt,
                raise=#1},
        thick, 
        pen colour=black},
        BraceThick/.default=0pt
}

\tikzset{
        BraceMirroredThick/.style = {decorate,
            decoration={calligraphic brace, amplitude=3.7pt,
                pre =moveto, pre  length=1.1pt,
                post=moveto, post length=1.1pt,
                mirror,
                raise=#1},
            thick,
            pen colour=black},
            BraceMirroredThick/.default=0pt,
}

\tikzset{
        BraceSemiThick/.style = {decorate,
            decoration={calligraphic brace, amplitude=2.5pt,
                pre =moveto, pre  length=1.1pt,
                post=moveto, post length=1.1pt,
                raise=#1},
        semithick, 
        pen colour=black},
        BraceSemiThick/.default=0pt
}

\tikzset{
        BraceMirroredSemiThick/.style = {decorate,
            decoration={calligraphic brace, amplitude=2.5pt,
                pre =moveto, pre  length=1.1pt,
                post=moveto, post length=1.1pt,
                mirror,
                raise=#1},
            semithick,
            pen colour=black},
            BraceMirroredSemiThick/.default=0pt,
}

\tikzset{
        BraceThin/.style = {decorate,
            decoration={calligraphic brace, amplitude=1.7pt,
                pre =moveto, pre  length=0.61pt,
                post=moveto, post length=0.61pt,
                raise=#1},
        thin, 
        pen colour=black},
        BraceThin/.default=0pt
}

\tikzset{
        BraceMirroredThin/.style = {decorate,
            decoration={calligraphic brace, amplitude=1.7pt,
                pre =moveto, pre  length=0.61pt,
                post=moveto, post length=0.61pt,
                mirror,
                raise=#1},
            thin,
            pen colour=black},
            BraceMirroredThin/.default=0pt,
}

\ExplSyntaxOn
    \NewDocumentCommand{\highlightNodes}{ O { } }
    {
        \foreach \i in {1,...,\arabic{iRow}}
        \foreach \j in {1,...,\arabic{jCol}}
        {
            \tikz [remember~picture, overlay] 
            \node [opacity=0.21] at (\i.5-|\j.5) {\color{gray} \i , \j} ;
        }
    }
\ExplSyntaxOff

\begin{document}

\begin{equation*}
    E =
        \begin{bNiceArray}{>{\strut}l|l}[create-extra-nodes, margin, extra-margin=1pt]
            \Block{2-1}{E_1}    & 0                 \\
            \cline {2-2}
            & \Block{2-1}{E_2}  \\
            \cline {1-1}
            0                   &                   \\
        \CodeAfter
        {
            \begin{tikzpicture}[name suffix = -large]
                \draw[BraceSemiThick={0pt}] (1-1.north west) -- node[above=0pt] {$C_1$} (1-1.north east);
                \draw[BraceSemiThick={0pt}] (1-2.north west) -- node[above=0pt] {$C_2$} (1-2.north east);
                \draw[BraceMirroredThin={0pt}] (3-1.south west) -- node[below=0pt] {$C_1$} (3-1.south east);
                \draw[BraceMirroredThin={0pt}] (3-2.south west) -- node[below=0pt] {$C_2$} (3-2.south east);
            \end{tikzpicture}
            \highlightNodes
        }
        \end{bNiceArray}
\end{equation*}

\end{document}

编辑

看起来过度矫正和不足矫正看起来都很好。但是,我们可以添加以下内容

\vspace{11mm}
\begin{equation*}
    \begin{bNiceArray}{ccc|c}[margin, create-extra-nodes]
        \Block[fill=Coral3]{3-3}<\large>{Z} &               &   &   0   \\
        & \hspace*{1cm} &   & \Vdots \\
        &               &   &   0    \\ \hline
        0            &    \Cdots     & 0 &   0
        \CodeAfter
        {
            \begin{tikzpicture}[name suffix = -large]
                \draw[BraceThick={0pt}] (1-1.north west) -- node[above=0pt] {$C_1$} (1-3.north east);
                \draw[BraceThick={0pt}] (1-4.north east) -- node[right=0pt] {$C_2$} (3-4.south east);
                \draw[BraceMirroredThin={0pt}] (4-4.south east)-- node[right=0pt] {$C_4$} (4-4.north east);
                \draw[BraceMirroredThick={0pt}] (4-1.south west)-- node[below=0pt] {$C_3$} (4-3.south east);
            \end{tikzpicture}
            %           \highlightNodes
        }
        \highlightNodes
    \end{bNiceArray}
    \mathstrut
\end{equation*}

\vspace{11mm}
\begin{equation*}
    \begin{bNiceArray}{ccc|c}[margin, create-extra-nodes]
        \Block[fill=Coral3]{3-3}<\large>{Z} &               &   &   0   \\
        & \hspace*{1cm} &   & \Vdots \\
        &               &   &   0    \\ \hline
        0            &    \Cdots     & 0 &   0
        \CodeAfter
        {
            \begin{tikzpicture}[name suffix = -large]
                \draw[BraceThin={0pt}] (1-1.north west) -- node[above=0pt] {$C_1$} (1-3.north east);
                \draw[BraceThin={0pt}] (1-4.north east) -- node[right=0pt] {$C_2$} (3-4.south east);
                \draw[BraceMirroredThick={0pt}] (4-4.south east)-- node[right=0pt] {$C_4$} (4-4.north east);
                \draw[BraceMirroredThin={0pt}] (4-1.south west)-- node[below=0pt] {$C_3$} (4-3.south  east);
            \end{tikzpicture}
            %           \highlightNodes
        }
        \highlightNodes
    \end{bNiceArray}
    \mathstrut
\end{equation*}

我们看到得到以下结果

在此处输入图片描述

如您所见,一个示例中的支架选择看起来不错,但另一个示例中的支架选择非常丑陋,因为支架太细了。对于BraceMirroredThickC4,它看起来甚至不像支架。

\SubMatrix调整其宽度(thickthin等)并amplitude动态地使其看起来美观。

答案1

像这样?

在此处输入图片描述

由于nicematrix也需要使用大括号tikz(并且最终图像需要两次或更多次编译),我认为在您的情况下最好采用纯tikz解决方案:

\documentclass[border=3.141592]{standalone}

\usepackage{mathdots}
\usepackage[x11names]{xcolor}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,
                calligraphy,
                fit,
                matrix}

\begin{document}
    \begin{tikzpicture}[
BC/.style args = {#1/#2}{% Braces Calligraphic
        decorate,
        decoration={calligraphic brace, amplitude=2pt,
        pre =moveto, pre  length=1pt,
        post=moveto, post length=1pt,
        raise=#1,
              #2},% for mirroring of brace
        thick,
        pen colour={black}
        },
                        ]  
\matrix (m) [matrix of math nodes,
             nodes={minimum height=2ex, minimum width=1.3em, inner sep=1pt, 
                    anchor=center, text=gray!50},
             column sep=4pt, row sep=1pt,
             left delimiter={[},right delimiter={]},
             inner xsep=0pt, inner ysep=1pt
             ]
{
1,1 & 1,2   & 1,3   & 1,4   \\
2,1 & 2,2   & 2,3   & 2,4   \\
3,1 & 3,2   & 3,3   & 3,4   \\
4,1 & 4,2   & 4,3   & 4,4   \\
};
\draw   (m-3-1.south west)  -- (m-3-4.south east)
        (m-1-3.north east)  -- (m-4-3.south east);
% braces
\draw[BC=2pt/ ] (m-1-1.north west) -- node[above=2pt] {$C_1$} (m-1-3.north east);
\draw[BC=8pt/ ] (m-1-4.north east) -- node[right=9pt] {$C_2$} (m-3-4.south east);
\draw[BC=2pt/mirror] (m-4-1.south west) -- node[below=2pt] {$C_3$} (m-4-3.south east);
\draw[BC=8pt/mirror] (m-4-4.south east) -- node[right=9pt] {$C_4$} (m-4-4.north east);
%
\node[fill=Coral3, semitransparent, fit=(m-1-1) (m-3-3),
      inner sep=0pt] {};
\draw[very thick, line cap=round, dash pattern=on 0pt off 2\pgflinewidth] 
        (m-1-4) node {0} -- (m-4-4) node {0} -- (m-4-1) node {0};
    \end{tikzpicture}
\end{document}

注意:上面的图片可以插入到数学环境中。另外,显示的结果只需要一个 MWE 编译即可。

答案2

6.4 版nicematrix(2021/11/23) 提供了新的命令\OverBrace\UnderBrace可用于\CodeAfter解决这种情况。括号是标准 LaTeX 的括号(即用当前数学字体构造的括号),而不是 Tikz 的括号。

\documentclass{book}
\usepackage[x11names]{xcolor}
\usepackage{nicematrix}
\usepackage{tikz}

\ExplSyntaxOn
\NewDocumentCommand{\highlightNodes}{ O { } }
{
    \foreach \i in {1,...,\arabic{iRow}}
    \foreach \j in {1,...,\arabic{jCol}}
    {
        \tikz [remember~picture, overlay] 
        \node [opacity=0.21] at (\i.5-|\j.5) {\color{gray} \i , \j} ;
    }
}
\ExplSyntaxOff

\begin{document}

\begin{equation*}
    \begin{bNiceArray}{ccc|c}[margin, create-extra-nodes]
        \Block[fill=Coral3]{3-3}<\large>{Z} & &   &   0   \\
          & \hspace*{1cm} &   & \Vdots \\
          &               &   &   0    \\ \hline
        0 &    \Cdots     & 0 &   0
        \CodeAfter
        \highlightNodes
        \OverBrace[shorten,yshift=1mm]{1-1}{4-3}{C_{1}}
        \UnderBrace[shorten]{4-1}{4-3}{C_{3}}
    \end{bNiceArray}
\end{equation*}

\end{document}

与往常一样nicematrix,您需要进行多次编译。

上述代码的输出

相关内容