8

I want to use the column type described in this answer to hide certain columns in a table. The table occurs multiple times throughout the document, always with different columns hidden. Some of the cells are highlighted using the colortbl package, and this is what appears to cause a problem:

problem

It appears that the colored cells are somehow "leaking" from the hidden cells into the following cells. What can I do to prevent this? I want the background color of the hidden cells disappear completely - I do not want it to appear even partially in the following cells.

MWE:

\documentclass{article}
\usepackage{array}
\usepackage{colortbl}

\newcommand{\gc}{\cellcolor[gray]{.7}}
\newcolumntype{H}{>{\setbox0=\hbox\bgroup}c<{\egroup}@{}}

\newcommand{\tablebody}{%
      foo &     bar & \gc boo &     baz &     faa \\
      bar & \gc boo &     baz & \gc faa &     foo \\
  \gc boo &     baz &     faa &     foo & \gc bar \\
      baz & \gc faa &     foo & \gc bar &     boo \\
      faa &     foo & \gc bar &     boo &     baz %
}

\begin{document}

\section*{all columns visible}

\begin{tabular}{ccccc}
  \tablebody
\end{tabular}

\section*{center column hidden}

\begin{tabular}{ccHcc}
  \tablebody
\end{tabular}

\section*{middle columns hidden}

\begin{tabular}{cHcHc}
  \tablebody
\end{tabular}

\section*{outer columns hidden}

\begin{tabular}{HcccH}
  \tablebody
\end{tabular}

\end{document}
vwegert
  • 2,875
  • So, if I understand correctly, the issue isn't that columns are disappearing (i.e., that is purposeful), but that when you attempt to hide them, they leave behind residual color? Is that a correct understanding? – Steven B. Segletes Nov 06 '13 at 19:09
  • @StevenB.Segletes: Exactly. – vwegert Nov 06 '13 at 19:16
  • Setting \tabcolsep to 0pt eliminates it, which is a start, but then you need to figure out a way to get the horizontal buffer space back in. – Steven B. Segletes Nov 06 '13 at 19:52

2 Answers2

7

enter image description here

\documentclass{article}
\usepackage{array}
\usepackage{colortbl}
\makeatletter
\def\zz{\aftergroup\zzz}
\def\zzz\begingroup#1\endgroup
    {\global\let\CT@do@color\relax\global\let\CT@cell@color\relax}
\newcommand{\gc}{\cellcolor[gray]{.7}}
\newcolumntype{H}
   {>{\setbox0=\hbox\bgroup\bgroup}c<{\egroup\egroup\aftergroup\aftergroup\aftergroup\zz}@{}}

\newcommand{\tablebody}{%
      foo &     bar & \gc boo &     baz &     faa \\
      bar & \gc boo &     baz & \gc faa &     foo \\
  \gc boo &     baz &     faa &     foo & \gc bar \\
      baz & \gc faa &     foo & \gc bar &     boo \\
      faa &     foo & \gc bar &     boo &     baz %
}

\begin{document}

\section*{all columns visible}

\begin{tabular}{ccccc}
  \tablebody
\end{tabular}

\section*{center column hidden}

\begin{tabular}{ccHcc}
  \tablebody
\end{tabular}

\section*{middle columns hidden}

\begin{tabular}{cHcHc}
  \tablebody
\end{tabular}

\section*{outer columns hidden}

\begin{tabular}{HcccH}
  \tablebody
\end{tabular}

\end{document}
David Carlisle
  • 757,742
  • I probably didn't succeed in making my aim clear enough. I want the background color of the hidden cells disappear completely, not stretched out across the following (totally unrelated) cells. – vwegert Nov 06 '13 at 20:15
  • @vwegert that was the intention here;-) I had to dash out for a bit just as I was posting, I'll have a look what this does. – David Carlisle Nov 06 '13 at 20:54
  • @vwege sorry, fixed now I hopert – David Carlisle Nov 06 '13 at 21:23
  • Brilliant - I just wish I knew how those incantations actually work… :-) – vwegert Nov 06 '13 at 21:27
  • 1
    @vwegert they just disable colortbl's color panel for the hidden cell (using a not exactly advertised interface, but it is my code I'm disabling so I think I'm allowed:-) – David Carlisle Nov 06 '13 at 21:29
  • Well, \zzz is a good suggestion - as in getting some sleep. However, I'll pose another table-related question first… – vwegert Nov 06 '13 at 21:35
2

This is, I hope, an improvement over my earlier attempt. To eliminate the residual color, I had to set \tabcolsep to 0pt. I then used @{\extracolsep{1ex}} to add column gap back in. The only downside is that the extracolsep will not be colored in a cell, but that may be a workable solution.

\documentclass{article}
\usepackage{array}
\usepackage{colortbl}
\let\svtabcolsep\tabcolsep
\setlength\tabcolsep{0pt}
\newcommand{\gc}{\cellcolor[gray]{.7}}
\newcolumntype{H}{>{\setbox0=\hbox\bgroup}c<{\egroup}@{}}

\newcommand{\tablebody}{%
      foo &     bar & \gc boo &     baz &     faa \\
      bar & \gc boo &     baz & \gc faa-extra-extra long &     foo \\
  \gc boo &     baz &     faa &     foo & \gc bar \\
      baz & \gc faa &     foo & \gc bar &     boo \\
      faa &     foo-extra long & \gc bar &     boo &     baz %
}

\begin{document}

\section*{all columns visible}

\begin{tabular}{@{\extracolsep{1ex}}ccccc}
  \tablebody
\end{tabular}

\section*{center column hidden}

\begin{tabular}{@{\extracolsep{1ex}}ccHcc}
  \tablebody
\end{tabular}

\section*{middle columns hidden}

\begin{tabular}{@{\extracolsep{1ex}}cHcHc}
  \tablebody
\end{tabular}

\section*{outer columns hidden}

\begin{tabular}{@{\extracolsep{1ex}}HcccH}
  \tablebody
\end{tabular}

\end{document}

enter image description here

  • I'm afraid that won't work for me - the grayed out cells are empty in my document. I need the entire cell grayed out, and the colorbox only colors part of the cell. – vwegert Nov 06 '13 at 19:28
  • @vwegert OK, I'll delete this answer, but think further upon it. – Steven B. Segletes Nov 06 '13 at 19:30
  • @vwegert Although, let me ask one last question... are there any added constraints, like all columns end up being the same width, for example, because of the nature of the inputs. (It sort of looks that way in your example, but that may be coincidence). – Steven B. Segletes Nov 06 '13 at 19:32
  • The identical width is a coincidence - my table is far more complex. http://imagebin.org/275170 is a screenshot of the spreadsheet file I'm trying to replace. – vwegert Nov 06 '13 at 19:35
  • @vwegert Understood. Perhaps you could add that pointer to your question. – Steven B. Segletes Nov 06 '13 at 19:41
  • @vwegert Please see totally revised answer. – Steven B. Segletes Nov 06 '13 at 20:06