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

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

我有这个数组:

\documentclass[crop,tikz,convert={outext=.svg,command\unexpanded{pdf2svg \infile\space\outfile}},multi=false]{standalone}
\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{positioning, matrix, fit, arrows.meta, shapes.misc, backgrounds}

\definecolor{customBlue}{HTML}{0063A2}
\definecolor{customAzure}{HTML}{C4EDF9}
\definecolor{customGreen}{HTML}{009053}
\definecolor{customPurple}{HTML}{812C7C}
\definecolor{customBrown}{HTML}{943227}
\definecolor{customViolet}{HTML}{3F367B}
\definecolor{customPink}{HTML}{DA0F70}
\definecolor{bracket}{HTML}{00A3DA}

\begin{document}
  \begin{tikzpicture}[x=1cm,y=1.5cm,]
    \matrix (C) [
      matrix of nodes,
      nodes={draw, minimum size=6mm},
      inner sep=6pt, outer sep=0pt,
      column sep=-\pgflinewidth
    ] at (0, 3){
      $\textcolor{customBlue}{2}, sl^1_0, ic^1_0$ &
      $\textcolor{orange}{31}, sl^1_1, ic^1_1$ &
      $\textcolor{customBrown}{102}, sl^1_2, ic^1_2$ &
      $\textcolor{customPink}{187}, sl^1_3, ic^1_3 $
      \\
    };
    \node[above=3pt of C-1-1]{$levels[1]$};
    \node[below=3pt of C-1-1]{$0$};
    \scoped[on background layer] {
      \node[fill=customAzure, inner sep=0mm, fit=(C-1-2.north east)(C-1-2.south west) ] {};
    }
  \end{tikzpicture}
\end{document}

如何在节点2和节点之间绘制括号102?结果如下: 在此处输入图片描述

答案1

一种可能性是 TiZ 库decorations.pathreplacing中有这样的装饰。您需要添加此库,括号的可选样式(查看我的tikzset),然后只需绘制括号(最后一个\draw)。

完整代码(您的代码与上述添加的内容):

\documentclass[crop,tikz]{standalone}
\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{positioning, matrix, fit, arrows.meta, shapes.misc, backgrounds,
                decorations.pathreplacing} % <-- we need this library for the underbrace

\definecolor{customBlue}  {HTML}{0063A2}
\definecolor{customAzure} {HTML}{C4EDF9}
\definecolor{customGreen} {HTML}{009053}
\definecolor{customPurple}{HTML}{812C7C}
\definecolor{customBrown} {HTML}{943227}
\definecolor{customViolet}{HTML}{3F367B}
\definecolor{customPink}  {HTML}{DA0F70}
\definecolor{bracket}     {HTML}{00A3DA}

\tikzset%
{% this sytle provides the underbrace
   myunderbrace/.style={bracket,decorate,decoration={brace,raise=4mm,amplitude=6pt,mirror}},
}

\begin{document}
  \begin{tikzpicture}[x=1cm,y=1.5cm,]
    \matrix (C) [
      matrix of nodes,
      nodes={draw, minimum size=6mm},
      inner sep=6pt, outer sep=0pt,
      column sep=-\pgflinewidth
    ] at (0, 3){
      $\textcolor{customBlue}{2}, sl^1_0, ic^1_0$ &
      $\textcolor{orange}{31}, sl^1_1, ic^1_1$ &
      $\textcolor{customBrown}{102}, sl^1_2, ic^1_2$ &
      $\textcolor{customPink}{187}, sl^1_3, ic^1_3 $
      \\
    };
    \node[above=3pt of C-1-1]{$levels[1]$};
    \node[below=3pt of C-1-1]{$0$};
    \scoped[on background layer] {
      \node[fill=customAzure, inner sep=0mm, fit=(C-1-2.north east)(C-1-2.south west) ] {};
    }
    \draw[myunderbrace] (C-1-1.south west) -- (C-1-3.south east);
  \end{tikzpicture}
\end{document}

还有图片: 在此处输入图片描述

当然,nicematrix这意味着项目mbc是另一个很好的可能性。

答案2

使用更花哨的(书法)括号,matrix of math nodes局部修改单元格样式和使用标签可以编写更简单、更简短的代码:

\documentclass[border=3.141592]{standalone}
\usepackage{tikz} % it load also xcolor
\usetikzlibrary{decorations.pathreplacing,%
                    calligraphy,% had to be after decorations.pathreplacing
                matrix,
                positioning} 

\definecolor{customBlue}  {HTML}{0063A2}
\definecolor{customAzure} {HTML}{C4EDF9}
\definecolor{customBrown} {HTML}{943227}
\definecolor{customPink}  {HTML}{DA0F70}

\tikzset%
{% this style provides the underbrace
BC/.style args = {#1/#2}{% Brace Calligraphic
        decorate,
        decoration={calligraphic brace, amplitude=6pt,
        raise=#1},% for mirroring of brace
        thick,
        pen colour={#2}  },
every label/.style = {draw=none, inner sep=0pt, font=\small}
}

\begin{document}
    \begin{tikzpicture}
\matrix (C) [matrix of math nodes,
             nodes={draw, minimum size=6mm},
             inner sep=6pt, outer sep=0pt,
             column sep=-\pgflinewidth,
             ]
{
|[label={\textit{levels}[1]}, label=below:0]|
                                \textcolor{customBlue}{2},      sl^1_0, ic^1_0  &
  |[fill=customAzure]|          \textcolor{orange}{31},  sl^1_1, ic^1_1    &
\textcolor{customBrown}{102},   sl^1_2, ic^1_2  &
                                \textcolor{customPink}{187},    sl^1_3, ic^1_3  \\
};
\draw[BC=3ex/red]  (C-1-3.south east) -- (C-1-1.south west);
  \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容