AlexP21 - 4 years ago 167
LaTeX Question

Creating a large table in rst with nested column headers and getting the latex rendering to wrap the header text

I need to create a wide table, like the example below, that will span the whole pdf page after it's rendered in latex.

The problem I'm having is that the column header text is not wrapping to fit the width of the column.

+----------+--------------------------------+------------------------+----------+----------+----------+----------+----------+
| Header 1 | Long Header 2 that should wrap | Common column Header 3 | Header 4 | Header 5 | Header 6 | Header 7 | Header 8 |
|          |                                +-----------+------------+          |          |          |          |          |
|          |                                | Header 3a | Header 3b  |          |          |          |          |          |
+==========+================================+===========+============+==========+==========+==========+==========+==========+
| Value 1  | Value 2 does actually wrap     | Value 3a  | Value 3b   | Value 4  | Value 5  | Value 6  | Value 7  | Value 8  |
+----------+--------------------------------+-----------+------------+----------+----------+----------+----------+----------+


Shows up like this:

I've tried adjusting column widths using the
.. tabularcolumns:: |p{0.1 \textwidth|...
directive, but it didn't seem to fix the encroaching header text problem.

If I remove the "Common column Header 3" (second example below) I get the desired wrapping header text behavior, so I'm guessing I'm doing something wrong with that part:

+----------+--------------------------------+-----------+------------+----------+----------+----------+----------+----------+
| Header 1 | Long Header 2 that should wrap | Header 3a | Header 3b  | Header 4 | Header 5 | Header 6 | Header 7 | Header 8 |
|          |                                |           |            |          |          |          |          |          |
|          |                                |           |            |          |          |          |          |          |
+==========+================================+===========+============+==========+==========+==========+==========+==========+
| Value 1  | Value 2 does actually wrap     | Value 3a  | Value 3b   | Value 4  | Value 5  | Value 6  | Value 7  | Value 8  |
+----------+--------------------------------+-----------+------------+----------+----------+----------+----------+----------+


Looks like this:

Any help would be greatly appreciated!

Answer Source

This problem is with the way the LaTeX multirow package is used. Sphinx produces this kind of mark-up:

   ...&\multirow{2}{*}{\relax \sphinxstylethead{\relax
Long Header 2 that should wrap
\unskip}\relax \unskip}\relax &...


but the multirow documentation says that * indicates to use the text parameter’s natural width and I don't know but perhaps this does not play well with tabulary rendering of the table. The alternative is to indicate an explicit width, which Sphinx can not guess in general: however it possibly could in case of presence of ..tabularcolumns as it would have the information then.

multirow is old package but has had recent update in september 2016 to v2.0 which introduces new possibility = for this parameter

to indicate that the specified width of the column in which the \multirow entry is set should be used.

I have tried manually inserting the = rather than * and it fixed your first example. As Sphinx LaTeX writer seemingly always uses \multirow with the {*}, I can propose this hack. Put in your conf.py

latex_elements = {
'preamble': r'\let\oldmultirow\multirow\def\multirow#1#2{\oldmultirow{#1}{=}}',
}


and make sure you are using a fully updated TeX installation as you need multirow 2.0. This means you need TeXLive 2016 up-to-date or MikTeX on Windows, up-to-date.

Sphinx can possibly consider using = but this will work only for users with such up-to-date TeX installations. Maybe there are better ways, but this one worked for me on your example 1 (no need then for ..tabularcolumns:: directive), on a4 paper and with Sphinx 1.5.2. (the table is longer than text linewidth)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download