I have tables (tabu, longtable and other) generated with PHP with variable cell content. Sometimes, all cells in column are empty, and in that case I would to hide whole column with header.

Here we have solution to manually hide column. Is it possible to write any script that does it automatically?

Minimal example code:

\begin{tabular}{c c}
\textbf{Lp.} & name \\\hline 
 1 &  \\\hline
 2 &  \\\hline

In this case, I want to hide the "name" column

  • Perhaps show example code with sample data, to work on.
    – Stefan Kottwitz
    Mar 6, 2015 at 12:48
  • @StefanKottwitz - i added minimal example. In this case i need to render table with only first column. Mar 6, 2015 at 13:06
  • Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem.
    – erik
    Mar 6, 2015 at 13:07
  • 1
    It looks easier to me to do this kind of stuff on the generator (PHP) side. Doing this on the LaTeX side seems wrong because it assumes LaTeX understands the meaning of the columns. For example, you use a divider (|) in the required argument of longtable. It's not clear whether it should be kept when a column becomes empty or not.
    – user10274
    Mar 6, 2015 at 13:23
  • 1
    @DavidCarlisle: One could put (tikz)marks at the begin and end of every data cell and then measure the distance between both, it is is zero for all cells the column is empty. But this looks like a lot of trouble for something that should be easy for find out on the php-side. Mar 6, 2015 at 14:11

1 Answer 1


Based on this page and this page, here's a crude answer which can be further improved:

% define hidden column type
% counter for non-blanks
% define character count function
\def\gchar{\let\char= }
  \ifx\char\nil \let\next=\relax%
    \advance\ccount by 1%
\def\cchar#1{\edef\xx{#1}\ccount=0 \expandafter\axchar\xx\nil}
% define conditional display function
   \tablebody{c c}%
   \tablebody{c H}%
% define table structure function
\textbf{Lp.} & name \\\hline 
%%%%%% try removing what is in \xc{} %%%%%%
 1 & \xc{} \\\hline
 2 & \xc{abc} \\\hline
 3 & \xc{} \\\hline
 4 & \xc{3.14} \\\hline
 5 & \xc{} \\\hline


% use box to initially render table without displaying it,
% so non-blanks can be counted
\setbox\uvox\hbox{\tablebody{c c}}

% display table

Note that each cell in the column to hide should be enclosed with \xc{} in order for this to work.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .