2

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.

2

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}
  • Delete the first \cmidrule(l){4-5} which is wrong, load microtype and caption, and extend the blue box as said in my last comment of my answer. Then your second approach will be perfect. – LaRiFaRi Aug 11 '15 at 12:57
  • @LaRiFaRi, ah copy-past error. Of course it is superfluous. With microtype all will look even more nicer. I will upgrade my unswer with your suggestion. And sory, when I already pres key submit I see, that already (now I see almost the same) answer. – Zarko Aug 11 '15 at 13:05
  • Don't you worry. I just learned about \Centering from your answer. And I have this package loaded all the time... So that's worthy enough. +1 – LaRiFaRi Aug 11 '15 at 13:06
  • Perfect! Thank you very much to both LaRiFaRi and Zarko! – user2011987 Aug 12 '15 at 7:16
2

For me, M seems to work in \multicolumn or did I understand something wrong?

% arara: pdflatex

\documentclass{article}
\usepackage{hhline}
\usepackage{array,booktabs}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
\usepackage[table]{xcolor}
\usepackage{caption} % better spacing around the caption

\begin{document}   
\setcounter{table}{1}   
\begin{table}[htbp]
    \centering
    \caption{Add caption}\label{tab:addlabel}
    \begin{tabular}{rM{0.15\textwidth}M{0.15\textwidth}M{0.35\textwidth}M{0.25\textwidth}}
         & \multicolumn{2}{c}{MultiColumn1} & \multicolumn{2}{c}{MultiColumn2} \\
        \hhline{~----}
        & column1 & column2 & column3 & column4 \\
        \hhline{-----}
        row1 & \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. \\
        \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}
\end{table}
\end{document}

enter image description here


However, I would recommend to load the package calc and to do \multicolumn{2}{M{.3\textwidth+2\tabcolsep}} instead.

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.