4

I would like to wrap (and center) text within \multicolumn. Tabularx and tabilary only wrap the text outside of the \multicolumn{} columns. I would also like to define the column width as defined by the non \multicolumn{} rows. Any ideas?

Example:

% Unwrapped example
\begin{tabular}{l*{7}{c}}
Team              & P &\multicolumn{2}{c}{Points Collected bssss} & L & F  & \multicolumn{2}{c}{Points Collected blablabl} \\
\hline
Manchester United & 6 & 4 & 0 & 2 & 10 & 5 & 12  \\
Celtic            & 6 & 3 & 0 & 3 &  8 & 9 &  9  \\
Benfica           & 6 & 2 & 1 & 3 &  7 & 8 &  7  \\
FC Copenhagen     & 6 & 2 & 1 & 3 &  5 & 8 &  7  \\
\end{tabular} 
6

You can use a new column type:

\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}

with the help of array package.

Complete code:

\documentclass{article}

\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}

\begin{document}
    % Unwrapped example
\begin{tabular}{l*{7}{c}}
Team              & P &\multicolumn{2}{C{1.5cm}}{Points Collected bssss} & L & F  & \multicolumn{2}{C{1.5cm}}{Points Collected blablabl} \\
\hline
Manchester United & 6 & 4 & 0 & 2 & 10 & 5 & 12  \\
Celtic            & 6 & 3 & 0 & 3 &  8 & 9 &  9  \\
Benfica           & 6 & 2 & 1 & 3 &  7 & 8 &  7  \\
FC Copenhagen     & 6 & 2 & 1 & 3 &  5 & 8 &  7  \\
\end{tabular} 

\end{document}

enter image description here

You may use either m{#1} or b{#1} instead of p{#1} as your likes.

  • Thanks, both answers are quite useful. I still have the problem though that I have to define the width manually. However, I am producing many of these tables with STATA and the titles change occasionally. So I need the columns width to to be automatically defined (in a way that all the columns with data in them are the same width, that is excl. the first column). – Max May 23 '13 at 0:48
  • Much like \begin{tabularx}{l*{7}{X}} would work. – Max May 23 '13 at 1:02
4

To have wrapping columns of a fixed width that aligns right or centered, you need to define them, e.g. using the array package and \newcolumntype.

The code belows shows possible definitions for left L, right R and centered C columns, and applies the C one to your MWE:

\documentclass{article}
\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\begin{document}
\begin{tabular}{l*{7}{c}}
Team              & P &\multicolumn{2}{C{2cm}}{Points Collected bssss} & L & F  & \multicolumn{2}{C{2cm}}{Points Collected blablabl} \\
\hline
Manchester United & 6 & 4 & 0 & 2 & 10 & 5 & 12  \\
Celtic            & 6 & 3 & 0 & 3 &  8 & 9 &  9  \\
Benfica           & 6 & 2 & 1 & 3 &  7 & 8 &  7  \\
FC Copenhagen     & 6 & 2 & 1 & 3 &  5 & 8 &  7  \\
\end{tabular}    
\end{document}

Output

  • Thanks, both answers are quite useful. I still have the problem though that I have to define the width manually. However, I am producing many of these tables with STATA and the titles change occasionally. So I need the columns width to to be automatically defined (in a way that all the columns with data in them are the same width, that is excl. the first column). – Max May 23 '13 at 0:59
  • Much like \begin{tabularx}{l*{7}{X}} would work. – Max May 23 '13 at 1:03
  • Is it only the headers that will determine the width of the columns? If so, you could easily pre-compute those automatically. Otherwise, it is going to be much more painful... – Xavier May 23 '13 at 1:21
3

The statement in the question that wrapping does not occur in \multicolumn is not correct. Wrapping will occur in par-mode column types (p, X) etc whether they are specified in the main table preamble or in \multicolum. If you use \multicolumn to change the column type to c then it will be single line as c does not support line breaking.

The only complication is knowing what the width should be, here an X cell is spanning across two X columns so set \hsize to be twice the normal X \hsize plus additional space to account for the column separation and rule that is also spanned. I added rules to make the alignment between the spanning cell and the default columns more clear.

enter image description here

\documentclass{article}

\usepackage{tabularx}

\begin{document}

\begin{tabularx}{6cm}{|c|X|X|}

a& zzz zzzz zzzzz & yyy yyy yyyy yyy yyy y\\
b& \multicolumn{2}{>{\hsize=\dimexpr 2\hsize+2\tabcolsep+\arrayrulewidth}X|}{
qqq qqqq qqqq qqqq qqqq qqqq qqqq qqqq qqqq qqqq qqqq qqq qq qq q q q qqqq qq}\\
c& zzz zzzz zzzzz z z zzz zzzzz & yyy yyy yyyy yyy yyy yyyy yyyy y yyyyy yyy yy y y \\
\end{tabularx}
\end{document}

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.