I'm working on a complicated table, having multiple columns and multiple rows and each column needs a fixed width. When a cell has a long sentence, I want the cell fixed in width, while other cells in the same row are aligned center vertically. I found a way using
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
but this doesn't work if the cell is multicolumned.
Here is my example:
\usepackage{multirow, hhline}
\usepackage{array,booktabs}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
\usepackage{colortbl}
\begin{table}[htbp]
\centering
\caption{Add caption}
\begin{tabular}{rp{0.15\textwidth}p{0.15\textwidth}M{0.35\textwidth}M{0.25\textwidth}}
\multicolumn{1}{c}{\multirow{2}[0]{*}{}} & \multicolumn{2}{c}{MultiColumn1} & \multicolumn{2}{c}{MultiColumn2} \\
\hhline{~----}
\multicolumn{1}{c}{} & column1 & column2 & column3 & column4 \\
\hhline{-----}
row1 & \multicolumn{2}{p{0.3\textwidth}}{\cellcolor{blue!25} I want this sentence vertically aligned in center} & Here comes a long sentence. Here comes a long sentence. Here comes a long sentence. & Here comes a long sentence. Here comes a long sentence. Here comes a long sentence. \\
\hhline{-----}
row2 & Contents 1 & Contents 2 & Here comes a long sentence. Here comes a long sentence. Here comes a long sentence. & Here comes a long sentence. Here comes a long sentence. Here comes a long sentence. \\
\end{tabular}%
\label{tab:addlabel}%
\end{table}%
It comes as below.
I would like the sentence in the colored cell aligned vertically in center.
Does any one have an idea??
Thanks.
Best Answer
You almost reach your goal :-). for centering blue colored cell you just need to use
\multicolumn{2}{M{0.3\textwidth}}{....}
. I allow myself to remove unnecessary code in your table. Also not use ``hhline˙package (I haven't installed it), anyway to my taste is nicer to use rules as defined inbooktab
package.Obtained table is:
I suggest you to look, if you like cell formatinng with
\Centering
as it provide packageragged2e
. With it the text is centered, but long words are still hyphenated. With it you obtain:Edit: Considering @LaRiFaRi comment, the alternative, nicer table you can obtain width: