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.

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}
share|improve this question
add comment

1 Answer 1

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}

enter image description here

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}
share|improve this answer
    
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
add comment

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.