82

I have a set of 10 tables and each has an extra column that I don't want. Specifically, these tables are descriptions of database tables exported by phpMyAdmin, and they all have a "MIME" column that I would like to remove. Is there a way to do this other than using string manipulation / find and replace?

I have tried setting the column to p{0in}, but the column title still remains.

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

  • 3
    That's very cool! – Alan Munn Apr 25 '11 at 20:03
  • 6
    You can write it shorter by using the lrbox environment, which is the environment form of \sbox (which is the LaTeX form of the plain \setbox): \newcolumntype{H}{>{\lrbox0}c<{\endlrbox}@{}}. @Alan might find this even cooler ;-) – Martin Scharrer Apr 26 '11 at 11:42
  • 1
    This doesn't entirely erase the column. If it's only the end and there's a cell or row colour, it will show up: \newcolumntype{H}{>\setbox0=\hbox\bgroup\cellcolor{white}}c<{\egroup}@{}} – Tom Kelly Feb 15 '17 at 2:47
  • If it's used in the last column the spacing isn't correct (two \tabcolseps instead of one). The same is true for every solution provided in this thread that work in the last column. – Skillmon Apr 5 '18 at 14:59
  • @Skillmon Thanks for the prompt, I added the needed workaround – egreg Apr 5 '18 at 16:21
37

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}
  • 1
    Nice; I should remember the collcell package. – egreg Apr 25 '11 at 21:42
  • 4
    Is the solution that doesn't work in the last column, but is faster, really a solution? :-) When I first started programming I was told the following story by my boss: Some engineers and some computer scientists are asked to solve a problem. The engineers say "Our program is 10000 lines of FORTRAN that takes 8 hours to run"; the computer scientists say "Ours is much better, it's 2000 lines of LISP and takes an hour to run. But it sill has a few bugs...." – Alan Munn Apr 25 '11 at 21:42
  • 1
    @Alan: If it would be a satisfying solution I wouldn't had spend all the time coding the collcell package :-) It doesn't has a bug, just two limitations. The not-in-the-last-column is the first and the second I forgot to mention is that the cell must not contain an \unskip outside { }. – Martin Scharrer Apr 25 '11 at 22:23
3

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

  • This doesn't seem to work for the last column. – Faheem Mitha Nov 10 '17 at 19:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.