2

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}
1
  • 2
    Are you meaning horizontally or vertically centered?
    – egreg
    Jul 3 '12 at 11:43
4

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
line & The vase was \colorbox{red!50}{broken} \linebreak by the dog \\ 
\hline 
\end{tabular}

\end{document}

Edit
Reposted the changed output

Latex output

7
  • 2
    the last cell has no correct horizontally centered text. Compare it with using \linebreak instead of \newline
    – user2478
    Jul 3 '12 at 11:51
  • Thank you, @Herbert \linebreak centers better. But why is the difference? I edited my answer 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.) 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. 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
    – user2478
    Jul 3 '12 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.