Skip to content

Inconsistent vertical space around table and longtable in PDF #3491

Closed

Description

@jessetan

Problem

The vertical whitespace above and below a table is different from a longtable (explicitely set using :class: or implicitly by having more than 30 rows)

Procedure to reproduce the problem

Generate a PDF from the following rst:

This is a paragraph.
A centered regular table will now follow.

.. table:: This is a table
    :align: center

    =========== ========
    Header 1    Header 2
    =========== ========
    Cell 1      Cell 2
    Cell 3      Cell 4
    =========== ========

This is a paragraph.
A longtable will now follow.

.. table:: This is a longtable
    :class: longtable

    =========== ========
    Header 1    Header 2
    =========== ========
    Cell 1      Cell 2
    Cell 3      Cell 4
    =========== ========

This is a paragraph. No table will follow.

Results

table vertical space

In the screenshot, the first table has more vertical space above than the second table. It has less vertical space below than the second table. The second table has more vertical space between the caption and the table.

I have reproduced this on the most recent development version of sphinx by doing pip install git+https://github.com/sphinx-doc/sphinx.git inside a virtualenv, I assumed this would give me master.
Possibly related: #3415, #3158, and #3405

Expected results

Vertical space identical for both tables. To me, the space around the second table looks more balanced, but I also like the reduced space between caption and table of the first table.

Environment info

  • OS: OSX 10.10.5
  • Python version: 2.7.13
  • Sphinx version: Screenshot from 1.6 master cloned on 2017-3-2, but also seen on 1.5.2
  • PDF generated using pdfTeX, Version 3.14159265-2.6-1.40.17 (TeX Live 2016)

Activity

  1. jfbu commented on Mar 2, 2017

    @jfbu
    Contributor

    Thanks for report this is due to two different LaTeX packages used, in the first case threeparttable and in second case longtable. This is not only difference, if you try with

     This is a table with a very long title very long indeed
    

    you will see in first case it is wrapped to table width, which it is not in second case (well, I just learn now from its docs that longtable wraps the caption to a default width of 4in which one can change but I don't see an option to set it to the width of the table).

    Side remark: in your screenshot the weird width of second column of longtable is due to the fact that Sphinx configures longtable with something like "table continued from previous page" header, and longtable takes the width of that into account for the total width of the table (but this width goes to last column!) (*), even if like here the table is not split across pages !!

    (*) I must look into this in a more detailed way. Because this is very annoying.

    Package threeparttable is legacy usage in Sphinx. in LaTeX world it is used for its table notes, which Sphinx does not use. The threeparttable like the tabular is an inline element, it needs wrapping into a table or center or other environment to become block level element. The longtable is from the start a block level element which unfortunately has an extremely idiosyncratic way of rendering captions.

    In particular longtable does not react to \belowcaptionskip setting like normal captions. But threeparttable does. For example, this shows it (big setting for demonstration purposes):

    latex_elements = {
        'preamble': '\\setlength{\\belowcaptionskip}{30pt}',
    }
    

    but setting this will affect I think captions above literal blocks, hence you probably don't want that. The dev version of Sphinx (future 1.6) has table templates but they are in a state of flux. But the simplest for controlling threeparttable if still used will be to insert LaTeX command like the above in the template (either after \begin{threeparttable} or after the \begin{savenotes} which is currently before it in template).

    The longtable uses its own codepath for caption, which is in fact like a special row in the table (which uses LaTeX's \multicolumn). There is no user interface to customize it, but in the LaTeX markup what one can do is replace the first \\ after the \caption by \\[\belowcaptionskip] for example. However, from later investigation (see next) I learned that longtable uses \baselineskip space before caption, but the problem is that inside \\[..] the \baselineskip has value zero (arrggh!) hence doing \\[\dimexpr\belowcaptionskip-\baselineskip\relax] does not have expected effect, and one must do something explicit \\[\dimexpr\belowcaptionskip-1Opt\relax]. Besides it would be better here to use own custom length \mybelowtablecaptionskip.

    Again this is trivial to do on Sphinx dev version which has public table templates. You only need to do the replacement in the longtable template. (not so trivial after added remarks above)

    With current Sphinx 1.5.x series or earlier, I experimented with some LaTeX hack to insert a vertical space in caption argument, but first \par is not accepted and second and worse, vertical spaces are ignored if the caption natural width turns out smaller than \LTcapwidth parameter mentioned above, as I determined in the end by looking at longtable's code. It appears only solution then is to redefine a longtable inner macro whose original is:

    \def\LT@makecaption#1#2#3{%
      \LT@mcol\LT@cols c{\hbox to\z@{\hss\parbox[t]\LTcapwidth{%
        \sbox\@tempboxa{#1{#2: }#3}%
        \ifdim\wd\@tempboxa>\hsize
          #1{#2: }#3%
        \else
          \hbox to\hsize{\hfil\box\@tempboxa\hfil}%
        \fi
        \endgraf\vskip\baselineskip}%
      \hss}}}
    

    You can see the vertical space is \baselineskip. Thus with preamble key like this

    latex_elements = {
        'preamble': r"""
    \newlength{\mylongtablebelowcaptionskip}
    \setlength{\mylongtablebelowcaptionskip}{0pt}% or whatever
    \makeatletter
    \def\LT@makecaption#1#2#3{%
      \LT@mcol\LT@cols c{\hbox to\z@{\hss\parbox[t]\LTcapwidth{%
        \sbox\@tempboxa{#1{#2: }#3}%
        \ifdim\wd\@tempboxa>\hsize
          #1{#2: }#3%
        \else
          \hbox to\hsize{\hfil\box\@tempboxa\hfil}%
        \fi
        \endgraf\vskip\mylongtablebelowcaptionskip}%
    %                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      \hss}}}
    \makeatother
    """
    }
    

    you gain complete control about below caption vertical space for longtable.

    In short, things will be easier with Sphinx 1.6 table templates. But they (the things) are complicated due to the fact that LaTeX is a mess and Sphinx has to fight it.

  2. jfbu commented on Mar 2, 2017

    @jfbu
    Contributor

    I realize I have focused on below caption space only. I will examine above caption space later ... perhaps you can already get information on LaTeX fora.

    The problem as you can see is that any hack like the one I indicated for longtable (and the investigation and testing time it requires) will be made useless if we change LaTeX packages. Worse, there may be some packages out there hacking around threeparttable or longtable issues, thus we can not make this sphinx-core, else we might break some Sphinx's user advanced customizations.

    We could get the table templates first introduced at #3389 rich enough to have some LaTeX macros inserted which can be configured from preamble key to add or remove vertical spaces.

    In the end classifying this issue as bug but its resolution should take place in general framework. It is better to make this part of the process of decision about which LaTeX packages Sphinx should use for tables.

    As Sphinx 1.6 will support Ubuntu/trusty type of TeX installation (#3070) which is TL2013 snapshot, it is easier now.

  3. added this to the 1.6 milestone on Mar 2, 2017
  4. jessetan commented on Mar 2, 2017

    @jessetan
    ContributorAuthor

    Thanks for your detailed explanation. I agree that this is something to look at when deciding which packages to use, rather than trying to fix it now.
    For now we'll most likely just use longtable for most tables and live with any spacing differences we have.

  5. jfbu commented on Mar 2, 2017

    @jfbu
    Contributor

    I am sorry for having given very detailed explanation on a secondary question (space between caption and table)... As per your original queries about space before and after table, here is why I can say:

    • threeparttable

      • above caption: there is 18 pt (baselineskip + parskip) from previous base line to top of T of Table. Customizable by threeparttable user: no. Must manually insert vspace.

      • between caption and table: governed by \belowcaptionskip. But setting this affects all other captions.

      • below table: no special spacing. Customizable by threeparttable user: no. Must manually insert vspace.

    • longtable

      • above caption: there is a big skip amount. Customizable: yes, \LTpre.

      • between caption and table: additionally to inter-row space there is a baselineskip. Customizable by user of longtable: not at all. Needs to overwrite inner macro of longtable. See my detailed answer above.

      • below table: there is a big skip amount. Customizable: yes \LTpost.

    The dev version of Sphinx, which you tested comes with table templates. They were introduced by @tk0miya at #3389. But (I should have checked) I don't think they can currently be overriden by user by files in project _templates/ repertory. Maybe I made some remarks above where I was erroneously thinking it to be already possible. Sorry about that.

    But when it becomes possible these are good locations to insert extra or correcting vertical spacing, rather than go via patching or hacking macros. In the mean time perhaps packages will have changed. (but there aren't many alternatives to longtable).

  6. added 4 commits that reference this issue on Mar 3, 2017
    e17736a
    5403e7b
    d33f075
    5f71a2c
  7. jfbu commented on Mar 4, 2017

    @jfbu
    Contributor

    The problem is fixed at #3504 for 1.6 release. It introduces following macros:

    \newcommand*\sphinxtablepre {0pt}% extra space before table
    \newcommand*\sphinxtablepost{\medskipamount}% extra space after table
    % as one can not use \baselineskip from inside longtable (it is zero there)
    % we need \sphinxbaselineskip, which defaults to \baselineskip
    \newcommand*\sphinxbelowcaptionspace{.5\sphinxbaselineskip}%
    

    which can be renewcommand'd from 'preamble' key of latex_elements. Probably some interface via 'sphinxsetup' key will be added.

    The \sphinxtablepre and \sphinxtablepost can hold negative dimensions. They serve as adjustments compared to the default which is distance between baselines of two successive paragraphs.

  8. jfbu commented on Mar 4, 2017

    @jfbu
    Contributor

    Thanks for reporting, this will close automatically when #3504 is merged.

  9. added a commit that references this issue on Mar 4, 2017
    469d261
  10. locked as resolved and limited conversation to collaborators on Aug 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions