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

! 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