2

My question is pretty similar to this one with the main difference that I need to create an empty array that has multiple columns merged together as in the following picture

enter image description here

How can I merge the columns and add the underbrace?

Edit: I think I expressed my request in a wrong way

My Idea is to create an array as you can see in the picture.
To do that I read some previous requests in this forum, and I took inspiration in order to do wat I wanted.
The example that I linked, get an output that looks like this: enter image description here

The main difference with my request is that in my example there are multiple cells merged together.
Something like this:
enter image description here

The code for the first photo is this:

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{matrix,backgrounds}

\begin{document}
\begin{tikzpicture}[font=\ttfamily,
array/.style={matrix of nodes,nodes={draw, minimum size=7mm, fill=green!30},column sep=-\pgflinewidth, row sep=0.5mm, nodes in empty cells,
row 1/.style={nodes={draw=none, fill=none, minimum size=5mm}},
row 1 column 1/.style={nodes={draw}}}]

\matrix[array] (array) {
0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9\\
  &   &   &   &   &   &   &   &   &  \\};
\node[draw, fill=gray, minimum size=4mm] at (array-2-9) (box) {};

\begin{scope}[on background layer]
\fill[green!10] (array-1-1.north west) rectangle (array-1-10.south east);
\end{scope}

\draw[<->]([yshift=-3mm]array-2-1.south west) -- node[below] {Array length is 10} ([yshift=-3mm]array-2-10.south east);

\draw (array-1-1.north)--++(90:3mm) node [above] (first) {First index};
\draw (array-1-10.east)--++(0:3mm) node [right]{Indices};
\node [align=center, anchor=south] at (array-2-9.north west|-first.south) (8) {Element\\ (at index 8)};
\draw (8)--(box);
%
\end{tikzpicture}
\end{document}
2

1 Answer 1

4

You can define styles that add frames around a row or a single cell.

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{matrix,backgrounds}
\usetikzlibrary{decorations.pathreplacing,calligraphy}
\tikzset{matrix row frame/.style={execute at end matrix={%
\path[matrix frame] (\tikzmatrixname-#1-1.south west)
rectangle
(\tikzmatrixname-#1-\the\pgfmatrixcurrentcolumn.north east);}},
matrix cell frame/.style args={#1-#2}{execute at end matrix={%
\path[matrix frame] (\tikzmatrixname-#1-#2.south west)
rectangle (\tikzmatrixname-#1-#2.north east);}},
matrix frame/.style={draw}
}
\begin{document}
\begin{tikzpicture}[font=\ttfamily,
array/.style={matrix of nodes,
column sep=-\pgflinewidth, row sep=0.5mm, nodes in empty cells,
row 1/.style={nodes={draw=none, minimum size=5mm}},
row 2/.style={nodes={fill=green!30, minimum size=7mm}},
row 1 column 1/.style={nodes={draw}}}]

\matrix[array,matrix row frame=2,matrix cell frame=2-6] (array) {
0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9\\
  &   &   &   &   &   &   &   &   &  \\};
\node[draw, fill=gray, minimum size=4mm] at (array-2-9) (box) {};
\draw[decorate,decoration={calligraphic brace,raise=0.2ex},thick] 
  (array.south-|array-2-5.east) --  node[midway,below=1ex,font=\sffamily]{ORDINATA} 
  (array.south-|array-2-1.west);

\begin{scope}[on background layer]
\fill[green!10] (array-1-1.north west) rectangle (array-1-10.south east);
\end{scope}

\draw[<->]([yshift=-13mm]array-2-1.south west) -- node[below] {Array length is
10} ([yshift=-13mm]array-2-10.south east);

\draw (array-1-1.north)--++(90:3mm) node [above] (first) {First index};
\draw (array-1-10.east)--++(0:3mm) node [right]{Indices};
\node [align=center, anchor=south] at (array-2-9.north west|-first.south) (8) {Element\\ (at index 8)};
\draw (8)--(box);
%
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .