# How to span over variable number of cells?

I'm looking for a macro to span all cells in a tabu environment to get category headers in tables.

This example does what I need for two columns.

\begin{tabu} {XX}
abc   & def \\
ghi   & jkl \\
\end{tabu}


What I want is a macro without the need to specify the number of columns. The category header should always span over all columns of the tabu environment.

I tried to access the tabu@nbcols constant but I've got no clue how to do that. These two don't work:

\multicolumn{\tabu@nbcols}{c}{header}


What I want could look like:

\begin{tabu} {XX}
abc   & def \\
ghi   & jkl \\
\end{tabu}

-
errr, can't you use \noalign{\textbf{Header text}}? –  Thruston Dec 9 '12 at 19:33
No, this breaks the layout and uses a wrong row height. –  Lutz Dec 9 '12 at 20:46

\makeatletter
\makeatother

\begin{document}

\begin{tabu} {XX}
abc   & def \\
ghi   & jkl \\
\end{tabu}

-
Thank You, works perfectly! –  Lutz Dec 9 '12 at 20:34

You've fast got it.

Your problem is the use of macro with @ letter, that is normally not allowed; you need to write \makeatletter to be able to use @ and \makeatother to make the situation normal again.

Here is what you can do:

In your preamble, define a new macro to access the number of columns:

\makeatletter
\edef\mycol{\tabu@nbcols}
\makeatother


And in a tabu environment:

\begin{tabu} {XX}
abc   & def \\
ghi   & jkl \\
\end{tabu}


Your macro could be written like this:

\def\mycatheader#1{\multicolumn{\mycol}{c}{#1}}

-
Thank You for your quick reply, I didn't use edef, but makeatletter directly inside the tabu environment. And that didn't work. –  Lutz Dec 9 '12 at 20:35
\documentclass[a4paper]{article}
\usepackage{tabu}
\begin{document}

\begin{tabu} {XX}
abc   & def \\
ghi   & jkl \\
\end{tabu}

\bigskip