Menu

#444 Table column width rounding can result in uneven column widths

closed-fixed
nobody
None
5
2022-07-06
2022-02-10
No

In the HTML generated for a table with 8 equal-width columns, the column widths in the generated colgroups are all rounded up from 12.5% to 13%, resulting in the last column being much too narrow (0.5 * 7 = 3.5, 3.5/12.5 = 0.28, so the accumulated error is almost 30%). The fix would be to revise the rounding in depart_colspec in _html_base.py to produce at least one decimal place, instead of rounding to the nearest integer.

Discussion

  • Alex Forencich

    Alex Forencich - 2022-02-10

    And if it's necessary for the width to be an integer for some reason, then an alternative fix could be to keep track of the accumulated error, and round up or down depending on which would minimize the error. In this case, the column widths should get set to 13, 12, 13, 12, etc. which would distribute the error across the columns, instead of having the last column off by such a large amount.

     
  • Günter Milde

    Günter Milde - 2022-02-11

    Thank you for the report.

    Unless the problem is fixed, there is a workaround:

    Tables works fine with Docutils 0.18.1 and rst2html5.py "out of the box",
    because of the new default (colwidths-auto).
    https://docutils.sourceforge.io/RELEASE-NOTES.html#release-0-18-2021-10-26

    With "html4css1" and "latex", you can use the config setting
    table-style: colwidths-auto or command line option
    --table-style=colwidth-auto (for the complete document)
    or :widths: auto on individual tables.
    This should also work for Docutils 0.17.

    Compare:

    .. table::
       :width: 100%
       :widths: auto
    
       ===  ===  ===  ===  ===  ===  ===  ===
        x    x    x    x    x    x    x    x 
       ===  ===  ===  ===  ===  ===  ===  ===
    
    .. table::
       :width: 100%
       :widths: grid
    
       ===  ===  ===  ===  ===  ===  ===  ===
        x    x    x    x    x    x    x    x 
       ===  ===  ===  ===  ===  ===  ===  ===
    
     
  • Alex Forencich

    Alex Forencich - 2022-02-11

    Here is what I have tried, all in combination with :width: 100%:

    With no :widths:, all columns in HTML output are 13%, so the last column is too narrow. In LaTeX output, the table doesn't fit the page width (:width: 100% seems to be ignored).

    With :widths auto, the column widths in HTML are not specified, and hence the columns are all different widths, depending on the contents. In LaTeX output, the table doesn't fit the page width (:width: 100% seems to be ignored).

    With :widths: 1 1 1 1 1 1 1 1 or :widths: grid, all columns in HTML output are 13%, so the last column is too narrow. In LaTeX output, the table fits the full page width and all columns are the same width.

    Also note that I am using docutils via sphinx, I'm not sure if sphinx messes with anything in between.

     
  • Günter Milde

    Günter Milde - 2022-02-19
    • status: open --> open-fixed
     
  • Günter Milde

    Günter Milde - 2022-02-19

    Fixed in [r9009].
    The Docutils LaTeX writer already uses 3 digit precision and fixed the "ignored width" bug with the new algorithm for table column widths in release 0.18 (cf. [bugs:#422]). Sphinx uses a LaTeX writer fork so using Docutils >= 0.18 will not fix Sphinx behaviour "automagically".

    Thank you for your report.

     

    Related

    Bugs: #422
    Commit: [r9009]

  • Alex Forencich

    Alex Forencich - 2022-02-20

    Thanks for the fix! All I need is that there is a way to specify things in the rst so that both the HTML and the LaTeX output look OK, so if that means specifying :widths: grid or explicit widths, then that's not a problem. The bigger issue, however, is that Arch Linux appears to be holding back the docutils package due to some issue between docutils and recommonmark, which is particularly annoying as recommonmark seems to be deprecated. But that's beyond the scope of this issue.

     
    • Günter Milde

      Günter Milde - 2022-02-20

      that's not a problem. The bigger issue, however, is that Arch Linux
      appears to be holding back the docutils package due to some issue
      between docutils and recommonmark

      Sphinx introduced an upper bound on the Docutils version as a precaution.
      If you want to experiment, you may install a repository snapshot
      https://docutils.sourceforge.io/#snapshots

      It should work with a not too old Sphinx but may need customization of the
      CSS style sheets due to recent changes in HTML5 writer.

       
  • Adam  Turner

    Adam Turner - 2022-02-20

    Arch Linux appears to be holding back the docutils package due to some issue between docutils and recommonmark

    Do you have a link to a tracker issue for Arch at all? Equally, would it be of any use to have a statement from a Docutils maintainer to help with this?

    A

     
  • Günter Milde

    Günter Milde - 2022-07-06
    • status: open-fixed --> closed-fixed
     
  • Günter Milde

    Günter Milde - 2022-07-06

    The table collumn bug is fixed in Release 0.19 (2022-07-05).
    Docutils 0.19 should also work nice with "recommonmark" and the development version of Sphinx.

     

Log in to post a comment.