矩阵内矩阵

矩阵内矩阵

我怎样才能在矩阵内创建一个矩阵,如图所示? 在此处输入图片描述

事实上,我想将每个块视为一个单独的矩阵。例如,我想通过\underbrace{}‎ 或\overbrace{}‎ 命令在第二个矩阵的上方和下方添加括​​号,如下图所示:

在此处输入图片描述

答案1

这是一个 TiZ 方法。

步骤1

首先我发现fit库在计算边界框时很有用。在下图中,棕色框未包含在内,bbbbbb_3因为我没有传递(A-2-3)fit=

\documentclass[border=9,tikz]{standalone}
    \usetikzlibrary{matrix,fit}
\begin{document}
\makeatletter
\begin{tikzpicture}
    \matrix(A)[matrix of math nodes]{
        a_1 & a_2 & a_3 & aaaaaa_4 \\
        b_1 & b_2 & bbbbbb_3 & b_4 \\
        c_1 & cccccc_2 & c_3 & c_4 \\
        dddddd_1 & d_2 & d_3 & d_4 \\
    };
    \node[draw=brown,inner sep=0,fit={(A-2-2)(A-3-2)(A-3-3)}]{};
\end{tikzpicture}

第2步

由于棕色框实际上是一个节点,因此我想赋予该节点一个特殊的形状:左括号形状。代码基本上是从 复制而来tikzlibrarymatrix.code.tex

\pgfdeclareshape{left parenthesis}{
  \inheritsavedanchors[from=rectangle]\inheritanchorborder[from=rectangle]
  \inheritanchor[from=rectangle]{center}
  \foregroundpath{
    \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
    \northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
    \pgf@yc=\pgf@yb \advance\pgf@yc-\pgf@ya
    \pgftransformshift{\pgfpoint{\pgf@xa}{.5\pgf@ya+.5\pgf@yb}}
    \pgfnode{rectangle}{center}{$\left(\vcenter{\hrule height\pgf@yc width0pt}\right.$}{leftparenthesis}{\pgfusepath{}}
  }
}
\begin{tikzpicture}
    \matrix(A)[matrix of math nodes]{
        a_1 & a_2 & a_3 & aaaaaa_4 \\
        b_1 & b_2 & bbbbbb_3 & b_4 \\
        c_1 & cccccc_2 & c_3 & c_4 \\
        dddddd_1 & d_2 & d_3 & d_4 \\
    };
    \node[draw=brown,          inner sep=0,fit={(A-2-2)(A-3-2)(A-3-3)}]{};
    \node[red,left parenthesis,inner sep=0,fit={(A-2-2)(A-3-2)(A-3-3)}]{};
\end{tikzpicture}

步骤 2.5

针对其他形状的相同任务

\pgfdeclareshape{below curly bracket}{
  \inheritsavedanchors[from=rectangle]\inheritanchorborder[from=rectangle]
  \inheritanchor[from=rectangle]{center}
  \foregroundpath{
    \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
    \northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
    \pgf@xc=\pgf@xb \advance\pgf@xc-\pgf@xa
    \pgftransformshift{\pgfpoint{.5\pgf@xa+.5\pgf@xb}{\pgf@ya}}\pgftransformrotate{90}
    \pgfnode{rectangle}{center}{$\left\{\vcenter{\hrule height\pgf@xc width0pt}\right.$}{belowcurlybracket}{\pgfusepath{}}
  }
}
\begin{tikzpicture}
    \matrix(A)[matrix of math nodes]{
        a_1 & a_2 & a_3 & aaaaaa_4 \\
        b_1 & b_2 & bbbbbb_3 & b_4 \\
        c_1 & cccccc_2 & c_3 & c_4 \\
        dddddd_1 & d_2 & d_3 & d_4 \\
    };
    \node[draw=brown,              inner sep=0,fit={(A-2-2)(A-3-2)(A-3-3)}]{};
    \node[blue,below curly bracket,inner sep=0,fit={(A-2-2)(A-3-2)(A-3-3)}]{};
\end{tikzpicture}

步骤3

请记住,这里的一切都是节点,因此可以fit=分层。

\begin{tikzpicture}
    \matrix(A)[matrix of math nodes]{
        a_1 & a_2 & a_3 & aaaaaa_4 \\
        b_1 & b_2 & bbbbbb_3 & b_4 \\
        c_1 & cccccc_2 & c_3 & c_4 \\
        dddddd_1 & d_2 & d_3 & d_4 \\
    };
    \node[draw=brown,              inner sep=0,fit={(A-2-2)(A-3-2)(A-3-3)}]{};
    \node[red,left parenthesis,    inner sep=0,fit={(A-2-2)(A-3-2)(A-3-3)}]{};
    \node[blue,below curly bracket,inner sep=0,fit={(A-2-2)(A-3-2)(A-3-3)(leftparenthesis)}]{};
    \node[red,left parenthesis,    inner sep=0,fit={(A-2-2)(A-3-2)(A-3-3)(belowcurlybracket)}]{};
    \node[blue,below curly bracket,inner sep=0,fit={(A-2-2)(A-3-2)(A-3-3)(leftparenthesis)}]{};
    \node[red,left parenthesis,    inner sep=0,fit={(A-2-2)(A-3-2)(A-3-3)(belowcurlybracket)}]{};
\end{tikzpicture}

步骤4

然后我们需要添加文字,所以我必须将锚点移动text到合适的位置。(事实上我重新定义它。)请注意,在这种情况下,边界框计算会忽略我们的文本。 (这始终是正确的。但通常我们的文本完全位于边框内,因为我们想要这样。)

\pgfdeclareshape{above square bracket}{
  \inheritsavedanchors[from=rectangle]\inheritanchorborder[from=rectangle]
  \inheritanchor[from=rectangle]{center}
  \anchor{text}{
    \southwest
    \pgf@xa=\pgf@x
    \northeast
    \pgf@x=\pgf@xa
    \advance\pgf@y3pt
  }
  \foregroundpath{
    \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
    \northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
    \pgf@xc=\pgf@xb \advance\pgf@xc-\pgf@xa
    \pgftransformshift{\pgfpoint{.5\pgf@xa+.5\pgf@xb}{\pgf@yb}}\pgftransformrotate{90}
    \pgfnode{rectangle}{center}{$\left.\vcenter{\hrule height\pgf@xc width0pt}\right]$}{abovesquarebracket}{}
  }
}
\begin{tikzpicture}
    \matrix(A)[matrix of math nodes]{
        a_1 & a_2 & a_3 & aaaaaa_4 \\
        b_1 & b_2 & bbbbbb_3 & b_4 \\
        c_1 & cccccc_2 & c_3 & c_4 \\
        dddddd_1 & d_2 & d_3 & d_4 \\
    };
    \node[draw=brown,               inner sep=0,fit={(A-2-2)(A-3-2)(A-3-3)}]{};
    \node[teal,above square bracket,inner sep=0,fit={(A-2-2)(A-3-2)(A-3-3)}]{Caption};
    \node[red,left parenthesis,     inner sep=0,fit={(A-2-2)(A-3-2)(A-3-3)(abovesquarebracket)}]{};
    \node[blue,below curly bracket, inner sep=0,fit={(A-2-2)(A-3-2)(A-3-3)(leftparenthesis)}]{};
\end{tikzpicture}

结果

\pgfdeclareshape{above curly bracket}{
  \inheritsavedanchors[from=rectangle]\inheritanchorborder[from=rectangle]
  \inheritanchor[from=rectangle]{center}
  \anchor{text}{
    \southwest
    \pgf@xa=\pgf@x
    \northeast
    \pgf@x=\pgf@xa
    \advance\pgf@y5pt
  }
  \foregroundpath{
    \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
    \northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
    \pgf@xc=\pgf@xb \advance\pgf@xc-\pgf@xa
    \pgftransformshift{\pgfpoint{.5\pgf@xa+.5\pgf@xb}{\pgf@yb}}\pgftransformrotate{90}
    \pgfnode{rectangle}{center}{$\left.\vcenter{\hrule height\pgf@xc width0pt}\right\}$}{abovecurlybracket}{}
  }
}
\pgfdeclareshape{below curly bracket}{
  \inheritsavedanchors[from=rectangle]\inheritanchorborder[from=rectangle]
  \inheritanchor[from=rectangle]{center}
  \anchor{text}{
    \southwest
    \advance\pgf@y-12pt
  }
  \foregroundpath{
    \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
    \northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
    \pgf@xc=\pgf@xb \advance\pgf@xc-\pgf@xa
    \pgftransformshift{\pgfpoint{.5\pgf@xa+.5\pgf@xb}{\pgf@ya}}\pgftransformrotate{90}
    \pgfnode{rectangle}{center}{$\left\{\vcenter{\hrule height\pgf@xc width0pt}\right.$}{belowcurlybracket}{}
  }
}
\pgfdeclareshape{left square bracket}{
  \inheritsavedanchors[from=rectangle]\inheritanchorborder[from=rectangle]
  \inheritanchor[from=rectangle]{center}
  \foregroundpath{
    \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
    \northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
    \pgf@yc=\pgf@yb \advance\pgf@yc-\pgf@ya
    \pgftransformshift{\pgfpoint{\pgf@xa}{.5\pgf@ya+.5\pgf@yb}}
    \pgfnode{rectangle}{center}{$\left[\vcenter{\hrule height\pgf@yc width0pt}\right.$}{leftsquarebracket}{\pgfusepath{}}
  }
}
\pgfdeclareshape{right square bracket}{
  \inheritsavedanchors[from=rectangle]\inheritanchorborder[from=rectangle]
  \inheritanchor[from=rectangle]{center}
  \foregroundpath{
    \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
    \northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
    \pgf@yc=\pgf@yb \advance\pgf@yc-\pgf@ya
    \pgftransformshift{\pgfpoint{\pgf@xb}{.5\pgf@ya+.5\pgf@yb}}
    \pgfnode{rectangle}{center}{$\left.\vcenter{\hrule height\pgf@yc width0pt}\right]$}{rightsquarebracket}{\pgfusepath{}}
  }
}

\begin{tikzpicture}
    \matrix(A)[matrix of math nodes]{
        a_1 & a_2 & a_3 & aaaaaa_4 \\[12pt]
        b_1 & b_2 & bbbbbb_3 & b_4 \\
        c_1 & cccccc_2 & c_3 & c_4 \\
        dddddd_1 & d_2 & d_3 & d_4 \\
    };
    \node[magenta,left square bracket, inner sep=0,fit={(A-2-2)(A-3-2)(A-4-4)}]{};
    \node[magenta,right square bracket,inner sep=0,fit={(A-2-2)(A-3-2)(A-4-4)}]{};
    \node[magenta,above curly bracket, inner sep=0,fit={(leftsquarebracket)(rightsquarebracket)}]{This is B};
    \node[magenta,below curly bracket, inner sep=0,fit={(leftsquarebracket)(rightsquarebracket)}]{This is also B};
    \node[cyan,left square bracket, inner sep=0,fit={(A-1-4)(A-4-1)(belowcurlybracket)}]{};
    \node[cyan,right square bracket,inner sep=0,fit={(A-1-4)(A-4-1)(belowcurlybracket)}]{};
    \node[cyan,above curly bracket, inner sep=0,fit={(leftsquarebracket)(rightsquarebracket)}]{This is A};
\end{tikzpicture}

答案2

我注意到所有矩阵元素都相对于外部矩阵对齐,就好像括号和花括号覆盖在原始元素之上,使用预先存在的间隙。所以这正是我所做的。

在修订版中,我使用 Tikz 在角落设置标记并计算子矩阵的大小和位置。这并不精确,但只要没有任何特别大的矩阵条目就可以了。垂直间隙使用 \vphantom 代替 \rule,水平间隙为空。

过度支撑和不足支撑很简单,但我使用了一个数组来对齐它们上方和/或下方的文本。

子矩阵

\documentclass{article}
\usepackage{mathtools}
\usepackage{tikz}

\newlength{\tempwidth}
\newlength{\tempheight}

\newcommand{\Mark}[2]% #1 = node name, #2 = text
{\tikz[remember picture, overlay]%
{\node[anchor=base](#1){$#2$};}}

\begin{document}

\begin{equation*}
f_k(x) = \det 
\begin{array}{c}
\textrm{This is $A$}\\
\overbrace{%
\begin{bmatrix}
x& &a&b&c&\cdots&d& \\
\vphantom{\overbrace{\strut}} \\
e& &\Mark{NW}{x}&f&g&\cdots&h\\
i&&j&&&&\vdots\\
&&&&&&k\\
\vdots&&\vdots&&&x&j\\
l&&m&\cdots&n&o&\Mark{SE}{x}\\
\vphantom{\underbrace{\strut}}
\end{bmatrix}}
\end{array}
\end{equation*}

\begin{tikzpicture}[remember picture,overlay]
\pgfextracty{\tempheight}{\pgfpointdiff{\pgfpointanchor{SE}{south}}%
  {\pgfpointanchor{NW}{north}}}
\global\tempheight=\tempheight
\pgfextractx{\tempwidth}{\pgfpointdiff{\pgfpointanchor{NW}{west}}%
  {\pgfpointanchor{SE}{east}}}
\global\tempwidth=\tempwidth
\path (NW.north west) +(0.5\tempwidth,-0.5\tempheight) node{$\displaystyle
\begin{array}{c}
\textrm{This is $B$}\\
\overbrace{\underbrace{%
\begin{bmatrix}
\rule{0pt}{\tempheight}\rule{\tempwidth}{0pt}
\end{bmatrix}}}\\
\textrm{This is also B}
\end{array}$};
\end{tikzpicture}

\end{document}

答案3

与。{bNiceMatrix}nicematrix

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

$\begin{bNiceMatrix}[margin]
x & y   & z  & a+b+c & z \\[7mm]
x & y+y & 0  & t     & 0 \\
x & y   & 1  &       & 0 \\
x & y   & 2  &       & 0 \\
x & y   & 3  &       & 0 \\
x & y   & 4  &       & 0 \\
\CodeAfter
  \SubMatrix[{2-2}{last-last}]
  \OverBrace[yshift=2mm]{1-1}{1-last}{\text{This is } A}
  \OverBrace[yshift=1mm]{2-2}{last-last}{\text{This is } B \text{ too}}
  \UnderBrace[yshift=1mm]{2-2}{last-last}{\text{This is } B}
\end{bNiceMatrix}$

\end{document}

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

在此处输入图片描述

相关内容