# [texhax] variable number of columns in a longtable

Philip G. Ratcliffe philip.ratcliffe at fastwebnet.it
Thu May 8 17:00:55 CEST 2008

> I would like to use the same document for two output formats,
> both generated with LaTeX. One of the differences is the
> number of columns in one longtable.
>
> What I have right now (stripped down of course):
>
> --------------------
> \documentclass{scrartcl}
> \usepackage{longtable}
> \begin{document}
>
> \makeatletter
> \newif\if at SHORT
> % only the short column (does not work):
> %\@SHORTtrue
> % two columns instead (seems to work):
> \@SHORTfalse
>
> \if at SHORT\longtable{|l|l|}\else\longtable{|l|l|l|}\fi
> \hline
> \endfoot
> main column
> & \if at SHORT
>   \else
>   \fi
> \\\hline
> \endlongtable
> \end{document}
> ---------------
>
> I think I understand why it does not work (it seems OK if you
> generate the long version, but its flaw shows itself for the
> short version).

> Any ideas how I could make this work?

This is all I could come up with in the ten minutes I had spare, but it
seems to work.  By the way, I wouldn't use @ and \makeatletter all the way
through your code; it's really not likely to be necessary if you use capital
letters.

\documentclass{scrartcl}
\usepackage{longtable}
\begin{document}

\newif\ifSHORT
\SHORTtrue % only the short column

\def\SecondCol#1#2{\ifSHORT#1\else#2\fi}
\def\ThirdCol#1{\ifSHORT\else#1\fi}
\ifSHORT
\longtable{|l|l@{}l|}
\else
\longtable{|l|l|l|}
\fi
\hline
\endfoot
main column