1

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>
            Heading 1 <br>
            A <br>
            B <br>
            C <br>
            D
        </td>
        <td rowspan="2">
            Heading 2 <br>
            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>
            Heading 3 <br>
            A <br>
            B <br>
            C <br>
            D
        </td>
        <td>
            Heading 4 <br>
            A <br>
            B <br>
            C <br>
            D <br>
            E
        </td>
    </tr>
    <tr>
        <td>
            Heading 5 <br>
            A <br>
            B <br>
            C <br>
            D <br>
            E
        </td>
        <td rowspan="2">
            Heading 6 <br>
            A <br>
            B <br>
            C <br>
            D <br>
            E <br>
            F <br>
            G <br>
            H <br>
            I
        </td>
        <td>
            Heading 7 <br>
            A <br>
            B <br>
            C <br>
            D <br>
            E <br>
            F <br>
            G
        </td>
    </tr>
    <tr>
        <td>
            Heading 8 <br>
            A <br>
            B <br>
            C <br>
            D <br>
            E
        </td>
        <td>
            Heading 9 <br>
            A <br>
            B <br>
            C <br>
            D
        </td>
        <td>
            Heading 10 <br>
            A <br>
            B <br>
            C <br>
            D <br>
            E
        </td>
    </tr>
</table>

This renders a table that appears roughly like this: Table layout, with 4 columns and 3 rows. In the 2nd column, row 1 and 2 are merged. In the 3rd column, row 2 and 3 are merged.

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@{}}
                Heading 1 \\
                A \\
                B \\
                C \\
                D
            \end{tabular} &
            \multirow{2}{*}{
                \begin{tabular}[c]{@{}l@{}}
                    Heading 2 \\
                    A \\
                    B \\
                    C \\
                    D \\
                    E \\
                    F \\
                    G \\
                    H \\
                    I \\
                    J \\
                    K \\
                    L \\
                    M \\
                    N \\
                    O \\
                    P
                \end{tabular}
            } &
            \begin{tabular}[c]{@{}l@{}}
                Heading 3 \\
                A \\
                B \\
                C \\
                D
            \end{tabular} &
            \begin{tabular}[c]{@{}l@{}}
                Heading 4 \\
                A \\
                B \\
                C \\
                D \\
                E
            \end{tabular} \\
            \hline
            \begin{tabular}[c]{@{}l@{}}
                Heading 5 \\
                A \\
                B \\
                C \\
                D \\
                E
            \end{tabular} & &
            \multirow{2}{*}{
                \begin{tabular}[c]{@{}l@{}}
                    Heading 6 \\
                    A \\
                    B \\
                    C \\
                    D \\
                    E \\
                    F \\
                    G \\
                    H \\
                    I
                \end{tabular}
            } &
            \begin{tabular}[c]{@{}l@{}}
                Heading 7 \\
                A \\
                B \\
                C \\
                D \\
                E \\
                F \\
                G
            \end{tabular} \\
            \hline
            \begin{tabular}[c]{@{}l@{}}
                Heading 8 \\
                A \\
                B \\
                C \\
                D \\
                E
            \end{tabular} &
            \begin{tabular}[c]{@{}l@{}}
                Heading 9 \\
                A \\
                B \\
                C \\
                D
            \end{tabular} & &
            \begin{tabular}[c]{@{}l@{}}
                Heading 10 \\
                A \\
                B \\
                C \\
                D \\
                E
            \end{tabular} \\
            \hline
        \end{tabular}
    \end{table}
\end{document}

As seen below, this results in overflows and weird aligns: Mangled LaTeX table layout

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.

3
  • 1
    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
0

Not sure if this is what you are looking for:

mwe

\documentclass[]{article}
\usepackage[margin=1cm]{geometry}
\usepackage{multirow,lipsum,tabularx}
\let\xpar\par
\def\heading#1{\vskip-10pt\textbf{Heading #1}\par}
\newcolumntype{T}{>{\parskip2ex}X}
\begin{document}
\noindent
\begin{tabularx}{\linewidth}{|T|T|T|T|}\hline
\heading A \lipsum[1][1]\lipsum[1][0]\par\lipsum[1][1]\par\lipsum[1][2] &
\multirow{2}{\linewidth}{\parskip2ex plus 2ex minus 1ex  
\heading B 
\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]
}
 & 
\heading C \lipsum[3][1]\lipsum[3][0]\par\lipsum[3][3]\par\lipsum[3][2] & 
\heading D \lipsum[4][1]\lipsum[4][0]\par\lipsum[4][1]\par\lipsum[4][2] \\\cline{1-1}\cline{3-4}
\heading E \lipsum[5][1]\lipsum[5][0]\par\lipsum[5][1]\par\lipsum[5][2-4] & 
 & % empty cell 
\multirow{2}{\linewidth}{\parskip2ex plus 2ex minus 1ex 
\heading G 
\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 H \lipsum[8][1]\lipsum[1][0]\par\lipsum[8][1] \\\cline{1-2}\cline{4-4}
\heading I \lipsum[9][1]\lipsum[9][0]\par\lipsum[9][1] \smallskip & % ending skip  
\heading J \lipsum[10][1]\lipsum[10][0]\par\lipsum[10][1]\par\lipsum[10][2] & 
 & % empty cell
\heading L \lipsum[11][11]\lipsum[11][0]\par\lipsum[11][1]\par\lipsum[11][2] \\\hline
\end{tabularx}

\end{document}

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.

1
  • Thank you very much, this is pretty much perfect. – Wynand Marais Mar 6 '20 at 5:55

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.