6

How can I get brackets (say "normal parenthese math brackets") inside a TikZ-matrix around some blocks?
For example here at the marked places:

enter image description here

Is there a decoration or something?

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document} \begin{tikzpicture}[align=center] \matrix[matrix of nodes, row sep=5mm, column sep=3mm, ]{ a11 & a12 & a13 & a14 & a15 \ a21 & a22 & a23 & a24 & a25 \ a31 & a32 & a33 & a34 & a35 \ a41 & a42 & a43 & a44 & a45 \ a51 & a52 & a53 & a54 & a55 \ a61 & a62 & a63 & a64 & a65 \ a71 & a72 & a73 & a74 & a75 \ };

\end{tikzpicture} \end{document}

F. Pantigny
  • 40,250
cis
  • 8,073
  • 1
  • 16
  • 45

4 Answers4

9

For information, here is what you can do with {NiceMatrix} of nicematrix. That package uses PGF (which is a sublayer of Tikz) but does not load Tikz and does not use the library matrix of Tikz.

However, each cell is a PGF node that the user can use with standard Tiks instructions.

There is a command \SubMatrix to add brackets. The brackets are the standard brackets of the mathematical expressions of LaTeX.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\renewcommand{\arraystretch}{1.5} $\begin{NiceMatrix} a11 & a12 & a13 & a14 & a15 \ a21 & a22 & a23 & a24 & a25 \ a31 & a32 & a33 & a34 & a35 \ a41 & a42 & a43 & a44 & a45 \ a51 & a52 & a53 & a54 & a55 \ a61 & a62 & a63 & a64 & a65 \ a71 & a72 & a73 & a74 & a75 \ \CodeAfter [delimiters/color=blue] \SubMatrix({1-4}{2-5}) \SubMatrix({5-1}{7-3}) \end{NiceMatrix}$

\end{document}

You need several compilations because of the PGF nodes.

Output of the above code

F. Pantigny
  • 40,250
5

enter image description here

Parenthesis you can dear :-)

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document} \begin{tikzpicture} \matrix (m) [matrix of nodes, % <-- added matrix name row sep=3mm, column sep=3mm, ] { a11 & a12 & a13 & a14 & a15 \ a21 & a22 & a23 & a24 & a25 \ a31 & a32 & a33 & a34 & a35 \ a41 & a42 & a43 & a44 & a45 \ a51 & a52 & a53 & a54 & a55 \ a61 & a62 & a63 & a64 & a65 \ a71 & a72 & a73 & a74 & a75 \ }; \draw[thick, blue, rounded corners=2mm] ([xshift=+2mm] m-1-4.north west) -| (m-2-4.south west) -- ++ (+2mm,0) ([xshift=-2mm] m-1-5.north east) -| (m-2-5.south east) -- ++ (-2mm,0) % ([xshift=+2mm] m-5-1.north west) -| (m-7-1.south west) -- ++ (+2mm,0) ([xshift=-2mm] m-5-3.north east) -| (m-7-3.south east) -- ++ (-2mm,0); \end{tikzpicture} \end{document}

Zarko
  • 296,517
2

To get original amsmath-brackets, I worked with some tricks due to length-measurement of the calc-library:

enter image description here

\documentclass[border=5mm, varwidth]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{calc}
\pgfkeys{/tikz/savevalue/.code 2 args={\global\edef#1{#2}}}
%\pgfmathsetlengthmacro{\InnerSep}{1pt} % optional 1/2

\begin{document}

\section{amsmath-Brackets inside a TikZ-matrix} \begin{tikzpicture}[] \matrixmatrix of math nodes, nodes in empty cells, row sep=5mm, column sep=3mm, nodes={draw=none, % Test %inner sep=\InnerSep % optional 2/2 }, % Maybe useful: %every odd row/.style={anchor=base west},% <--- don't know, how %every even row/.style={anchor=base west},% to make this shorter { a11 a2& a12 & a13 & a14 & a15 \ a21 & a22 & a23 & a24 & a25 \ a31 & a32 & a33 & a34 & a35 \ a41 & a42 & a43 & a44 & a45 \ a51 & a52 & a53 & a54 & a55 \ a61 & a62 & a63 & a64 & a65 \ a71 & a72 & a73 & a74 & a75 \ };

% Brackets around A \coordinateAul at (m-5-1.north west); \coordinateAll at (m-7-1.south west); \coordinateAur at (m-5-3.north east); \coordinateAlr at (m-7-3.south east);

\path let \p1=($(Aul)-(All)$) in \pgfextra{ \pgfmathsetlengthmacro{\templength}{veclen(\x1,\y1)} } [savevalue={\u}{\templength}]; \pgfmathsetlengthmacro{\uHalf}{0.5*\u}

\node[] at ($(Aul)!0.5!(All)$) {$ \left( \rule[-\uHalf]{0mm}{\u} \right.$};

\node[] at ($(Aur)!0.5!(Alr)$) {$ \left. \rule[-\uHalf]{0mm}{\u} \right)$};

% Brackets around B \coordinateBul at (m-1-4.north west); \coordinateBll at (m-2-4.south west); \coordinateBur at (m-1-5.north east); \coordinateBlr at (m-2-5.south east);

\path let \p1=($(Bul)-(Bll)$) in \pgfextra{ \pgfmathsetlengthmacro{\templength}{veclen(\x1,\y1)} } [savevalue={\u}{\templength}]; \pgfmathsetlengthmacro{\uHalf}{0.5\u1}

\node[blue] at ($(Bul)!0.5!(Bll)$) {$ \left( \rule[-\uHalf]{0mm}{\u} \right.$};

\node[blue] at ($(Bur)!0.5!(Blr)$) {$ \left. \rule[-\uHalf]{0mm}{\u} \right)$}; \end{tikzpicture}

\section{For Comparison: Brackets at amsmath-Matrix} $\begin{pmatrix} a51 & a52 & a53 \[5mm] a61 & a62 & a63 \[5mm] a71 & a72 & a73 \end{pmatrix}$ \end{document}

cis
  • 8,073
  • 1
  • 16
  • 45
1

The matrix library of TikZ already comes with a tool to put delimiters around a \matrix. We can re-use that to put delimiteres around any nodes.


My first approach was to define a new macro

\newcommand*\delimAroundTikZCells[5][]{%
  \path[every delim around tikz cells/.try,#1,local bounding box=datc]
    (#2.north west)(#3.south east) [freeze local bounding box=datc]
    (datc) [late options={left delimiter=#4, right delimiter=#5}];}

that can be used as such (when the matrix is named a):

\delimAroundTikZCells{a-1-4}{a-2-5}()
\delimAroundTikZCells{a-5-1}{a-7-3}()

This commands puts a rectangular node around the range provided by #2 and #3 (this is similar to the fit key from the fit library) and then adds to delimiters #4 (left) and #5 (right) around this new pseudo-node.


Though, we can do this also as an option to the matrix it self with my second solution, the keys are

  • lr delim = <left><left options> and <right><right options> around <top left> to <bottom left> and similarly
  • ab delim.

The options are optionally. This also works without explicitly naming the matrix.

There are a lot of { and } involved because the \tikzlastnode macro gets overwritten by the way the late options work.


Since the nodes have some inner xseps (default .3333em) I've added a delim xshift and delim yshift key that can be used to correct the placement of delimiters.

Due to how these delimiters are set these options (as all styles like every [left|right|above|below] delimiter) need to be defined at an encompassing group, that could be the tikzpicture it self, a scope, \scoped (though this needs ampersand replacemnt) or simply

\path[delim xshift = .5em, lr delims = green] node[
  matrix of nodes,
  lr delim = ( and ) around 2-2 to 4-4] {
  <matrix content>
};

I've added a key delim options as a way to set options that apply to all delimiters that come after it and are added with the keys lr delim and ab delim.


Internally, the delimiters are real math delimiters done via

\left( <a box with the same height as the node> \right.

I've also added a more tabular/array like version of the TikZ \matrix for comparison.

Code

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset{
  delim options/.style={append after command={[#1]}},
  lr delims/.style={
    every delim around tikz cells/.append style={
      every left delimiter/.append style={#1},
      every right delimiter/.append style={#1}}},
  ab delims/.style={
    every delim around tikz cells/.append style={
      every above delimiter/.append style={#1},
      every below delimiter/.append style={#1}}},
  delim xshift/.style={
    every delim around tikz cells/.append style={
      every left delimiter/.append style={xshift={#1}},
      every right delimiter/.append style={xshift={-(#1)}}}},
  delim yshift/.style={
    every delim around tikz cells/.append style={
      every above delimiter/.append style={yshift={-(#1)}},
      every below delimiter/.append style={yshift={#1}}}},
  lr delim/.style args={#1#2 and #3#4 around #5 to #6}{% Left Right
    append after command={
      {{[local bounding box=@] (\tikzlastnode-#5.north west)(\tikzlastnode-#6.south east)}
      (@)[every delim around tikz cells/.try,
          every left delimiter/.append style={#2},
          every right delimiter/.append style={#4},
          late options={left delimiter={#1},right delimiter={#3}}]}[]}},
  ab delim/.style args={#1#2 and #3#4 around #5 to #6}{% Above Below
    append after command={
      {{[local bounding box=@] (\tikzlastnode-#5.north west)(\tikzlastnode-#6.south east)}
      (@)[every delim around tikz cells/.try,
          every above delimiter/.append style={#2},
          every below delimiter/.append style={#4},
          late options={above delimiter={#1},below delimiter={#3}}]}[]}},
}
\begin{document}

\begin{tikzpicture}[ delim yshift=.8ex, delim xshift=.5em, lr delims=blue] \matrix[ matrix of nodes, row sep=5mm, column sep=3mm, lr delim=( and ) around 1-4 to 2-5, lr delim=( and ) around 5-1 to 7-3, ab delim={[ red and ] green around 1-2 to 4-3}, ] (a) { a11 & a12 & a13 & a14 & a15 \ a21 & a22 & a23 & a24 & a25 \ a31 & a32 & a33 & a34 & a35 \ a41 & a42 & a43 & a44 & a45 \ a51 & a52 & a53 & a54 & a55 \ a61 & a62 & a63 & a64 & a65 \ a71 & a72 & a73 & a74 & a75 \ }; \end{tikzpicture}

\begin{tikzpicture} \matrix[ matrix of nodes, row sep=0mm, column sep=2\tabcolsep, inner sep=0pt, execute at begin node=\strut, delim options={delim xshift=.1em, lr delims=blue}, lr delim=( and ) around 1-4 to 2-5, lr delim=( and ) around 5-1 to 7-3] (a) { a11 & a12 & a13 & a14 & a15 \ a21 & a22 & a23 & a24 & a25 \ a31 & a32 & a33 & a34 & a35 \ a41 & a42 & a43 & a44 & a45 \ a51 & a52 & a53 & a54 & a55 \ a61 & a62 & a63 & a64 & a65 \ a71 & a72 & a73 & a74 & a75 \ }; \end{tikzpicture} \end{document}

Output

enter image description here

Qrrbrbirlbel
  • 119,821
  • Nice and very useful solution! +1! – Zarko Oct 04 '22 at 09:17
  • Sorry for late response. Unfortunately your edited MWE doesn't work. It gives me error ! Package pgfkeys Error: I do not know the key '/tikz/appendafter command', to which you passed '[delim xshift=.1em, lr delims=blue]', and I am going to ignor e it. Perhaps you misspelled it. I tested with recent MiKTeX. – Zarko Oct 04 '22 at 17:04
  • @Zarko Whoops. Yes, that was a typo in the fifth line. – Qrrbrbirlbel Oct 04 '22 at 17:06
  • Now works! Thank you very much. – Zarko Oct 04 '22 at 17:25