2

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?

3

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}
  • 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
  • Perfect. This really helped today as I continue on my project. Note that you have one extra |r, seven columns instead of the required six columns, but still awesome reply. – David Dec 1 '13 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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