在矩阵内部添加带有外部括号的彩色虚线

在矩阵内部添加带有外部括号的彩色虚线

这是我想要创建的矩阵类型:

在此处输入图片描述

因为我想在矩阵周围加上带标签的括号,所以我使用了这个问题的 TikZ 解决方案。为了得到虚线,我使用cheating dash这个问题并从中汲取灵感如何在矩阵中创建垂直和水平虚线?。我的问题是这种方法需要大量的手动调整。

在矩阵内画线的另一种方法来自如何在矩阵中添加虚线,其中有一个使用array和的解决方案arydshln

在此处输入图片描述

这会自动绘制它们,但目前我无法 (a) 更改颜色、(b) 向矩阵添加外部括号以及 (c) 虚线的末端被切断(这正是我cheating dash在上一个示例中使用的原因)。所有这些原因都表明 TikZ 方法是可行的。如果有更好的方法可以自动绘制线条,而无需手动指定起点和终点,那该有多好?

平均能量损失

\documentclass[12pt,a4paper]{article}

\usepackage{mathtools}
\usepackage{arydshln}

   % CODE BELOW FROM https://tex.stackexchange.com/a/1070/128068
% Load TikZ
\usepackage{tikz}
\usetikzlibrary{matrix,decorations.pathreplacing,calc}

% Set various styles for the matrices and braces. It might pay off to fiddle around with the values a little bit
\pgfkeys{tikz/mymatrixenv/.style={decoration=brace,every left delimiter/.style={xshift=3pt},every right delimiter/.style={xshift=-3pt}}}
\pgfkeys{tikz/mymatrix/.style={matrix of math nodes,left delimiter=[,right delimiter={]},inner sep=2pt,column sep=1em,row sep=0.5em,nodes={inner sep=0pt}}}
\pgfkeys{tikz/mymatrixbrace/.style={decorate,thick}}
\newcommand\mymatrixbraceoffseth{0.5em}
\newcommand\mymatrixbraceoffsetv{0.2em}


\newcommand*\mymatrixbraceright[4][m]{
    \draw[mymatrixbrace] ($(#1.north west)!(#1-#3-1.south west)!(#1.south west)-(\mymatrixbraceoffseth,0)$)
        -- node[left=2pt] {#4} 
        ($(#1.north west)!(#1-#2-1.north west)!(#1.south west)-(\mymatrixbraceoffseth,0)$);
}
\newcommand*\mymatrixbraceleft[4][m]{
    \draw[mymatrixbrace] ($(#1.north east)!(#1-#2-1.north east)!(#1.south east)+(\mymatrixbraceoffseth,0)$)
        -- node[right=2pt] {#4} 
        ($(#1.north east)!(#1-#3-1.south east)!(#1.south east)+(\mymatrixbraceoffseth,0)$);
}
\newcommand*\mymatrixbracetop[4][m]{
    \draw[mymatrixbrace] ($(#1.north west)!(#1-1-#2.north west)!(#1.north east)+(0,\mymatrixbraceoffsetv)$)
        -- node[above=2pt] {#4} 
        ($(#1.north west)!(#1-1-#3.north east)!(#1.north east)+(0,\mymatrixbraceoffsetv)$);
}
\newcommand*\mymatrixbracebottom[4][m]{
    \draw[mymatrixbrace] ($(#1.south west)!(#1-1-#3.south east)!(#1.south east)-(0,\mymatrixbraceoffsetv)$)
        -- node[below=2pt] {#4} 
        ($(#1.south west)!(#1-1-#2.south west)!(#1.south east)-(0,\mymatrixbraceoffsetv)$);
}

% CHEATING DASH FROM https://tex.stackexchange.com/a/133357/128068
\tikzset{
    cheating dash/.code args={on #1 off #2}{
        % Use csname so catcode of @ doesn't have do be changed.
        \csname tikz@addoption\endcsname{%
            \pgfgetpath\currentpath%
            \pgfprocessround{\currentpath}{\currentpath}%
            \csname pgf@decorate@parsesoftpath\endcsname{\currentpath}{\currentpath}%
            \pgfmathparse{\csname pgf@decorate@totalpathlength\endcsname-#1}\let\rest=\pgfmathresult%
            \pgfmathparse{#1+#2}\let\onoff=\pgfmathresult%
            \pgfmathparse{max(floor(\rest/\onoff), 1)}\let\nfullonoff=\pgfmathresult%
            \pgfmathparse{max((\rest-\onoff*\nfullonoff)/\nfullonoff+#2, #2)}\let\offexpand=\pgfmathresult%
            \pgfsetdash{{#1}{\offexpand}}{0pt}}%
    }
}

\begin{document}

\begin{equation}
\mathbf{X} = 
\begin{tikzpicture}[baseline=0cm,mymatrixenv]
    \matrix [mymatrix,inner sep=4pt,row sep=1em] (m)  
    {
    \frac{x}{y} + C &  m\omega^2 + U     \\ 
    0               &   1                \\
    1               & \frac{x}{y} + Dx^2 \\
    };

    % Dashed lines
    \draw [cheating dash=on 2pt off 2pt,red]
         ([xshift=1ex,yshift=1ex]m-1-1.north east) --++(-90:2.4cm);
    \draw [cheating dash=on 2pt off 2pt,red]
         ([xshift=-0.3ex,yshift=-0.9ex]m-1-1.south west) --++(0:3.3cm);

    % Braces     
    \mymatrixbraceright{1}{3}{$A$}
    \mymatrixbracetop{2}{2}{$B$}
\end{tikzpicture}
\end{equation}


\begingroup
\renewcommand*{\arraystretch}{1.75}
\begin{equation}
\mathbf{X} = 
\left[
    \begin{array}{c;{2pt/2pt}c}
     \frac{x}{y} + C &  m\omega^2 + U     \\ \hdashline[2pt/2pt]
    0               &   1                \\
    1               & \frac{x}{y} + Dx^2 \\
    \end{array}
\right]
\end{equation}
\endgroup

\end{document}

添加- 我应该提到,对于不同的矩阵,我会尝试各种设置tikz/mymatrix/.style,例如更改inner seprow sep。如果虚线的位置可以以某种方式与这些设置相关联,那就太理想了。这突出了为什么我当前的方法(以厘米为单位指定长度)并不理想。

答案1

为了避免使用明确的距离,我会使用以下代码。

\documentclass[12pt,a4paper]{article}
   % CODE BELOW FROM https://tex.stackexchange.com/a/1070/128068
% Load TikZ
\usepackage{tikz}
\usetikzlibrary{matrix,decorations.pathreplacing,calc}

% Set various styles for the matrices and braces. It might pay off to fiddle around with the values a little bit
\pgfkeys{tikz/mymatrixenv/.style={decoration=brace,every left delimiter/.style={xshift=3pt},every right delimiter/.style={xshift=-3pt}}}
\pgfkeys{tikz/mymatrix/.style={matrix of math nodes,left delimiter=[,right delimiter={]},inner sep=2pt,column sep=1em,row sep=0.5em,nodes={inner sep=0pt}}}
\pgfkeys{tikz/mymatrixbrace/.style={decorate,thick}}
\newcommand\mymatrixbraceoffseth{0.5em}
\newcommand\mymatrixbraceoffsetv{0.2em}


\newcommand*\mymatrixbraceright[4][m]{
    \draw[mymatrixbrace] ($(#1.north west)!(#1-#3-1.south west)!(#1.south west)-(\mymatrixbraceoffseth,0)$)
        -- node[left=2pt] {#4} 
        ($(#1.north west)!(#1-#2-1.north west)!(#1.south west)-(\mymatrixbraceoffseth,0)$);
}
\newcommand*\mymatrixbraceleft[4][m]{
    \draw[mymatrixbrace] ($(#1.north east)!(#1-#2-1.north east)!(#1.south east)+(\mymatrixbraceoffseth,0)$)
        -- node[right=2pt] {#4} 
        ($(#1.north east)!(#1-#3-1.south east)!(#1.south east)+(\mymatrixbraceoffseth,0)$);
}
\newcommand*\mymatrixbracetop[4][m]{
    \draw[mymatrixbrace] ($(#1.north west)!(#1-1-#2.north west)!(#1.north east)+(0,\mymatrixbraceoffsetv)$)
        -- node[above=2pt] {#4} 
        ($(#1.north west)!(#1-1-#3.north east)!(#1.north east)+(0,\mymatrixbraceoffsetv)$);
}
\newcommand*\mymatrixbracebottom[4][m]{
    \draw[mymatrixbrace] ($(#1.south west)!(#1-1-#3.south east)!(#1.south east)-(0,\mymatrixbraceoffsetv)$)
        -- node[below=2pt] {#4} 
        ($(#1.south west)!(#1-1-#2.south west)!(#1.south east)-(0,\mymatrixbraceoffsetv)$);
}

% CHEATING DASH FROM https://tex.stackexchange.com/a/133357/128068
\tikzset{
    cheating dash/.code args={on #1 off #2}{
        % Use csname so catcode of @ doesn't have do be changed.
        \csname tikz@addoption\endcsname{%
            \pgfgetpath\currentpath%
            \pgfprocessround{\currentpath}{\currentpath}%
            \csname pgf@decorate@parsesoftpath\endcsname{\currentpath}{\currentpath}%
            \pgfmathparse{\csname pgf@decorate@totalpathlength\endcsname-#1}\let\rest=\pgfmathresult%
            \pgfmathparse{#1+#2}\let\onoff=\pgfmathresult%
            \pgfmathparse{max(floor(\rest/\onoff), 1)}\let\nfullonoff=\pgfmathresult%
            \pgfmathparse{max((\rest-\onoff*\nfullonoff)/\nfullonoff+#2, #2)}\let\offexpand=\pgfmathresult%
            \pgfsetdash{{#1}{\offexpand}}{0pt}}%
    }
}

\begin{document}

\begin{equation}
\mathbf{X} = 
\begin{tikzpicture}[baseline=0cm,mymatrixenv]
    \matrix [mymatrix,inner sep=4pt,row sep=1em] (m)  
    {
    \frac{x}{y} + C &  m\omega^2 + U     \\ 
    0               &   1                \\
    1               & \frac{x}{y} + Dx^2 \\
    };
    \path (m-1-1.east) -- (m-1-2.west) coordinate[midway] (X)
    (m-1-1.south) -- (m-2-1.north) coordinate[midway] (Y);
    % Dashed lines
    \draw [cheating dash=on 2pt off 2pt,red]
         (X |- m.north) --   (X |- m.south);
    \draw [cheating dash=on 2pt off 2pt,red]
         (Y -| m.west) -- (Y -| m.east);

    % Braces     
    \mymatrixbraceright{1}{3}{$A$}
    \mymatrixbracetop{2}{2}{$B$}
\end{tikzpicture}
\end{equation}
\end{document}

在此处输入图片描述

编辑:使用您自己的建议(进行一些小的修改),代码可以简化为:

\documentclass[12pt,a4paper]{article}
   % CODE BELOW FROM https://tex.stackexchange.com/a/1070/128068
% Load TikZ
\usepackage{tikz}
\usetikzlibrary{matrix,decorations.pathreplacing,calc}

% Set various styles for the matrices and braces. It might pay off to fiddle around with the values a little bit
\pgfkeys{tikz/mymatrixenv/.style={decoration=brace,every left delimiter/.style={xshift=3pt},every right delimiter/.style={xshift=-3pt}}}
\pgfkeys{tikz/mymatrix/.style={matrix of math nodes,left delimiter=[,right delimiter={]},inner sep=2pt,column sep=1em,row sep=0.5em,nodes={inner sep=0pt}}}
\pgfkeys{tikz/mymatrixbrace/.style={decorate,thick}}
\newcommand\mymatrixbraceoffseth{0.5em}
\newcommand\mymatrixbraceoffsetv{0.2em}


\newcommand*\mymatrixbraceright[4][m]{
    \draw[mymatrixbrace] ($(#1.north west)!(#1-#3-1.south west)!(#1.south west)-(\mymatrixbraceoffseth,0)$)
        -- node[left=2pt] {#4} 
        ($(#1.north west)!(#1-#2-1.north west)!(#1.south west)-(\mymatrixbraceoffseth,0)$);
}
\newcommand*\mymatrixbraceleft[4][m]{
    \draw[mymatrixbrace] ($(#1.north east)!(#1-#2-1.north east)!(#1.south east)+(\mymatrixbraceoffseth,0)$)
        -- node[right=2pt] {#4} 
        ($(#1.north east)!(#1-#3-1.south east)!(#1.south east)+(\mymatrixbraceoffseth,0)$);
}
\newcommand*\mymatrixbracetop[4][m]{
    \draw[mymatrixbrace] ($(#1.north west)!(#1-1-#2.north west)!(#1.north east)+(0,\mymatrixbraceoffsetv)$)
        -- node[above=2pt] {#4} 
        ($(#1.north west)!(#1-1-#3.north east)!(#1.north east)+(0,\mymatrixbraceoffsetv)$);
}
\newcommand*\mymatrixbracebottom[4][m]{
    \draw[mymatrixbrace] ($(#1.south west)!(#1-1-#3.south east)!(#1.south east)-(0,\mymatrixbraceoffsetv)$)
        -- node[below=2pt] {#4} 
        ($(#1.south west)!(#1-1-#2.south west)!(#1.south east)-(0,\mymatrixbraceoffsetv)$);
}

% CHEATING DASH FROM https://tex.stackexchange.com/a/133357/128068
\tikzset{
    cheating dash/.code args={on #1 off #2}{
        % Use csname so catcode of @ doesn't have do be changed.
        \csname tikz@addoption\endcsname{%
            \pgfgetpath\currentpath%
            \pgfprocessround{\currentpath}{\currentpath}%
            \csname pgf@decorate@parsesoftpath\endcsname{\currentpath}{\currentpath}%
            \pgfmathparse{\csname pgf@decorate@totalpathlength\endcsname-#1}\let\rest=\pgfmathresult%
            \pgfmathparse{#1+#2}\let\onoff=\pgfmathresult%
            \pgfmathparse{max(floor(\rest/\onoff), 1)}\let\nfullonoff=\pgfmathresult%
            \pgfmathparse{max((\rest-\onoff*\nfullonoff)/\nfullonoff+#2, #2)}\let\offexpand=\pgfmathresult%
            \pgfsetdash{{#1}{\offexpand}}{0pt}}%
    }
}


\newcommand*\mymatrixdashedlineH[4][]{ 
\path (#2-#3-1.south) -- (#2-#4-1.north)
coordinate [midway] (Y); \draw [#1] (Y -| #2.west) -- (Y -| #2.east);}


\newcommand*\mymatrixdashedlineV[4][]{ \path (#2-1-#3.east) -- (#2-1-#4.west)
coordinate [midway] (X); \draw [#1] (X |- #2.north) -- (X |- #2.south);}

\begin{document}

\begin{equation}
\mathbf{X} = 
\begin{tikzpicture}[baseline=0cm,mymatrixenv]
    \matrix [mymatrix,inner sep=4pt,row sep=1em] (m)  
    {
    \frac{x}{y} + C &  m\omega^2 + U     \\ 
    0               &   1                \\
    1               & \frac{x}{y} + Dx^2 \\
    };
    \path (m-1-1.east) -- (m-1-2.west) coordinate[midway] (X)
    (m-1-1.south) -- (m-2-1.north) coordinate[midway] (Y);
    % Dashed lines
    \mymatrixdashedlineV[red,dashed]{m}{1}{2}
    \mymatrixdashedlineH[red,dashed]{m}{1}{2}
    % Braces     
    \mymatrixbraceright{1}{3}{$A$}
    \mymatrixbracetop{2}{2}{$B$}
\end{tikzpicture}
\end{equation}
\end{document}

编辑:只是为了好玩:一些 Ti您的好建议的 Zy 版本。(笔记:我并不是说这是一条安全路径,因为我正在使用\pgfextra。这是真的只是为了好玩。)

\documentclass[12pt,a4paper]{article}
   % CODE BELOW FROM https://tex.stackexchange.com/a/1070/128068
% Load TikZ
\usepackage{tikz}
\usetikzlibrary{matrix,decorations.pathreplacing,calc}

% Set various styles for the matrices and braces. It might pay off to fiddle around with the values a little bit
\pgfkeys{tikz/mymatrixenv/.style={decoration=brace,every left delimiter/.style={xshift=3pt},every right delimiter/.style={xshift=-3pt}}}
\pgfkeys{tikz/mymatrix/.style={matrix of math nodes,left delimiter=[,right
delimiter={]},inner sep=2pt,column sep=1em,row sep=0.5em,nodes={inner
sep=0pt}}}
\pgfkeys{tikz/mymatrixbrace/.style={decorate,thick}}
\newcommand\mymatrixbraceoffseth{0.5em}
\newcommand\mymatrixbraceoffsetv{0.2em}


\newcommand*\mymatrixbraceright[4][m]{
    \draw[mymatrixbrace] ($(#1.north west)!(#1-#3-1.south west)!(#1.south west)-(\mymatrixbraceoffseth,0)$)
        -- node[left=2pt] {#4} 
        ($(#1.north west)!(#1-#2-1.north west)!(#1.south west)-(\mymatrixbraceoffseth,0)$);
}
\newcommand*\mymatrixbraceleft[4][m]{
    \draw[mymatrixbrace] ($(#1.north east)!(#1-#2-1.north east)!(#1.south east)+(\mymatrixbraceoffseth,0)$)
        -- node[right=2pt] {#4} 
        ($(#1.north east)!(#1-#3-1.south east)!(#1.south east)+(\mymatrixbraceoffseth,0)$);
}
\newcommand*\mymatrixbracetop[4][m]{
    \draw[mymatrixbrace] ($(#1.north west)!(#1-1-#2.north west)!(#1.north east)+(0,\mymatrixbraceoffsetv)$)
        -- node[above=2pt] {#4} 
        ($(#1.north west)!(#1-1-#3.north east)!(#1.north east)+(0,\mymatrixbraceoffsetv)$);
}
\newcommand*\mymatrixbracebottom[4][m]{
    \draw[mymatrixbrace] ($(#1.south west)!(#1-1-#3.south east)!(#1.south east)-(0,\mymatrixbraceoffsetv)$)
        -- node[below=2pt] {#4} 
        ($(#1.south west)!(#1-1-#2.south west)!(#1.south east)-(0,\mymatrixbraceoffsetv)$);
}

% CHEATING DASH FROM https://tex.stackexchange.com/a/133357/128068
\tikzset{
    cheating dash/.code args={on #1 off #2}{
        % Use csname so catcode of @ doesn't have do be changed.
        \csname tikz@addoption\endcsname{%
            \pgfgetpath\currentpath%
            \pgfprocessround{\currentpath}{\currentpath}%
            \csname pgf@decorate@parsesoftpath\endcsname{\currentpath}{\currentpath}%
            \pgfmathparse{\csname pgf@decorate@totalpathlength\endcsname-#1}\let\rest=\pgfmathresult%
            \pgfmathparse{#1+#2}\let\onoff=\pgfmathresult%
            \pgfmathparse{max(floor(\rest/\onoff), 1)}\let\nfullonoff=\pgfmathresult%
            \pgfmathparse{max((\rest-\onoff*\nfullonoff)/\nfullonoff+#2, #2)}\let\offexpand=\pgfmathresult%
            \pgfsetdash{{#1}{\offexpand}}{0pt}}%
    }
}


\tikzset{
    vertical line/.style args={with style #1 right of column #2 in
    row #3}{
        append after command={
            \pgfextra{ \pgfmathparse{int(#2+1)} 
            \path (\tikzlastnode-#3-#2.east) -- (\tikzlastnode-#3-\pgfmathresult.west) coordinate [midway] (MiloX); 
            \draw [#1] (MiloX |- \tikzlastnode.north) -- (MiloX |- \tikzlastnode.south);
            }
        }
    },
    horizontal line/.style args={with style #1 below row #2 in 
    column #3}{append after command={\pgfextra{ \pgfmathparse{int(#2+1)}
    \path (\tikzlastnode-#2-#3.south) -- (\tikzlastnode-\pgfmathresult-#3.north) coordinate [midway] (MiloY); 
    \draw [#1] (MiloY -| \tikzlastnode.west) -- (MiloY -| \tikzlastnode.east);
    }}
    }
}


\begin{document}

\begin{equation}
\mathbf{X} = 
\begin{tikzpicture}[baseline=0cm,mymatrixenv]
    \matrix [mymatrix,inner sep=4pt,row sep=1em,
    vertical line={with style {dashed,red} right of column 1 in row 1},
    horizontal line={with style {dashed,red} below row 2 in column 1}] (m)  
    {
    \frac{x}{y} + C &  m\omega^2 + U     \\ 
    0               &   1                \\
    1               & \frac{x}{y} + Dx^2 \\
    };
    \path (m-1-1.east) -- (m-1-2.west) coordinate[midway] (X)
    (m-1-1.south) -- (m-2-1.north) coordinate[midway] (Y);
    % Braces     
    \mymatrixbraceright{1}{3}{$A$}
    \mymatrixbracetop{2}{2}{$B$}
\end{tikzpicture}
\end{equation}
\end{document}

答案2

具有(≥ 6.11 {bNiceArray}nicematrix截至 2022-06-16)。

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

\NiceMatrixOptions
  {
    custom-line = 
     {
       command = dashedline , 
       letter = : , 
       tikz = { dashed, red } , 
       total-width = \pgflinewidth
     }
  }

\begin{document}

\[ 
\renewcommand{\arraystretch}{1.4}
\mathbf{X} =\qquad 
\begin{bNiceArray}{c:c}[margin]
  \frac{x}{y}+C & m\omega^2+U \\
  \dashedline
  0             & 1 \\
  1             & \frac{x}{y}+Dx^2 \\
\CodeAfter
  \OverBrace[shorten,yshift=3pt]{1-2}{1-2}{B}
  \SubMatrix{\{}{1-1}{3-1}{.}[xshift=3mm,extra-height=-1mm,name=mybrace]
  \tikz \node [left] at (mybrace-left) {$A$} ; 
\end{bNiceArray}
\]

\end{document}

您需要多次编译(因为 PGF/Tikz 节点)。

上述代码的输出

相关内容