[Tex/LaTex] Multirow and top alignment in tabular

multirowtablesvertical alignment

I want to vertically align text to the top of every cell, in a tabular environment where some cells must span multiple rows.

Try not to use other packages if possible. (I've no control over installation of additional packages.)

\documentclass[12pt, margin = 1mm]{standalone}
\newcommand\TS{\rule{0pt}{2.6ex}}         % Top strut
\newcommand\BS{\rule[-0.9ex]{0pt}{0pt}}   % Bottom strut
\usepackage{array, multirow}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\TS\BS\arraybackslash}p{#1}}
\newcolumntype{C}[1]{>{\centering  \let\newline\\\TS\BS\arraybackslash}p{#1}}
\newcolumntype{R}[1]{>{\raggedleft \let\newline\\\TS\BS\arraybackslash}p{#1}}

\begin{document}
\begin{tabular} [t] { C{2cm} C{3cm} | C{2cm} C{3cm} }
    Column 1 & Column 2 & Column 3 & Column 4 \\ \hline
    Foo & Line 1 \newline Line 2 &
    \multirow{2}{*}{Alpha} & Line 1 \newline Line 2 \newline Line 3 \\
    Bar & Line 1 \newline Line 2 \\
    Spam & Line 1 \newline Line 2 &
    \multirow{2}{*}{Beta} & Line 1 \newline Line 2 \newline Line 3 \\
    Eggs & Line 1 \newline Line 2 \\
\end{tabular}
\end{document}

enter image description here

There are two problems:

  1. Using p{#1} instead of m{#1} helps to align Foo, Bar, Spam, Eggs to the top, but not Alpha, Beta. The [t] argument didn't work.

  2. Alpha and its 3 lines do not span the rows of Foo, Bar. Likewise for Beta.

How can we solve both problems?

Best Answer

like this?

enter image description here

without multirow, with simplified types of columns definitions and with use of makecell:

\documentclass[12pt, margin=3mm]{standalone}
\usepackage{array,  makecell}
\setcellgapes{3pt}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}}

\begin{document}
{\makegapedcells
    \begin{tabular} { C{2cm} C{3cm} | C{2cm} C{3cm} }
Column 1    & Column 2 & Column 3 & Column 4 \\
    \hline
Foo         & \makecell[t]{Line 1\\ Line 2} & Alpha & \makecell[t]{Line 1\\ Line 2} \\
Bar         & \makecell[t]{Line 1\\ Line 2} &       &   \\
Spam        & \makecell[t]{Line 1\\ Line 2} & Beta  & \makecell[t]{Line 1\\ Line 2}  \\
Eggs        & \makecell[t]{Line 1\\ Line 2} &       & \\
    \end{tabular}
}
\end{document}

edit (1): or you looking for for simple table:

{\makegapedcells
    \begin{tabular} { C{2cm} C{3cm} | C{2cm} C{3cm} }
Column 1    & Column 2 & Column 3 & Column 4 \\
    \hline
Foo         & Line 1    & Alpha & Line 1    \\ 
            & Line 2    &       & Line 2    \\ 
Bar         & Line 1    &       &           \\
            & Line 2    &       &           \\
Spam        & Line 1    & Beta  & Line 1    \\
            & Line 2    &       & Line 2    \\ 
Eggs        & Line 1    &       &           \\
            & Line 2    &       &           \\
    \end{tabular}
}

which has slightly different vertical spacing:

enter image description here

edit(2): solution with multirow (as op so much desire, however which/how many rows should span is unfortunately no clear to me)

\documentclass[12pt, margin=3mm]{standalone}
\usepackage{array,  makecell, multirow}
\setcellgapes{3pt}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}}

\begin{document}
{\makegapedcells
 \renewcommand\multirowsetup{\centering}% <-- for centering contents of multirow center
    \begin{tabular} { C{2cm} C{3cm} | C{2cm} C{3cm} }
Column 1    & Column 2 & Column 3 & Column 4 \\
    \hline
\multirow[t]{2}{=}{Foo} & Line 1    & \multirow[t]{4}{=}{Alpha} & Line 1    \\ 
                        & Line 2    &                           & Line 2    \\ 
\multirow[t]{2}{=}{Bar} & Line 1    &                           &           \\
                        & Line 2    &                           &           \\
\multirow[t]{2}{=}{Spam}& Line 1    & \multirow[t]{4}{=}{Beta}  & Line 1    \\
                        & Line 2    &                           & Line 2    \\ 
\multirow[t]{2}{=}{Eggs}& Line 1    &                           &           \\
                        & Line 2    &                           &           \\
    \end{tabular}
}
\end{document}

the result is is look the same as before (in edit (1)) if only one word is in multirows cell. in case that there are more, for example as:

    \begin{tabular} { C{2cm} C{3cm} | C{2cm} C{3cm} }
Column 1    & Column 2 & Column 3 & Column 4 \\
    \hline
\multirow[t]{2}{=}{Foo} & Line 1    & \multirow[t]{4}{=}{Alpha} & Line 1    \\ 
                        & Line 2    &                           & Line 2    \\ 
\multirow[t]{2}{=}{Bar} & Line 1    &                           &           \\
                        & Line 2    &                           &           \\
\multirow[t]{2}{=}{Spam}& Line 1    & \multirow[t]{4}{=}{Beta Beta Beta Beta Beta
                                                         Beta Beta Beta Beta Beta}  
                                                                & Line 1    \\
                        & Line 2    &                           & Line 2    \\ 
\multirow[t]{2}{=}{Eggs}& Line 1    &                           &           \\
                        & Line 2    &                           &           \\
    \end{tabular}

the result is:

enter image description here

Related Question