[Tex/LaTex] Vertical alignment of multi column cells

multicolumntablesvertical alignment

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.
Current problem
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 in booktab package.

    \documentclass{article}
%\usepackage{multirow, hhline}
\usepackage{array,makecell,booktabs}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
\usepackage[svgnames,table]{xcolor}
%\usepackage{ragged2e}

    \begin{document}
\begin{table}
\centering
\caption{Add caption}
\begin{tabular}{rp{0.15\textwidth}p{0.15\textwidth}
                 M{0.35\textwidth}M{0.25\textwidth}}
    \cmidrule[1pt]{2-5}      \cmidrule(l){4-5}
    &   \multicolumn{2}{c}{MultiColumn1} 
                            &   \multicolumn{2}{c}{MultiColumn2}    \\
    \cmidrule(r){2-3}      \cmidrule(l){4-5}
    &   column1 &   column2 &   column3 &   column4                 \\
    \hline
row 1
    &   \multicolumn{2}{M{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.                        \\
    \hline
row 2
    &   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. \\
    \bottomrule
\end{tabular}%
\label{tab:addlabel}%
\end{table}%
    \end{document}

Obtained table is: enter image description here

I suggest you to look, if you like cell formatinng with \Centering as it provide package ragged2e. With it the text is centered, but long words are still hyphenated. With it you obtain:

enter image description here

Edit: Considering @LaRiFaRi comment, the alternative, nicer table you can obtain width:

    \documentclass{article}
\usepackage{array,makecell,booktabs}
\newcolumntype{M}[1]{>{\Centering\arraybackslash}m{#1}}
\usepackage[svgnames,table]{xcolor}
\usepackage{calc,ragged2e}
\usepackage{microtype}

    \begin{document}
\begin{table}
\centering
\caption{Add caption}
\begin{tabular}{rp{0.15\textwidth}p{0.15\textwidth}
                 M{0.35\textwidth}M{0.25\textwidth}}
    \cmidrule[1pt]{2-5}      %\cmidrule(l){4-5}
    &   \multicolumn{2}{c}{MultiColumn1} 
                            &   \multicolumn{2}{c}{MultiColumn2}    \\
    \cmidrule(r){2-3}      \cmidrule(l){4-5}
    &   column1 &   column2 &   column3 &   column4                 \\
    \hline
row 1
    &   \multicolumn{2}{M{0.3\textwidth+2\tabcolsep}}{\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.                        \\
    \hline
row 2
    &   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. \\
    \bottomrule
\end{tabular}%
\label{tab:addlabel}%
\end{table}%
    \end{document}