Hiding a column with colored cells
\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}
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}