# Center array headers but right justify numbers in columns

I'd like to center my headers but right-justify the numbers in the columns.

\begin{array}{c|c|c|c|c|c}
x & y & (x-\overline{x}) & (y-\overline{y}) & (x-\overline{x})(y-\overline{y}) & (x-\overline{x})^2\\\hline
10 & 31 & -20 & -36 & 720 & 400\\
20 & 44 & -10 & -23 & 230 & 100\\
30 & 66 & 0 & -1 & 0 & 0\\
40 & 90 & 10 & 23 & 230 & 100\\
50 & 104 & 20 & 37 & 740 & 400\\\hline
150 & 335 & & & 1920 & 1000
\end{array}


Suggestions?

-

Define the header via multicolumn{1}{c|}{content} to the first two columns where centering alignment fails. Here the alignment specifier is changed to {r|r|r|r|r|r|r}.

Code

\documentclass[]{article}

\begin{document}
$\begin{array}{r|r|r|r|r|r|r} \multicolumn{1}{c|}{x}&\multicolumn{1}{c|}{y}&(x-\overline{x}) &(y-\overline{y}) &(x-\overline{x})(y-\overline{y}) & (x-\overline{x})^2\\\hline 10 & 31 & -20 & -36 & 720 & 400\\ 20 & 44 & -10 & -23 & 230 & 100\\ 30 & 66 & 0 & -1 & 0 & 0\\ 40 & 90 & 10 & 23 & 230 & 100\\ 50 & 104 & 20 & 37 & 740 & 400\\\hline 150 & 335 & & & 1920 & 1000 \end{array}$
\end{document}


Update: Based on Dan's comment below that centering only works for p{}-type cell. One can take a more general approach as shown below, where multicolumn{1}{c|}{content} are used throughout the header. However, the centering works for that case is the fact that the contents take less spaces when compared with their corresponding headers. Same reasoning for the first two columns (single x and y) and those requires multicolumn technique.

$\begin{array}{r|r|r|r|r|r|r} \multicolumn{1}{c|}{x}&\multicolumn{1}{c|}{y}&\multicolumn{1}{c|}{(x-\overline{x})} & \multicolumn{1}{c|}{(y-\overline{y})} &\multicolumn{1}{c|}{(x-\overline{x})(y-\overline{y})} & \multicolumn{1}{c|}{(x-\overline{x})^2}\\ \hline 10 & 31 & -20 & -36 & 720 & 400\\ 20 & 44 & -10 & -23 & 230 & 100\\ 30 & 66 & 0 & -1 & 0 & 0\\ 40 & 90 & 10 & 23 & 230 & 100\\ 50 & 104 & 20 & 37 & 740 & 400\\\hline 150 & 335 & & & 1920 & 1000 \end{array}$
\end{document}

-
Perfect. Thanks. –  David Dec 1 '13 at 4:50
@David -- You are welcome, Glad it helps. –  Jesse Dec 1 '13 at 4:53
Those \centering commands have no effect. They only have effect in p{}-type cells. –  Dan Dec 1 '13 at 5:21
@Dan -- In that case, multicolumn{}{}{} will be used. I will refine my answer for you remark. –  Jesse Dec 1 '13 at 5:42
@David Don't forget to accept answers. :) –  Ch'en Meng Dec 1 '13 at 6:08