You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Inconsistent vertical space around table and longtable in PDF #3491
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
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)
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):
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:
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.
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.
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.
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).
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.
Activity
jfbu commentedon Mar 2, 2017
Thanks for report this is due to two different LaTeX packages used, in the first case
threeparttableand in second caselongtable. This is not only difference, if you try withyou 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
4inwhich 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
threeparttableis legacy usage in Sphinx. in LaTeX world it is used for its table notes, which Sphinx does not use. Thethreeparttablelike the tabular is an inline element, it needs wrapping into atableorcenteror other environment to become block level element. Thelongtableis from the start a block level element which unfortunately has an extremely idiosyncratic way of rendering captions.In particular
longtabledoes not react to\belowcaptionskipsetting like normal captions. Butthreeparttabledoes. For example, this shows it (big setting for demonstration purposes):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
longtableuses 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\captionby\\[\belowcaptionskip]for example. However, from later investigation (see next) I learned that longtable uses\baselineskipspace before caption, but the problem is that inside\\[..]the\baselineskiphas 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
longtabletemplate. (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
\paris not accepted and second and worse, vertical spaces are ignored if the caption natural width turns out smaller than\LTcapwidthparameter 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:You can see the vertical space is
\baselineskip. Thus with preamble key like thisyou 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.
jfbu commentedon Mar 2, 2017
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
preamblekey to add or remove vertical spaces.In the end classifying this issue as
bugbut 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.
jessetan commentedon Mar 2, 2017
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.
jfbu commentedon Mar 2, 2017
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).
Fix sphinx-doc#3491 via equalizing vertical spaces for latex tables o…
Fix sphinx-doc#3491 via equalizing vertical spaces for latex tables o…
Fix sphinx-doc#3491 via equalizing vertical spaces for latex tables o…
Fix sphinx-doc#3491 via equalizing vertical spaces for latex tables o…
jfbu commentedon Mar 4, 2017
The problem is fixed at #3504 for 1.6 release. It introduces following macros:
which can be renewcommand'd from
'preamble'key of latex_elements. Probably some interface via'sphinxsetup'key will be added.The
\sphinxtablepreand\sphinxtablepostcan hold negative dimensions. They serve as adjustments compared to the default which is distance between baselines of two successive paragraphs.jfbu commentedon Mar 4, 2017
Thanks for reporting, this will close automatically when #3504 is merged.
Merge pull request #3504 from jfbu/fixtablevspaces