3

I'm trying to make a table with some multicolumns that have wrapped text, but I also want them to be center justified. I have managed to get the text to wrap using advice from other questions, but it wont center and I don't know why.

\documentclass{article}

\usepackage{array}
\newcolumntype{D}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\begin{document}
\begin{table}[htbp]\centering
\begin{tabular}{l c c c c}
& \multicolumn{2}{D{2cm}}{\textbf{Some Big Words}} & \multicolumn{2}{D{2cm}}{\textbf{More Big Words}} \\
rowname & BlahBlah & BlahBlah & BlahBlah & BlahBlah \\
\end{tabular}
\end{table}
\end{document}

Image

Notice how the multicolumns are not located in the center of the two columns they are supposed to span, but are off to the left. How do I center the multicolumns?

2 Answers 2

4

Your titles are centred in the specifed width of 2cm, it's possible to make a vertical stack with a tight box that is centred in a c multicolumn by using a nested tabular

enter image description here

\documentclass{article}

\newcommand\hd[2]{\multicolumn{2}{c}{\bfseries\begin{tabular}{@{}c@{}}#2\end{tabular}}}

\usepackage{array}


\begin{document}
\begin{table}[htbp]\centering
\begin{tabular}{l c c c c}
& \hd{2}{Some Big\\Words} & \hd{2}{More Big\\Words} \\
rowname & BlahBlah & BlahBlah & BlahBlah & BlahBlah \\
\end{tabular}
\end{table}
\end{document}
6

As I don't really understand why you should need a width of 2cm;, I propose a very simple solution based on the makecell package, and its \thead command, that allows for line breaks in cells and a common formatting of cells. Does this code produce what you want?

\documentclass{article}
\usepackage{makecell}
\renewcommand\theadfont{\bfseries}

\usepackage{array}

\begin{document}

\begin{table}[htbp]
\centering
\begin{tabular}{l c c c c}
& \multicolumn{2}{c}{\thead{Some Big\\ Words}} & \multicolumn{2}{c}{\thead{More Big\\ Words}} \\
rowname & BlahBlah & BlahBlah & BlahBlah & BlahBlah
\end{tabular}
\end{table}

\end{document} 

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .