旧答案

旧答案

我知道如何使用every left delimiter/.style={xshift=1pt}和控制括号的水平间距every right delimiter/.style={xshift=-1pt}。但是如何调整括号的垂直长度?我希望它们大于矩阵的边界框。

在此处输入图片描述

\documentclass[margin=0.5cm]{standalone}
\usepackage{mathtools}

% CODE BELOW FROM https://tex.stackexchange.com/a/1070/128068
\usepackage{tikz}
\usetikzlibrary{matrix,decorations.pathreplacing,calc}
%Set various styles for the matrices and braces.
\pgfkeys{tikz/mymatrixenv/.style={decoration=brace,every left delimiter/.style={xshift=1pt},every right delimiter/.style={xshift=-1pt}}}
\pgfkeys{tikz/mymatrix/.style={matrix of math nodes,left delimiter=[,right delimiter={]},inner sep=4pt,column sep=1em,row sep=0.5em,nodes={inner sep=0pt}}}
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

\begin{document}

\begin{tikzpicture}[baseline=0cm,mymatrixenv]
    \matrix [mymatrix,inner sep=0pt,row sep=0em,column sep=0em,nodes in empty cells,nodes=draw,draw=red] (m)  
    {
   \dfrac{C}{D}  & 2 & 3  \\ 
    1   &   2   & \dfrac{A}{B}  \\
    1  & \dfrac{A}{B} + \dfrac{A}{B} & 3 \\
    };
\end{tikzpicture}

\end{document}

答案1

\makeatletter也许可以对实际绘制分隔符的宏进行一些小修改。请参见下面代码中的块。只有两行更改,请参见代码中的注释。

在此处输入图片描述

\documentclass[margin=0.5cm]{standalone}
\usepackage{mathtools}

% CODE BELOW FROM https://tex.stackexchange.com/a/1070/128068
\usepackage{tikz}
\usetikzlibrary{matrix,decorations.pathreplacing}
%Set various styles for the matrices and braces.
\tikzset{
  mymatrixenv/.style={decoration=brace,every left delimiter/.style={xshift=1pt},every right delimiter/.style={xshift=-1pt}},
  mymatrix/.style={matrix of math nodes,left delimiter=[,right delimiter={]},inner sep=4pt,column sep=1em,row sep=0.5em,nodes={inner sep=0pt}}
}
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


% code from tikzlibrarymatrix.code.tex
\makeatletter
\def\tikz@delimiter#1#2#3#4#5#6#7#8{%
  \bgroup
    \pgfextra{\let\tikz@save@last@fig@name=\tikz@last@fig@name}%
    node[outer sep=0pt,inner sep=0pt,draw=none,fill=none,
     yshift=0.1#8, % <-- added this
     anchor=#1,at=(\tikz@last@fig@name.#2),#3]
    {%
      {\nullfont\pgf@process{\pgfpointdiff{\pgfpointanchor{\tikz@last@fig@name}{#4}}{\pgfpointanchor{\tikz@last@fig@name}{#5}}}}%
      % changed two occurrences of 0.5#8 to 0.55#8 in this line:
      $\left#6\vcenter{\hrule height .55#8 depth .55#8 width0pt}\right#7$%
    }
    \pgfextra{\global\let\tikz@last@fig@name=\tikz@save@last@fig@name}%
  \egroup%
}
\makeatother


\begin{document}

\begin{tikzpicture}[baseline=0cm,mymatrixenv]
    \matrix [mymatrix,inner sep=0pt,
             row sep=0em,column sep=0em,
             nodes in empty cells,nodes=draw,draw=red] (m)  
    {
   \dfrac{C}{D}  & 2 & 3  \\ 
    1   &   2   & \dfrac{A}{B}  \\
    1  & \dfrac{A}{B} + \dfrac{A}{B} & 3 \\
    };

\draw [blue,thick] (m.north) -- (m.south);
\end{tikzpicture}

\end{document}

旧答案

向选项添加outer ysep=1mm(或其他值)\matrix。这会移动锚点

在此处输入图片描述

\documentclass[margin=0.5cm]{standalone}
\usepackage{mathtools}

% CODE BELOW FROM https://tex.stackexchange.com/a/1070/128068
\usepackage{tikz}
\usetikzlibrary{matrix,decorations.pathreplacing}
%Set various styles for the matrices and braces.
\tikzset{
  mymatrixenv/.style={decoration=brace,every left delimiter/.style={xshift=1pt},every right delimiter/.style={xshift=-1pt}},
  mymatrix/.style={matrix of math nodes,left delimiter=[,right delimiter={]},inner sep=4pt,column sep=1em,row sep=0.5em,nodes={inner sep=0pt}}
}
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

\begin{document}

\begin{tikzpicture}[baseline=0cm,mymatrixenv]
    \matrix [mymatrix,inner sep=0pt,
             outer ysep=1mm, % <-- added this
             row sep=0em,column sep=0em,nodes in empty cells,nodes=draw,draw=red] (m)  
    {
   \dfrac{C}{D}  & 2 & 3  \\ 
    1   &   2   & \dfrac{A}{B}  \\
    1  & \dfrac{A}{B} + \dfrac{A}{B} & 3 \\
    };
\end{tikzpicture}

\end{document}

答案2

使用节点和库拟合的界定进行的研究:使用inner xsepinner ysep拟合节点来控制长度。

结果: 在此处输入图片描述

梅威瑟:

\documentclass[margin=0.5cm]{standalone}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{matrix,fit,arrows.meta}
\begin{document}
    \begin{tikzpicture}[
        %Global environment config.
        %baseline=0cm, %Nice but I think is usefull only when tikzpicture  
        %Environment styles declarations
        Matrix/.style={
            matrix of math nodes,
            nodes in empty cells,
            column sep=1em,
            row sep=1em,
            nodes={inner sep=0pt}
            },
        Brackets/.style={
            left delimiter={[},
            right delimiter={]}
            }
    ]
    %Start drawing the thing.
    \matrix [Matrix,draw=red,nodes={draw,blue},inner sep=0pt] at (0,0)(M1){
        %Matrix contents
        \dfrac{C}{D}    &   2                       &   3           \\ 
        1           &   2                       &   \dfrac{A}{B}    \\
        1           &   \dfrac{A}{B}+\dfrac{A}{B}   &   3           \\
    };

    \matrix [
        Matrix,
        draw=red,
        draw opacity =0.2,
        nodes={
            draw,
            blue,
            draw opacity=0.2,
        }
        ,inner sep=0pt] at (5,0)(M2){
        %Matrix contents
                    &   1                       &   2                           &   3           \\
        A           &   \dfrac{C}{D}                &   2                           &   3           \\ 
        B           &   1                       &   2                           &   \dfrac{A}{B}    \\
        C           &   1                       &   \dfrac{A}{B}+\dfrac{A}{B}       &   3           \\  
    };
    \node[%Nearest brace
        Brackets,
        fit=(M1),%Arround matrix M1
        inner sep=0pt,
    ](M1-B){};
    \node[%Nested brace M1
        Brackets,
        fit=(M1-B),%Arround Nearest brace of M1
        inner xsep=10pt, %Inner separation in x
        inner ysep=5pt, %Inner separation in y
    ](M1-B1){};

    \node[%Internal braces 
        Brackets,
        draw,
        draw opacity=0.3,
        dashed,
        fit=(M2-2-2)(M2-3-4)(M2-4-3), % Nodes in matrix with greater nodes
        inner xsep=-2pt,
    ](M2-B){};
    \node[%Another internal brace
        Brackets,
        fit=(M2-2-2)(M2-3-3),
        inner xsep=-4pt,
        inner ysep=1pt,
    ](M2-B2){};
    \draw[-Stealth,shorten > = 4pt, shorten <=-6pt] (M1-B1) -- (M2-B.165);
    \end{tikzpicture}

\end{document}

相关内容