# Table with fixed width centering won't compile

All, I don't think I've ever been this stuck. The following compiles. But if I uncomment the commented line, it does not compile. What am I doing wrong?

\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage{booktabs}
\usepackage{array}

\begin{document}

\begin{center}
\begin{tabular}{*{4}{>{\centering}p{2cm} |}}\toprule
%$x$ & $p$ & $xp$ & $x^2p$ \\\midrule
2 & 0.1 & &\\
3 & 0.2 & &\\
4 & 0.2 & &\\
5 & & &\\\midrule
$\sum x=$ & $\sum p=$ & $\sum xp=$ & $\sum x^2p=$
\end{tabular}
\end{center}

\end{document}

-

You need to use >{\centering\arraybackslash} since \centering redefines \\ (I removed the vertical lines since their use won't produce noce results, more if booktabs' features are in use):

\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage{booktabs}
\usepackage{array}

\begin{document}

\begin{center}
\begin{tabular}{*{4}{>{\centering\arraybackslash}p{2cm} }}\toprule
$x$ & $p$ & $xp$ & $x^2p$ \\\midrule
2 & 0.1 & &\\
3 & 0.2 & &\\
4 & 0.2 & &\\
5 & & &\\\midrule
$\sum x=$ & $\sum p=$ & $\sum xp=$ & $\sum x^2p=$
\end{tabular}
\end{center}

\end{document}


Another option is to use \tabularnewline instead of \\:

\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage{booktabs}
\usepackage{array}

\begin{document}

\begin{center}
\begin{tabular}{*{4}{>{\centering}p{2cm} }}\toprule
$x$ & $p$ & $xp$ & $x^2p$ \tabularnewline\midrule
2 & 0.1 & &\tabularnewline
3 & 0.2 & &\tabularnewline
4 & 0.2 & &\tabularnewline
5 & & &\tabularnewline\midrule
$\sum x=$ & $\sum p=$ & $\sum xp=$ & $\sum x^2p=$
\end{tabular}
\end{center}

\end{document}

-
Thank you. Can I also ask how I can left-justify the entries in the bottom row? –  David Oct 7 '12 at 23:57
@user16203 sure. You can use \multicolumn{1}{l}{$\sum...$} for each cell. –  Gonzalo Medina Oct 8 '12 at 0:00
Thanks. I can't tell you how helpful you have been. –  David Oct 8 '12 at 0:31