# Emulate html rowspan and colspan layout

I have an html file that passes through wkhtmltopdf to render a PDF. I would like to translate it to LaTeX, due to its better handling of footers, pagebreaks and so on.

The specific problem I'm currently having lies with a table defined in the html. Here's a rough MWE:

<style>
table { width: 100% }
td {
border: 1px solid black;
vertical-align: top;
}
</style>

<table class>
<tr>
<td>
A <br>
B <br>
C <br>
D
</td>
<td rowspan="2">
A <br>
B <br>
C <br>
D <br>
E <br>
F <br>
G <br>
H <br>
I <br>
J <br>
K <br>
L <br>
M <br>
N <br>
O <br>
P
</td>
<td>
A <br>
B <br>
C <br>
D
</td>
<td>
A <br>
B <br>
C <br>
D <br>
E
</td>
</tr>
<tr>
<td>
A <br>
B <br>
C <br>
D <br>
E
</td>
<td rowspan="2">
A <br>
B <br>
C <br>
D <br>
E <br>
F <br>
G <br>
H <br>
I
</td>
<td>
A <br>
B <br>
C <br>
D <br>
E <br>
F <br>
G
</td>
</tr>
<tr>
<td>
A <br>
B <br>
C <br>
D <br>
E
</td>
<td>
A <br>
B <br>
C <br>
D
</td>
<td>
A <br>
B <br>
C <br>
D <br>
E
</td>
</tr>
</table>


This renders a table that appears roughly like this:

Note that the cells in each row are aligned, and where the output is larger than the minimum allotted cell size, as in the one merged cell, all the other cells gain empty space to keep aligned.

Also note that the items A, B, C... are key phrases, generally short enough to fit in their cells, but very occasionally large enough to fill 2 or 3 lines. Thus, they must be able to overflow onto multiple lines in their column.

I am having some difficulty preventing overflow, getting text to align, and drawing borders around the cells in LaTeX. The best I have working so far is the following, using minipages:

\documentclass[]{article}
\usepackage[margin=1cm]{geometry}
\usepackage{multirow}
\begin{document}
\begin{table}[]
\begin{tabular}{|p{4cm}|p{4cm}|p{4cm}|p{4cm}|}
\hline
\begin{tabular}[c]{@{}l@{}}
A \\
B \\
C \\
D
\end{tabular} &
\multirow{2}{*}{
\begin{tabular}[c]{@{}l@{}}
A \\
B \\
C \\
D \\
E \\
F \\
G \\
H \\
I \\
J \\
K \\
L \\
M \\
N \\
O \\
P
\end{tabular}
} &
\begin{tabular}[c]{@{}l@{}}
A \\
B \\
C \\
D
\end{tabular} &
\begin{tabular}[c]{@{}l@{}}
A \\
B \\
C \\
D \\
E
\end{tabular} \\
\hline
\begin{tabular}[c]{@{}l@{}}
A \\
B \\
C \\
D \\
E
\end{tabular} & &
\multirow{2}{*}{
\begin{tabular}[c]{@{}l@{}}
A \\
B \\
C \\
D \\
E \\
F \\
G \\
H \\
I
\end{tabular}
} &
\begin{tabular}[c]{@{}l@{}}
A \\
B \\
C \\
D \\
E \\
F \\
G
\end{tabular} \\
\hline
\begin{tabular}[c]{@{}l@{}}
A \\
B \\
C \\
D \\
E
\end{tabular} &
\begin{tabular}[c]{@{}l@{}}
A \\
B \\
C \\
D
\end{tabular} & &
\begin{tabular}[c]{@{}l@{}}
A \\
B \\
C \\
D \\
E
\end{tabular} \\
\hline
\end{tabular}
\end{table}
\end{document}


As seen below, this results in overflows and weird aligns:

Trying to add \hline to the individual cells doesn't improve matters either.

Is there a way to implement the layout shown in the html output in LaTeX, while preserving some html characteristics like growing the cells to fit the content, keeping cells in a given row aligned, and also preserving the borders? (Note, the borders don't have to appear around each individual square, it's preferable if they appear like a standard Word table, with continuous borders against each other.)

One further note - appearance-wise, the table as requested doesn't adhere to best practices, which I am well aware of. However, it is per client request, so I am not at liberty to rearrange the table layout to any great extent.

• While imho one can do it in latex, I wouldn't do it with a tabular - rowspan is difficult there. I would create boxes, measure them and then place them. I suggest to look in the tcolorbox documentation at the raster and the poster library for ideas. – Ulrike Fischer Mar 3 '20 at 8:34
• Each cell content must be also a table? In real life, items A, B, C, etc. are a list of words, single line items or paragraphs of whatever length or any type of content? – Fran Mar 3 '20 at 22:21
• @Fran Will amend in the main thread quickly - the items underneath the heading are sentences, typically only as wide as the cell or less, though in some cases they will be multi-line sentences (only 2 or 3, though). Technically, then, they would fall under the category of paragraphs of arbitrary length – Wynand Marais Mar 4 '20 at 8:35

Not sure if this is what you are looking for:

\documentclass[]{article}
\usepackage[margin=1cm]{geometry}
\usepackage{multirow,lipsum,tabularx}
\let\xpar\par
\newcolumntype{T}{>{\parskip2ex}X}
\begin{document}
\noindent
\begin{tabularx}{\linewidth}{|T|T|T|T|}\hline
\multirow{2}{\linewidth}{\parskip2ex plus 2ex minus 1ex
\xpar\lipsum[2][1]\lipsum[2][0]\xpar\lipsum[2][1]\xpar\lipsum[2][2]
\lipsum[2][3]\lipsum[2][4]\xpar\lipsum[1][1]
}
&
& % empty cell
\multirow{2}{\linewidth}{\parskip2ex plus 2ex minus 1ex
\xpar\lipsum[6][1]\lipsum[6][0]\xpar\lipsum[6][1]\xpar\lipsum[6][2]
\lipsum[7][1]\lipsum[7][0]\xpar\lipsum[7][1]\xpar\lipsum[7][2]
\lipsum[7][3]\xpar\lipsum[7][4-11]
}
&
\heading I \lipsum[9][1]\lipsum[9][0]\par\lipsum[9][1] \smallskip & % ending skip

The approach, in summary, is simply multirow cells (in a tabularx to easily fix the widths of columns) but paragraph are a bit tricky in these cells, so the use of \parbox with a special \xpar (alternatively, you can use linebreaks with extra space as \\[3ex] to make fake paragraph breaks).
Note also that the height of the multirow cells is not expanded according the inner \parbox content but the contents of the other cells, so you add too much text in cell B, it will overlap with cell J.