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.

Hi I just can't get the text in row 3 to centre. Array package is loaded. Any help greatly appreciated.

\centering

\begin{tabular}{|c|m{5cm}|}
\hline
STATE & EVENT \\ 
\hline 
The vase was broken &  \\ 
\hline 
\multicolumn {2} {|c|} {The vase was \colorbox{red!50}{broken}}\\ 
\hline
& The vase was \colorbox{red!50}{broken} \newline by the dog \\ 
\hline 
\end{tabular}
share|improve this question
2  
Are you meaning horizontally or vertically centered? –  egreg Jul 3 '12 at 11:43

1 Answer 1

up vote 4 down vote accepted

It will work, if you make the second column centered:

\documentclass{article}
\usepackage{amsmath,array,xcolor}

\begin{document}

\centering

\begin{tabular}{|c|>{\centering\arraybackslash}m{5cm}|}
\hline
STATE & EVENT \\ 
\hline 
The vase was broken &  \\ 
\hline 
\multicolumn{2}{|c|}{The vase was \colorbox{red!50}{broken}}\\ 
\hline
& The vase was \colorbox{red!50}{broken} \linebreak by the dog \\ 
\hline 
\end{tabular}

\end{document}

Edit
Reposted the changed output

enter image description here

share|improve this answer
2  
the last cell has no correct horizontally centered text. Compare it with using \linebreak instead of \newline –  Herbert Jul 3 '12 at 11:51
    
Thank you, @Herbert \linebreak centers better. But why is the difference? I edited my answer –  Peter Breitfeld Jul 3 '12 at 12:00
    
the first line in the last cell doesn't look centered; if you remove the space before \linebreak that should be adjusted. (by the way, this doesn't happen with tex live 2010, and the code in latex.ltx looks stable, so i'm not sure what happened here.) –  barbara beeton Jul 3 '12 at 12:36
1  
@Peter: You need different commands to end a line as you need different behaviours. E.g. \\ in a center environment is actually a \par so replacing it by a \newline is wrong. The best imho is to use the intended end-of-line command. In your case either remove the \arraybackslash, use \\ to end the center lines and \tabularnewline to end the tabular lines. Or store the "center meaning" of \\ in a command before restoring the "tabular meaning": {\centering\let\cnewline\\\arraybackslash} and then use this command. –  Ulrike Fischer Jul 3 '12 at 12:44
1  
@Peter; you should use package ragged2e and then only \Centering, then you can use \newline and also \\. However, as Ulrike already wrote, \tabularnewline makes more sense –  Herbert Jul 3 '12 at 13:12

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.