Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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}.

enter image description here

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}
share|improve this answer
    
Perfect. Thanks. –  David Dec 1 '13 at 4:50
    
@David -- You are welcome, Glad it helps. –  Jesse Dec 1 '13 at 4:53
3  
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
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.