5

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 \\
   \multicolumn{2}{c}{category header} \\
   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}
\multicolumn{\the\tabu@nbcols}{c}{header}

What I want could look like:

\begin{tabu} {XX}
   abc   & def \\
   \mycatheader{category header} \\
   ghi   & jkl \\
\end{tabu}
2
  • errr, can't you use \noalign{\textbf{Header text}}?
    – Thruston
    Commented Dec 9, 2012 at 19:33
  • No, this breaks the layout and uses a wrong row height.
    – Lutz
    Commented Dec 9, 2012 at 20:46

3 Answers 3

9
\makeatletter
\def\mycatheader#1{\multicolumn{\tabu@nbcols}{c}{#1}}
\makeatother

\begin{document}


\begin{tabu} {XX}
   abc   & def \\
   \mycatheader{category header}\\
   \multicolumn{2}{c}{category header} \\
   ghi   & jkl \\
\end{tabu}
0
3

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 \\
\multicolumn{\mycol}{c}{header}\\
ghi   & jkl \\
\end{tabu}

Your macro could be written like this:

\def\mycatheader#1{\multicolumn{\mycol}{c}{#1}}
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
    Commented Dec 9, 2012 at 20:35
2
\documentclass[a4paper]{article}
\usepackage{tabu}
\begin{document}

\begin{tabu} {XX}
 abc   & def \\
 \multicolumn{2}{c}{category header} \\
 ghi   & jkl \\
\end{tabu}

\bigskip
\newcommand{\mycatheader}[1]{\noalign{\medskip\noindent\centerline{\textbf{#1}}\smallskip}}

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

\end{document}
1
  • Thank You for your answer. Your mycatheader works in a very specific (well limited) way as it is not possible to use all of the tabu styling options. But I have to admit, that it does, what I asked for above. :-)
    – Lutz
    Commented Dec 9, 2012 at 20:38

You must log in to answer this question.

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