6

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?

10
  • 1
    welcome to tex.se! [t] determine position of table, not content in cells. instead of m{...} you should use p{...} column type.
    – Zarko
    Commented Feb 11, 2018 at 0:46
  • @Zarko That works for "Foo, Bar, Spam, Eggs" but not "Alpha, Beta".
    – visitor
    Commented Feb 11, 2018 at 0:49
  • of course not, because you use there \multirow. which is not necessary. just omit it.
    – Zarko
    Commented Feb 11, 2018 at 0:52
  • I need multirow because Alpha and Beta must span two rows. That can't be done without multirow, can it?
    – visitor
    Commented Feb 11, 2018 at 0:55
  • 2
    If you don't need \multirow for "Foo" and "Bar", why would you need it for "Alpha" and "Beta"?
    – gusbrs
    Commented Feb 11, 2018 at 0:57

1 Answer 1

4

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

6
  • The simple one got even better. It's a pity I'd already given you +1 before!
    – gusbrs
    Commented Feb 11, 2018 at 1:25
  • Unfortunately no cell can span multiple rows in your solution.
    – visitor
    Commented Feb 11, 2018 at 1:36
  • from your example is not seems any reason to use multirow cells, however if you so like it, i add this option ...
    – Zarko
    Commented Feb 11, 2018 at 7:45
  • Ah yes, the cell with "Beta Beta Beta..." runs on to the next row, which is what I want. But when I try to compile your code, I get the error messages "Missing number, treated as zero" and "Illegal unit of measure (pt inserted)" for \multirow[t]
    – visitor
    Commented Feb 11, 2018 at 17:03
  • Another minor problem is that text in multirow are not centered like the other text. Is it possible to center them?
    – visitor
    Commented Feb 11, 2018 at 17:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .