我想使用这个答案隐藏表格中的某些列。表格在整个文档中多次出现,并且始终隐藏不同的列。使用该colortbl
包突出显示了一些单元格,这似乎会导致问题:
似乎彩色单元格以某种方式从隐藏单元格“泄漏”到后续单元格中。我该怎么做才能防止这种情况发生?我希望隐藏单元格的背景颜色完全消失 - 我不希望它甚至部分出现在后续单元格中。
梅威瑟:
\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}
答案1
\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}
答案2
我希望这比我之前的尝试有所改进。为了消除残留颜色,我不得不设置\tabcolsep
为0pt
。然后我@{\extracolsep{1ex}}
又添加了列间隙。唯一的缺点是 extracolsep 不会在单元格中着色,但这可能是一个可行的解决方案。
\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}