How can I center the column headers in longtabu with the right aligned columns?

Tried centering but that did not work:

\begin{longtabu}{|r|r|} %\textwidth
\hline
\centering \boldmath  $X$ & \centering \boldmath $Y$ \tabularnewline
\hline
$-9.644$ &  $93.000$   \\
\hline
$1.997$ &  $3.988$   \\
\hline
\end{longtabu}


without of help of any packages:

\begin{longtabu}{|r|r|} %\textwidth
\hline
\multicolumn{1}{|c|}{$\bm{X}$} & \multicolumn{1}{c|}{$\bm{Y}$} \tabularnewline
\hline
$-9.644$ &  $93.000$   \\
\hline
$1.997$ &  $3.988$   \\
\hline
\end{longtabu}


for \bm is needed package bm

• Added the pipes around c as in |c| and it worked fine! – ajeh Nov 17 '16 at 19:06

The usual \multicolumn{1}{<alignment>}{<content>} can help. Also, use \mathbf{X} to get X bold in mathmode instead of \boldmath X. Furthermore, instead of manually switching to mathmode in each cell, the >{$} r <{$} specification in the header will suffice.

\documentclass{article}
\usepackage{longtable,tabu}
\begin{document}

\begin{longtabu}{| >{$}r<{$} | >{$}r<{$} |} %\textwidth
\hline
\multicolumn{1}{|c|}{$\mathbf{X}$} & \multicolumn{1}{c|}{$\mathbf{Y}$} \\
\hline
-9.644 & 93.000  \\
\hline
1.997  & 3.988   \\
\hline
\end{longtabu}

\end{document}


Use tabu's \rowfont [alignment]{font spec} command:

\documentclass{article}
\usepackage{tabu}
\begin{document}

\begin{tabu} to 15em {| X[r] | X[r] |} \hline
\everyrow{\hline}
\rowfont[c]{\bfseries}
X & Y \\
-9.644 & 93.000 \\
1.997 & 3.988 \\
\end{tabu}

\end{document}


(If you don't want the first line bold, you could just use \rowfont[c]{}, of course.)

Unfortunately the \rowfont[c] seems ignored when in math mode, I don't get why. But if you don't need math mode it's the simplest and prettiest solution imho.