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}