2

I want to create a longtabu table with a conditional number of columns: In development mode, 5 columns should be visiable. In the final version, only the first and the last column should be visible.

To achieve this, I defined a new column type "H" which is suppressed in the output (with a little help from Easiest way to delete a column? ). Hiding columns with this column type works fine.

The problem is: I can't switch conditionally between the "H" column and the "X" column. I want to use a different column definition in my longtabu table, but it doesn't work (error: "Misplaced alignment tab character").

How to make it work?

\documentclass[]{scrreprt}

\usepackage{tabu, longtable}

% Create hidden column, see: https://tex.stackexchange.com/q/16604/
\usepackage{collcell}
\makeatletter
\newcolumntype{H}{>{\collectcell\@gobble}c<{\endcollectcell}@{}}
\makeatother
\tabucolumn H


% If defined, only show first and last column
%\def\MyOnlyShowFinalTable{\relax}


\begin{document}

\ifdefined\MyOnlyShowFinalTable
\begin{longtabu} to \textwidth { X[2.0,m,m] | H | H | H | X[7.0,m,m] }
\else
\begin{longtabu} to \textwidth { X[1.3,m,m] | X[0.8,m,m] | X[0.8,m,m] | X[0.8,m,m] | X[5.0,m,m] }
\fi
A & B & C & D & E\\
A & B & C & D & E\\
A & B & C & D & E\\
A & B & C & D & E\\
\end{longtabu}

\end{document}          

Some additional information: I found out, that there is some workaround when using "c" columns instead of "X" columns:

\documentclass[]{scrreprt}

\usepackage{tabu, longtable}


% If defined, only show first and last column
%\def\MyOnlyShowFinalTable{\relax}


\ifdefined\MyOnlyShowFinalTable
  % Create hidden column, see: https://tex.stackexchange.com/q/16604/
  \usepackage{collcell}
  \makeatletter
  \newcolumntype{H}{>{\collectcell\@gobble}c<{\endcollectcell}@{}}
  \makeatother
\else
  \newcolumntype H{c |}
\fi

\tabucolumn H


%\newcolumntype Y{c |}
%
%\tabucolumn Y

\begin{document}

\begin{longtabu} to \textwidth { X[1.3,m,m] | H H H X[5.0,m,m] }
A & B & C & D & E\\
A & B & C & D & E\\
A & B & C & D & E\\
A & B & C & D & E\\
\end{longtabu}

\end{document}          

However, this is still not quite what I want, as it doesn't let me specify the different column ratios, and it forces me to use "c" columns.

However, I wonder why this doesn't work with "X" columns?

1 Answer 1

4

The problem is that the \fi is evaluated when already the table preamble has been read and built.

Here's a working version; the \condtabupreamble command has four arguments:

  • 0 or 1 for choosing the third or fourth argument respectively
  • the tabu specification
  • the column specification with the "hidden" columns
  • the full column specification for the "non hidden" case
\documentclass[paper=a4]{scrreprt}

\usepackage{tabu, longtable}

% Create hidden column, see: http://tex.stackexchange.com/q/16604/
\usepackage{collcell}
\makeatletter
\newcolumntype{H}{>{\collectcell\@gobble}c<{\endcollectcell}@{}}
\makeatother
\tabucolumn H

\makeatletter
\newcommand{\condtabupreamble}[4]{%
  \ifcase#1\relax
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
  {#2 {#3}}
  {#2 {#4}}%
}
\makeatother

\begin{document}

\condtabupreamble{0}% 1 for the non hidden version
  {\begin{longtabu} to \textwidth}
  { | X[2.0,m] | H | H | H | X[7.0,m] | }
  { | X[1.3,m] | X[0.8,m] | X[0.8,m] | X[0.8,m] | X[5.0,m] | }
A & B & C & D & E\\
A & B & C & D & E\\
A & B & C & D & E\\
A & B & C & D & E\\
\end{longtabu}

\condtabupreamble{1}% 1 for the non hidden version
  {\begin{longtabu} to \textwidth}
  { | X[2.0,m] | H | H | H | X[7.0,m] | }
  { | X[1.3,m] | X[0.8,m] | X[0.8,m] | X[0.8,m] | X[5.0,m] | }
A & B & C & D & E\\
A & B & C & D & E\\
A & B & C & D & E\\
A & B & C & D & E\\
\end{longtabu}

\end{document}

enter image description here

1
  • Thanks alot for your answer! I already suspected that it was some kind of expansion issue, but I know too little about (La)TeX to solve such kind of problem. I slightly modfied your example so that I can use a globel switch for all tables, and it works perfectly now!
    – arngineer
    Sep 11, 2013 at 14:53

You must log in to answer this question.

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