[Tex/LaTex] Multi-line Multi-row cells

multirowtables

I am trying to create a 4×2 table with two multi-row cells, which join cells (1,2) and (2,2) and (1,3) and (2,3) respectively.

The 1st multi-row cell should contain 2 lines and the second 3 lines:

+---+---+---+---+
| A | B | D | G |
+---+   | E +---+
| H | C | F | I |
+---+---+---+---+

Here is my first attempt:

\begin{tabular}{||c|c|c|c||}\hline\hline
A & \multirow{2}{*}{B \\ C} & \multirow{2}{*}{D \\ E \\ F} & G \\ \cline{1-1}\cline{4-4}
H &   &    & I \\ \hline\hline
\end{tabular}

The error (on the multirow line) I get is

! LaTeX Error: Something's wrong--perhaps a missing \item.

and the newlines (\\) inside multirows are ignored.

I also tried putting tabular inside multirow (as per How to add a forced line break inside a table cell):

\begin{tabular}{||c|c|c|c||}\hline\hline
A & \multirow{2}{*}{\begin{tabular}{c} B \\ C \end{tabular}} &
  \multirow{2}{*}{\begin{tabular}{c} D \\ E \\ F \end{tabular}} & G 
   \\ \cline{1-1}\cline{4-4}
H &   &    & I \\ \hline\hline
\end{tabular}

and there were no errors, but F was placed below the last two horizontal lines (i.e., the two rows in the table cannot accommodate the 3 rows in the 2nd multirow cell):

+---+---+---+---+
| A | B | D | G |
+---+   |   +---+
| H | C | E | I |
+---+---+---+---+
          F

The 3rd attempt was to pass an actual width in cm instead of {*} to multirow. This works in my case (because the chunks into which I break the multi-row cells have the same width), but the 3rd line (F) is still below the table – i.e., the table height is not increased to accommodate the multi-row cells.

So, my questions are:

  • how can I tell multirow where to break lines?
  • how do I tell tabular to make space for the multi-row cells?

Best Answer

I propose this solution. I loaded hhline for better intersecting double lines, and stackengine. The spacing required by multirow cells can be obtained with the cellspace package, which defines a minimal vertical padding for cells of columns with a qualifier prefixed by the letter S.

In case you really need line breaks in a \multirow the makecell package defines a\multirowcell command that allows for line breaks:

\documentclass{article}
\usepackage{array}

\usepackage{multirow}
\usepackage{cellspace}
\setlength\cellspacetoplimit{5pt}
\setlength\cellspacebottomlimit{5pt}
\usepackage{hhline}
\usepackage{stackengine}
\setstackEOL{\\}


\begin{document}

\begin{tabular}{||Sc|c|c|c||}
  \hhline{|t:====:t|}
  A &
  B & \multirow{2}{*} {\setstackgap{L}{2.05ex}\Centerstack{D\\E\\F}}
  & G \\ %
  \hhline{||-|~|~|-||}
  H & C & & I \\
  \hhline{|b:====:b|}
\end{tabular}

\end{document} 

enter image description here