4

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.

1 Answer 1

6

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.

1
  • I prefer your second solution. The first one messes with height of the header row. Many thanks!
    – phx
    Sep 12, 2016 at 14:40

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.