Search code examples
pdflatexpython-sphinxpdflatex

Adjust table widths for pdf output to perfectly fit the line width


My company uses Sphinx to create our user manuals and we are making extensive usage of tables in different varieties. In many cases it is necessary to adjust the column widths of the tables manually in order to generate a high quality pdf output.

For this I tried to use:

.. tabularcolumns:: |p{0.2\linewidth}|p{0.8\linewidth}|

+------+--------+
| A    | B      |
+------+--------+

My intention was, that the table should perfectly fit the surrounding text, but there is a little overlap. Seemingly the percentages do not account for the space occupied by the inner boxes.

How can I correctly compute the necessary amount, so that the box fits neatly to the space?


Solution

  • With Sphinx 1.6 or later, you can use

    .. tabularcolumns:: |\Y{0.2}|\Y{0.8}|
    

    (it is \Y, not Y.) See http://www.sphinx-doc.org/en/stable/markup/misc.html?highlight=tabularcolumns#directive-tabularcolumns.

    With Sphinx 1.5 or later you can use |\X{2}{10}|\X{8}{10}|.

    With Sphinx < 1.5 you need some special LaTeX, which you can extract from this code

    \newcolumntype{\X}[2]{p{\dimexpr
          (\linewidth-\arrayrulewidth)*#1/#2-2\tabcolsep-\arrayrulewidth\relax}}
    \newcolumntype{\Y}[1]{p{\dimexpr
          #1\dimexpr\linewidth-\arrayrulewidth\relax-2\tabcolsep-\arrayrulewidth\relax}}
    

    from current sphinx.sty, which explains how the \X and \Y are defined.