3
\documentclass{article}
\usepackage{array}
\begin{document}
\begin{tabular}{| >{\centering}p{2.5cm}  | >{\centering}p{0.5cm}  | >{\centering}p{2.5cm} |>{\centering}p{2.5cm} |>{\centering}p{2.5cm} |>{\centering}p{2.5cm} |}
\hline
Abc & Bcd & A long cell with text that wraps around and is centered & long cell with text  & AAAlong cell with text that wraps around and is centered & SSSlong cell with text that wraps around and is centered \\
1 & 3 & qw & qe & sad &11\\
\hline
\end{tabular}
\end{document}
David Carlisle
  • 757,742
Strong
  • 51
  • If the centering is your only problem, then the question is a duplicate of Center column with specifying width in table (tabular enviroment)?. – Martin Scharrer May 23 '11 at 16:36
  • 1
    @Strong: Welcome to tex.sx! +1 for your working minimal example. Would you mind to change your question title to something more descriptive (e.g. "Using \centering inside table column definition results in error message")? – lockstep May 23 '11 at 16:40
  • @Martin: I'd rather think this question is a helpful "near duplicate", because it's about the error resulting from the changed definition of \\. – lockstep May 23 '11 at 16:43
  • @Strong: I see that the same question appeared a second time. I assume this was a mistake, probably posting twice, so I've removed the duplicate. (For others, it was an exact copy of this one.) – Joseph Wright May 23 '11 at 16:58
  • @Martin: I'm not sure this is exactly a dupe, as the question here is to do with why a problem arises from the 'obvious' approach. – Joseph Wright May 23 '11 at 16:59
  • @Joseph: Mmm, the issue can be solved with the answers in the linked question, so it should be OK to close it as a duplicate after pointing out that the error can be fixed using the linked answers. I found the other question after I posted my answer. I personally don't mind if this question is closed or not. – Martin Scharrer May 23 '11 at 17:16

1 Answers1

5

\centering changes the definition of \\ which breaks inside tabluars and arrays. You need to add \arraybackslash afterwards to correct this. You also should define a new column type if you need this more often:

\documentclass{article}
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}

\begin{document}
\begin{tabular}{| C{2.5cm} | C{0.5cm} | C{2.5cm} |C{2.5cm} |C{2.5cm} |C{2.5cm} |}
\hline
Abc & Bcd & A long cell with text that wraps around and is centered & long cell with text  & AAAlong cell with text that wraps around and is centered & SSSlong cell with text that wraps around and is centered \\
1 & 3 & qw & qe & sad &11\\
\hline
\end{tabular}
\end{document}

See the related answer to Center column with specifying width in table (tabular enviroment)? for advanced declarations which support line-breaks and hyphenation of the first word.

David Carlisle
  • 757,742
Martin Scharrer
  • 262,582
  • 1
    For narrow columns (such as the second one in the example code) it would be a good idea to add \hspace{0pt} to the column definition, to allow hyphenation of the first word. – Gonzalo Medina May 23 '11 at 16:42
  • Dear Martin. When I ran it, one error appeared: úndefined control sequence'for command ''newcolumntype'' – Strong May 23 '11 at 20:52
  • @Strong: It works for me. \newcolumntype is defined by the array package. – Martin Scharrer May 23 '11 at 20:58