# Grouping cells in a tabular environment

I'm drawing a grid structure using a tabular environment. Some cells are empty, some cells are filled and some cells contain a symbol. Nothing spectacular there.

\documentclass{article}

\usepackage[table]{xcolor}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\setlength{\extrarowheight}{3pt} %for superscripts in tabular

\begin{document}

\newcommand{\xx}{\cellcolor[gray]{0.5}}

\begin{table}[h]
\centering
{\tt
\begin{tabular}{ *{11}{| C{0.4cm}}| }
\hline
N   &     & \xx &     & N   &     & \xx &     &     &     &     \\ \hline
& N   &     & N   &     & N   &     & N   &     & \xx &     \\ \hline
&     &     &     &     &     & \xx &     &     & \xx & \xx \\ \hline
\xx &     & \xx &     &     &     &     & \xx &     &     &     \\ \hline
&     & \xx &     &     &     & \xx &     &     &     &     \\ \hline
& \xx &     &     &     & \xx &     &     &     &     &     \\ \hline
&     &     &     &     &     & \xx &     &     &     & \xx \\ \hline
&     &     & \xx & \xx & \xx &     &     &     & \xx &     \\ \hline
\xx &     &     &     &     &     &     &     &     & \xx &     \\ \hline
\xx &     &     &     &     &     &     & \xx &     & \xx & \xx \\ \hline
& \xx &     &     &     &     & \xx & \xx & \xx & \xx & \xx \\ \hline
\end{tabular}}
\end{table}

\end{document}


What I'm trying to do, though, is add brackets to group the empty cells in groups of four. I run into a problem when I simply add [ and ] to the contents of the cells: the content does no longer fit the cells and it wraps to a line slightly below the original line (in the process expanding the cell vertically), as the width is set. The bracket would also be centered, as that is default behaviour for my cells.

Does anyone have any suggestion how I can mimic the behaviour as drawn in the following picture? Perhaps some sort of overlay that does not influence the drawing of the table itself? Note that I have no problem with manually inserting the [ and ] in the right places, so there's no need for automatically counting four empty fields.

-
I guess placing the [ and ] in the end cells themselves is not aesthetically or functionally acceptable? –  Steven B. Segletes May 28 '13 at 16:52
Yeah, it causes two issues: the brackets are centered, but the cells also tend to overflow and cause the content to wrap (when brackets are combined with cells with content - max. 3 characters). I'm open to it if it works though. Perhaps I can overcome the issues on individual basis.. –  Joost May 28 '13 at 17:03

Your original and my proposed figures shown. I'm not sure if it is acceptable to place the braces in the cells. EDITED to revise bracket size and placement. RE-EDITED to allow optional content in cells with brackets.

\documentclass{article}

\usepackage[table]{xcolor}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\setlength{\extrarowheight}{3pt} %for superscripts in tabular
\usepackage{scalerel}

\begin{document}

\newcommand{\xx}{\cellcolor[gray]{0.5}}

\begin{table}[h]
\centering
{\tt
\begin{tabular}{ *{11}{| C{0.4cm}}| }
\hline
N   &     & \xx &     & N   &     & \xx &     &     &     &     \\ \hline
& N   &     & N   &     & N   &     & N   &     & \xx &     \\ \hline
&     &     &     &     &     & \xx &     &     & \xx & \xx \\ \hline
\xx &     & \xx &     &     &     &     & \xx &     &     &     \\ \hline
&     & \xx &     &     &     & \xx &     &     &     &     \\ \hline
& \xx &     &     &     & \xx &     &     &     &     &     \\ \hline
&     &     &     &     &     & \xx &     &     &     & \xx \\ \hline
&     &     & \xx & \xx & \xx &     &     &     & \xx &     \\ \hline
\xx &     &     &     &     &     &     &     &     & \xx &     \\ \hline
\xx &     &     &     &     &     &     & \xx &     & \xx & \xx \\ \hline
& \xx &     &     &     &     & \xx & \xx & \xx & \xx & \xx \\ \hline
\end{tabular}}
\end{table}

\newcommand\lb[1][]{\raisebox{.3ex}{\scaleto[.7ex]{[}{3ex}\makebox[4.45ex]{#1}}\hfill}
\newcommand\rb[1][]{\hfill\makebox[4.4ex]{#1}\raisebox{.3ex}{\scaleto[.7ex]{]}{3ex}}}

\begin{table}[h]
\centering
{\tt
\tabcolsep=0pt
\begin{tabular}{ *{11}{| C{0.82cm}}| }
\hline
N   &     & \xx &     & N   &     & \xx & \lb &     &     & \rb \\ \hline
& N   &     & N   &     & N   &     & N   &     & \xx &     \\ \hline
\lb[a]  &     &     &\rb[b2]&   &     & \xx &     &     & \xx & \xx \\ \hline
\xx &     & \xx &     &     &     &     & \xx &     &     &     \\ \hline
&     & \xx &     &     &     & \xx &     &     &     &     \\ \hline
& \xx &     &     &     & \xx &     &     &     &     &     \\ \hline
&     &     &     &     &     & \xx &     &     &     & \xx \\ \hline
&     &     & \xx & \xx & \xx &     &     &     & \xx &     \\ \hline
\xx &     &     &     &     &     &     &     &     & \xx &     \\ \hline
\xx &     &     &     &     &     &     & \xx &     & \xx & \xx \\ \hline
& \xx &     &     &     &     & \xx & \xx & \xx & \xx & \xx \\ \hline
\end{tabular}}
\end{table}

\end{document}


-
Beautiful. Exactly the right answer to my question. It is a bit of a pity that adding a character to a field with a bracket puts the character on a new line - I guess I'll get that right with some case-by-case fiddling. Thanks! –  Joost May 28 '13 at 18:27
@joost Why didn't you say so? Voila! –  Steven B. Segletes May 28 '13 at 18:39
I was only vaguely aware of that requirement myself, sorry. You're the best! –  Joost May 28 '13 at 18:59