[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
> \endhead
> \endfoot
> main column
> & \if at SHORT
>     only one additional column
>   \else
>       first  additional column
>     & second additional column
>   \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).

Your guess is probably right.

> 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
%\SHORTfalse % two columns instead

\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
\endhead
\endfoot
main column
&\SecondCol{only one additional column}{first  additional column}
&\ThirdCol{second additional column}
\\\hline
\endlongtable
\end{document}

Cheers,  Phil



More information about the texhax mailing list