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?
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:Addendum: An alternative solution where you left line break to column tye (as you should do in your MWE):
Use of
\newline
doesn't work well in column with centering text. Instead it rather use\par
as is shown in above MWE.