# How to align table headers differently than all other table cells?

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}

\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

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}


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

• It seems to solve the problem! Thanks! (but I'll wait a few days before accepting the answer, just to see other solutions) Sep 9 '10 at 1:31
• Sounds like a plan. Still, I'm prepared to bet that you won't find anything much better than this! Sep 9 '10 at 4:59
• This did not work for me, I didn't use any special table package though. 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 Oct 18 '12 at 20:17
• @LevBishop The answer from Stefan did work, though. So I didn't need to post another question. :D 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.

• 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! 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. 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/… Apr 24 '14 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 define 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}

\begin{document}

\tabulinesep=1.2mm % Always use 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

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:

• For the record, tabu package is unmaintained and won't work, for example, with the latest texlive distributions. Feb 11 '21 at 17:51

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.

• It works for S columns, if you put \thead{...} in additional curly brackets.
– Dirk
Jan 21 '19 at 12:54

An alternative solution with longtblr environment of tabularray package:

\documentclass{article}

\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document}

\begin{longtblr}[
caption = {Long Table},
label = {tblr:test},
]{
colspec = {Q[m,j,20mm]Q[m,l,20mm]Q[m,c,20mm]Q[m,r,20mm]},
row{1} = {c},
}
\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
Justified alignment & Left alignment & Center alignment & Right alignment \\
Love alignment & Love alignment & Love alignment & Love alignment \\
\midrule
dummy text & dummy text & dummy text & 123456 \\
dummy text & dummy text & dummy text &   1234 \\
dummy text & dummy text & dummy text &    456 \\
\bottomrule
\end{longtblr}

\end{document}