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.
\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}
share|improve this question
    
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

1 Answer 1

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

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

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.