5

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.

grid (generated)

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

grid (drawn)

2
  • I guess placing the [ and ] in the end cells themselves is not aesthetically or functionally acceptable? Commented May 28, 2013 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
    Commented May 28, 2013 at 17:03

1 Answer 1

4

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}

enter image description here

3
  • 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
    Commented May 28, 2013 at 18:27
  • 1
    @joost Why didn't you say so? Voila! Commented May 28, 2013 at 18:39
  • I was only vaguely aware of that requirement myself, sorry. You're the best!
    – Joost
    Commented May 28, 2013 at 18:59

You must log in to answer this question.

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