1

I know how to control the horizontal spacing of the brackets using every left delimiter/.style={xshift=1pt} and every right delimiter/.style={xshift=-1pt}. But how can I adjust the vertical length of the brackets? I want them to be larger than the bounding box of the matrix.

enter image description here

\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

One could perhaps do a small modification of the macro that actually draws the delimiters. See the block right after \makeatletter in the code below. Only two lines are changed, see comments in the code.

enter image description here

\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}

Old answer

Add outer ysep=1mm (or some other value) to the \matrix options. This moves the anchors

enter image description here

\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}
  • +1 Thanks, this works. But, this changes the location of m.north and m.south? Is that right? For example, if you try \draw (m.north) -- (m.south); with and without setting outer ysep to some value, they are different. But the size of the red bounding box remains the same. I want the size of the red bounding box to remain the same, that's fine. But I want to draw a line from the north and south edges of that red bounding box, do you know what coordinates I should be using instead of m.north and m.south? – Milo Jul 25 '18 at 23:10
  • FYI, something like \draw ([yshift=-1mm]m.north) -- ([yshift=1mm]m.south); works of course. But that's not an option for me as it depends on knowing the value of outer ysep which doesn't work with what I'm trying to do in Automatically find which nodes are closest, to aid drawing lines within a TikZ matrix. – Milo Jul 25 '18 at 23:28
  • @Milo Hm, right. I did figure out a bit of a hack, see updated answer. Didn't test anything beyond this example. – Torbjørn T. Jul 26 '18 at 0:12
  • Interesting! I must confess this is all well above my pay grade. But I did test it out a bit, seems like sometimes the distance from the top of the bracket to the top of red matrix bounding box, and the distance from the bottom of the bracket to the bottom of the bounding box are different? This happens when you play with inner sep and row sep. For example, try inner sep=2pt, row sep=1em. It's a small difference, I'll admit, but nonetheless there. Don't know if that's something to be concerned about? – Milo Jul 26 '18 at 0:55
1

A study using delimitations for nodes and the library fit: the length is controlled using inner xsep and inner ysep in fit nodes.

RESULT: enter image description here

MWE:

\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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.