[Tex/LaTex] Multicolumn with line break does not center properly

line-breakingmulticolumntables

I try to build a table with a centered header that contains line breaks. Unfortunately, the text within the header is not correctly centered. What am I doing wrong?

enter image description here

MWE:

\documentclass[a4paper,11pt]{scrbook}
\usepackage[utf8]{inputenc}
\usepackage{array}
\usepackage{booktabs}


\begin{document}

  \begin{table}[htbp]
  \caption{My own test table.}
    \begin{center}
    \begin{tabular}{l
            >{\raggedright\arraybackslash}p{.27\textwidth}
            >{\raggedright\arraybackslash}p{.27\textwidth}
            >{\raggedright\arraybackslash}p{.27\textwidth}}
      \toprule      &
      \multicolumn{1}{>{\centering}p{.27\textwidth}}{AAA AAA \newline AAA} & 
      \multicolumn{1}{>{\centering}p{.27\textwidth}}{BBB BBB \newline BBB} & 
      \multicolumn{1}{>{\centering}p{.27\textwidth}}{CCC CCC \newline CCC}  \\ \midrule
      \emph{Blah} & 
test
      & 
test 
      & 
test \\ \bottomrule      
    \end{tabular}
    \end{center}
  \end{table}


\end{document}

Ps.: Yes, I do need this line break in the header of my table. The real table is much more complex, of course.

Best Answer

with use of makecell package this is simple:

\documentclass[a4paper,11pt]{scrbook}
\usepackage[utf8]{inputenc}
\usepackage{array,makecell}% <-- added makecell
\usepackage{booktabs}


\begin{document}

  \begin{table}[htbp]
  \caption{My own test table.}
    \begin{center}
    \begin{tabular}{l
            >{\raggedright\arraybackslash}p{.27\textwidth}
            >{\raggedright\arraybackslash}p{.27\textwidth}
            >{\raggedright\arraybackslash}p{.27\textwidth}}
      \toprule      &
      \thead{AAA AAA \\ AAA} &
      \thead{BBB BBB \\ BBB} &
      \thead{CCC CCC \\ CCC}  \\ \midrule
      \emph{Blah} &
test
      &
test
      &
test \\ \bottomrule
    \end{tabular}
    \end{center}
  \end{table}

enter image description here

Addendum: An alternative solution where you left line break to column tye (as you should do in your MWE):

enter image description here

\documentclass[a4paper,11pt]{scrbook}
\usepackage[utf8]{inputenc}
\usepackage{array}
\usepackage{booktabs}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}

\begin{document}

  \begin{table}[htbp]
  \caption{My own test table.}
    \begin{center}
    \begin{tabular}{l L{.27\textwidth}
                      L{.27\textwidth}
                      L{.27\textwidth}} \\
      \toprule      &
      \multicolumn{1}{C{.27\textwidth}}{AAA AAA AAA AAA AAA AAA AAA}    &
      \multicolumn{1}{C{.27\textwidth}}{BBB BBB\par BBB}    &
      \multicolumn{1}{C{.27\textwidth}}{CCC CCC CCC CCC CCC}    \\ \midrule
\emph{Blah} &
test
            &
test
            &
test        \\ \bottomrule
    \end{tabular}
    \end{center}
  \end{table}

Use of \newline doesn't work well in column with centering text. Instead it rather use \par as is shown in above MWE.