Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
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
add comment

3 Answers

up vote 8 down vote accepted
\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}
share|improve this answer
    
Thank You, works perfectly! –  Lutz Dec 9 '12 at 20:34
add comment

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}}
share|improve this answer
    
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
add comment
\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}
share|improve this answer
    
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 Dec 9 '12 at 20:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.