44

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

5 Answers 5

17

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
5
  • 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, 2010 at 1:31
  • 1
    Sounds like a plan. Still, I'm prepared to bet that you won't find anything much better than this! Sep 9, 2010 at 4:59
  • 1
    This did not work for me, I didn't use any special table package though. Oct 18, 2012 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, 2012 at 20:17
  • @LevBishop The answer from Stefan did work, though. So I didn't need to post another question. :D Oct 18, 2012 at 21:15
46

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.

3
  • 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, 2010 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, 2013 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, 2014 at 12:07
8

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:

enter image description here

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

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.

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

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},
  rowhead = 1,
}
\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} 

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .