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 using fixed-width columns with my table, and using both longtable and booktabs packages (as well as tabularx because of \newcolumntype command).

In my table, I'll have lots of number data in cells that I want to align to the right. However, I still want to keep the headers aligned to the center, because some heading text will be broken into two (or more) lines.

How can I solve this? Here is a small testcase:

\documentclass{article}

\usepackage[brazilian]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{longtable}
\usepackage{tabularx}
\usepackage{booktabs}

\title{Table header aligment}

\begin{document}

\newcolumntype{D}{m{20mm}}
\newcolumntype{L}{>{\raggedright\arraybackslash}m{20mm}}
\newcolumntype{C}{>{\centering\arraybackslash}m{20mm}}
\newcolumntype{R}{>{\raggedleft\arraybackslash}m{20mm}}

\begin{longtable}{DLCR}

\toprule
I want this header text centered &
I want this header text centered &
I want this header text centered &
I want this header text centered \\
\midrule

\endhead

Justified alignment &
Left alignment &
Center alignment &
Right alignment \\

% This row is just to show which columns will hyphenate, and which ones won't
Love alignment &
Love alignment &
Love alignment &
Love alignment \\

\midrule

% I wanna keep the numbers of this column aligned to the right, while keeping
% the headers aligned to the center.
dummy text & dummy text & dummy text & 123456 \\ 
dummy text & dummy text & dummy text &   1234 \\ 
dummy text & dummy text & dummy text &    456 \\ 

\bottomrule

\end{longtable}

\end{document}

LaTeX rendering of the above code

share|improve this question

4 Answers 4

up vote 14 down vote accepted

Is there a problem with changing the headers of your table to:

\toprule
\centering I want this header text centered &
\centering I want this header text centered &
\centering I want this header text centered &
\centering I want this header text centered \tabularnewline
\midrule
share|improve this answer
    
It seems to solve the problem! Thanks! (but I'll wait a few days before accepting the answer, just to see other solutions) –  Denilson Sá Sep 9 '10 at 1:31
1  
Sounds like a plan. Still, I'm prepared to bet that you won't find anything much better than this! –  Geoffrey Jones Sep 9 '10 at 4:59
    
This did not work for me, I didn't use any special table package though. –  Marcel Valdez Orozco Oct 18 '12 at 15:50
    
@MarcelValdezOrozco You'll have to open a new question with more details than that (include a minimal working example (MWE)) if you want any help –  Lev Bishop Oct 18 '12 at 20:17
    
@LevBishop The answer from Stefan did work, though. So I didn't need to post another question. :D –  Marcel Valdez Orozco Oct 18 '12 at 21:15

I suggest using

\multicolumn{1}{C}{I want this header text centered}

\multicolumn allows changing the formatting of single cells. Here you've already defined the C type, so just use this. Though using\centering directly in cells works, it changes the meaning of \\, so you may have to use \arraybackslash afterwards or use \tabularnewline instead of \\. Your C type has it already built in.

Since you use that format several times, perhaps in further tables, you could define a shortcut for it like

\newcommand{\centercell}[1]{\multicolumn{1}{C}{#1}}

To go further to semantic markup, I would define a head cell style and write just

\head{This is my column head}

in the document body and define in the header my desired formatting document wide, such as

\newcommand{\head}[1]{\centercell{\bfseries#1}}

This allows easy and consistent changing later. Imagine, you would decide later to omit the centering - instead of searching and removing \centering in the document body it's just a small change in the preamble. Here you could quickly switch to bold or medium typeface or perhaps to \Centering of ragged2e. That's no problem, since you separated the formatting from the content.

share|improve this answer
    
Your solution is also very good, and I've upvoted it too. But in the end I had used Lev Bishop solution. Anyway, thank you for your solution! –  Denilson Sá Oct 1 '10 at 19:57
    
Now the only missing macro to make my day would be something like \headerrow{spec}{content}, with spec being the usual tabular c|c|lp{2cm}. This macro would then apply the given specs to the & separated content via single multicolumns. –  quazgar Aug 5 '13 at 15:33
    
What would make my day would be if the same macro worked for p/m/b columns and l/r/c columns, and if it didn't require duplicating the column widths for the normal cells and the headers. See [tex.stackexchange.com/questions/173194/… –  mabartibin Apr 24 at 12:07

In 2012 (actually from 2011), we have also the option to use the tabu-package. The problem described here, can be solved by the flexible \rowfont command. I have also loaded the ragged2e-package, for nicer ragged (right) text.

You do not need to defined the centred, justified, left or right aligned column type. They are already defined. But tabu indent justified paragraphs, so sometime a noindent is necessary.

To avoid bad spacing (even with booktabs loaded) the \tabulinesep=-command is important.

\documentclass{article}

\usepackage[UKenglish]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{longtable,tabu}
\usepackage{booktabs}
\usepackage[newcommand]{ragged2e}

\title{Table header aligment}

\begin{document}

\tabulinesep=1.2mm % Always yous this command to avoid bad spacing
\begin{longtabu} to 90mm {>{\noindent}X[J]X[L]X[C]X[R]}

\toprule\rowfont[c]\bfseries
I want this header text centred &
I want this header text centred &
I want this header text centred &
I want this header text centred \\
\midrule

\endhead

Justified alignment &
Left alignment &
Centre alignment &
Right alignment \\

% This row is just to show which columns will hyphenate, and which ones won't
Love alignment &
Love alignment &
Love alignment &
Love alignment \\

\midrule

% I want to keep the numbers of this column aligned to the right, while keeping
% the headers aligned to the centre.
dummy text & dummy text & dummy text & 123456 \\ 
dummy text & dummy text & dummy text &   1234 \\ 
dummy text & dummy text & dummy text &    456 \\ 

\bottomrule

\end{longtabu}

\end{document}

This yields:

enter image description here

share|improve this answer

There is a package makecell, that defines a macro \thead for one column table heads. Alignment by default is centered, but you can give optional argument (l, c or r) like this:

\thead[c]{header text}

The font is by default \footnotesize. To redefine it do for example:

\def\theadfont{\bfseries}

Unfortunately it doesn't seem to work with S table format from siunitx package. \multicolumn works though, see Stefan's answer.

share|improve this answer

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.