Easiest way to delete a column?

\documentclass[a4paper]{article}
\usepackage{array}
\newcolumntype{H}{>{\setbox0=\hbox\bgroup}c<{\egroup}@{}}

\begin{document}
\begin{tabular}{ccHc}
one & two & hide & three\\
1 & 2 & H & 3
\end{tabular}

\begin{tabular}{ccc}
one & two & three\\
1 & 2 &  3
\end{tabular}

\end{document}

The second tabular is there to show that the result is the same. Basically, we set a column without intercolumn space, and its content is swallowed by \setbox0=\hbox{<entry>}

For the last column you need to fix the last padding, use, for instance

\begin{tabular}{ccH@{\hspace*{-\tabcolsep}}}

Example code

\documentclass[a4paper]{article}
\usepackage{array}
\newcolumntype{H}{>{\setbox0=\hbox\bgroup}c<{\egroup}@{}}
\newcolumntype{Z}{>{\setbox0=\hbox\bgroup}c<{\egroup}@{\hspace*{-\tabcolsep}}}

\begin{document}

\fbox{\begin{tabular}{ccHc}
one & two & hide & three\\
1 & 2 & H & 3
\end{tabular}}

\fbox{\begin{tabular}{ccc}
one & two & three\\
1 & 2 &  3
\end{tabular}}

\fbox{\begin{tabular}{ccH@{\hspace*{-\tabcolsep}}}
one & two & hide \\
1 & 2 & H
\end{tabular}}

\fbox{\begin{tabular}{ccZ}
one & two & hide \\
1 & 2 & H
\end{tabular}}

\fbox{\begin{tabular}{cc}
one & two \\
1 & 2
\end{tabular}}

\end{document}

enter image description here


The solution of egreg is good. It typesets the cell content in a box which is then discarded. All code in the cell is executed and typeset. There are also two alternatives which I list here for the sake of completeness.

First you can use the collcell package to collect the cell content and then feed it to a macro which simply discards it. The \@gobble macro does that. This method doesn't execute or typeset the cell content but needs to scan every token. It might be faster or slower than the use of a box dependent on the content.

Another method is to insert a macro which reads everything till the next \unskip which is inserted after each cell except the very last column of a row. There the \unskip is inside \\ and won't be seen by the macro. This method is the fastest and doesn't execute, typeset or check the cell content beside the argument scanning of the TeX parser.

For small cells the speed difference should be not important.

\documentclass[a4paper]{article}

\usepackage{collcell}
\makeatletter
\newcolumntype{G}{>{\collectcell\@gobble}c<{\endcollectcell}@{}}
\makeatother
% Fastest. Does not work in the last column.
\def\eatcell#1\unskip{}
\newcolumntype{E}{>{\eatcell}c@{}}

\begin{document}
\begin{tabular}{ccGc}
one & two & hide & three\\
1 & 2 & H & 3
\end{tabular}

\begin{tabular}{ccEc}
one & two & hide & three\\
1 & 2 & H & 3
\end{tabular}

\begin{tabular}{ccc}
one & two & three\\
1 & 2 &  3
\end{tabular}

\end{document}

Or in short:

\usepackage{array}
\newcolumntype{H}{>{\iffalse}c<{\fi}@{}}

Can be made visible by changing the column type or by changing \iffalse into \iftrue

Tags:

Columns

Tables