How to align table headers differently than all other table cells?
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.
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
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}
\title{Table header aligment}
\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
\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: