1

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:

\documentclass[a4paper]{article}
\begin{document}
\begin{tabular}{c c}
\hline
\textbf{Lp.} & name \\\hline 
 1 &  \\\hline
 2 &  \\\hline
\end{tabular}
\end{document}

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

9
  • 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

1

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

\documentclass[a4paper]{article}
\usepackage{array}
% define hidden column type
\newcolumntype{H}{>{\setbox0=\hbox\bgroup}c<{\egroup}@{}}
% counter for non-blanks
\newcounter{ub}
% define character count function
\def\gchar{\let\char= }
\newcount\ccount
\def\cunil{%
  \ifx\char\nil \let\next=\relax%
  \else%
    \let\next=\axchar%
    \advance\ccount by 1%
  \fi\next
}
\def\axchar{\afterassignment\cunil\gchar}
\def\cchar#1{\edef\xx{#1}\ccount=0 \expandafter\axchar\xx\nil}
\long\def\xc#1{#1\cchar{#1}
\ifnum\ccount>0
\stepcounter{ub}
\fi
}
% define conditional display function
\def\disptab#1{
\ifnum#1>0%
   \tablebody{c c}%
\else%
   \tablebody{c H}%
\fi
}
% define table structure function
\def\tablebody#1{
\begin{tabular}{#1}
\hline
\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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{tabular}
}

\begin{document}

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

% display table
\disptab{\theub}
\end{document}

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

0

You must log in to answer this question.

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