@@ -1483,60 +1483,95 @@ Check the :confval:`latex_table_style`.
1483
1483
1484
1484
.. rst :directive :: .. tabularcolumns:: column spec
1485
1485
1486
- This directive influences only the LaTeX output for the next table in
1487
- source. The mandatory argument is a column specification (known as an
1488
- "alignment preamble" in LaTeX idiom). Please refer to a LaTeX
1486
+ This directive influences only the LaTeX output, and only for the next
1487
+ table in source. The mandatory argument is a column specification (known
1488
+ as an "alignment preamble" in LaTeX idiom). Please refer to a LaTeX
1489
1489
documentation, such as the `wiki page `_, for basics of such a column
1490
1490
specification.
1491
1491
1492
1492
.. _wiki page : https://en.wikibooks.org/wiki/LaTeX/Tables
1493
1493
1494
1494
.. versionadded :: 0.3
1495
1495
1496
+ Sphinx renders tables with at most 30 rows using ``tabulary `` (or
1497
+ ``tabular `` if at least one cell contains either a code-block or a nested
1498
+ table), and those with more rows with ``longtable ``. The advantage of
1499
+ using ``tabulary `` is that it tries to compute automatically (internally to
1500
+ LaTeX) suitable column widths.
1501
+
1502
+ The ``tabulary `` algorithm often works well, but in some cases when a cell
1503
+ contains long paragraphs, the column will be given a large width and other
1504
+ columns whose cells contain only single words may end up too narrow. The
1505
+ :rst:dir: `tabularcolumns ` can help solve this via providing to LaTeX a
1506
+ custom "alignment preamble" (aka "colspec"). For example ``lJJ `` will be
1507
+ suitable for a three-columns table whose first column contains only single
1508
+ words and the other two have cells with long paragraphs.
1509
+
1496
1510
.. note ::
1497
1511
1498
- :rst:dir: `tabularcolumns ` conflicts with ``:widths: `` option of table
1499
- directives. If both are specified, ``:widths: `` option will be ignored.
1512
+ Of course, a fully automated solution would be better, and it is still
1513
+ hoped for, but it is an intrinsic aspect of ``tabulary ``, and the latter
1514
+ is in use by Sphinx ever since ``0.3 ``... It looks as if solving the
1515
+ problem of squeezed columns could require substantial changes to that
1516
+ LaTeX package. And no good alternative appears to exist, as of 2025.
1517
+
1518
+ .. hint ::
1519
+
1520
+ A way to solve the issue for all tables at once, is to inject in the
1521
+ LaTeX preamble (see :confval: `latex_elements `) a command such as
1522
+ ``\setlength{\tymin}{1cm} `` which causes all columns to be at least
1523
+ ``1cm `` wide (not counting inter-column whitespace). Currently, Sphinx
1524
+ configures ``\tymin `` to allow room for three characters at least.
1500
1525
1501
- Sphinx renders tables with at most 30 rows using ``tabulary ``, and those
1502
- with more rows with ``longtable ``.
1526
+ Here is a more sophisticated "colspec", for a 4-columns table:
1503
1527
1504
- ``tabulary `` tries to compute automatically (internally to LaTeX) suitable
1505
- column widths. However, cells are then not allowed to contain
1506
- "problematic" elements such as lists, object descriptions,
1507
- blockquotes... Sphinx will fall back to using ``tabular `` if such a cell is
1508
- encountered (or a nested ``tabulary ``). In such a case the table will have
1509
- a tendency to try to fill the whole available line width.
1528
+ .. code-block :: latex
1510
1529
1511
- :rst:dir: `tabularcolumns ` can help in coercing the usage of ``tabulary `` if
1512
- one is careful to not employ the ``tabulary `` column types (``L ``, ``R ``,
1513
- ``C `` or ``J ``) for those columns with at least one "problematic" cell, but
1514
- only LaTeX's ``p{<width>} `` or Sphinx ``\X `` and ``\Y `` (described next).
1530
+ .. tabularcolumns:: >{\r aggedright}\Y {.4}>{\c entering}\Y {.1}>{\s phinxcolorblend{!95!red}\centering\noindent\bfseries\color {red}}\Y {.12}>{\r aggedright\a rraybackslash}\Y {.38}
1515
1531
1516
- Literal blocks do not work at all with ``tabulary ``. Sphinx will fall back
1517
- to ``tabular `` or ``longtable `` environments depending on the number of
1518
- rows. It will employ the :rst:dir: `tabularcolumns ` specification only if it
1519
- contains no usage of the ``tabulary `` specific types.
1532
+ This is used in Sphinx own PDF docs at :ref: `dev-deprecated-apis `.
1533
+ Regarding column widths, this "colspec" achieves the same as would
1534
+ ``:widths: `` option set to ``40 10 12 38 `` but it injects extra effects.
1535
+
1536
+ .. note ::
1537
+
1538
+ In case both :rst:dir: `tabularcolumns ` and ``:widths: `` option of table
1539
+ directives are used, ``:widths: `` option will be ignored by the LaTeX
1540
+ builder. Of course it is obeyed by other builders.
1541
+
1542
+ Literal blocks do not work at all with ``tabulary `` and Sphinx will then
1543
+ fall back to ``tabular `` LaTeX environment. It will employ the
1544
+ :rst:dir: `tabularcolumns ` specification in that case only if it contains no
1545
+ usage of the ``tabulary `` specific column types (which are ``L ``, ``R ``,
1546
+ ``C `` and ``J ``).
1520
1547
1521
1548
Besides the LaTeX ``l ``, ``r ``, ``c `` and ``p{width} `` column specifiers,
1522
- one can also use ``\X{a}{b} `` which configures the column width to be a
1523
- fraction ``a/b `` of the total line width and ``\Y{f} `` where ``f `` is a
1524
- decimal: for example ``\Y{0.2} `` means that the column will occupy ``0.2 ``
1525
- times the line width.
1549
+ and the ``tabulary `` specific ``L ``, ``R ``, ``C `` and ``J ``, one can also
1550
+ use (with all table types) ``\X{a}{b} `` which configures the column width
1551
+ to be a fraction ``a/b `` of the total line width and ``\Y{f} `` where ``f ``
1552
+ is a decimal: for example ``\Y{0.2} `` means that the column will occupy
1553
+ ``0.2 `` times the line width.
1526
1554
1527
1555
.. versionchanged :: 1.6
1528
1556
1529
- Use ``J `` (justified) by default with ``tabulary ``, not ``L ``
1557
+ Sphinx uses ``J `` (justified) by default with ``tabulary ``, not ``L ``
1530
1558
(flushed-left). To revert, include ``\newcolumntype{T}{L} `` in the LaTeX
1531
1559
preamble, as in fact Sphinx uses ``T `` and sets it by default to be an
1532
1560
alias of ``J ``.
1533
1561
1534
- .. hint ::
1562
+ .. versionchanged :: 8.3.0
1535
1563
1536
- A frequent issue with ``tabulary `` is that columns with little contents
1537
- appear to be "squeezed". One can add to the LaTeX preamble for example
1538
- ``\setlength{\tymin}{40pt} `` to ensure a minimal column width of ``40pt ``,
1539
- the ``tabulary `` default of ``10pt `` being too small.
1564
+ Formerly, Sphinx did not use ``tabulary `` if the table had at least one
1565
+ cell containing "problematic" elements such as lists, object descriptions,
1566
+ blockquotes (etc...) because such contents are not out-of-the-box
1567
+ compatible with ``tabulary ``. At ``8.3.0 `` a technique, which was already
1568
+ in use for merged cells, was extended to such cases, and the sole
1569
+ "problematic" contents are code-blocks and nested tables. So tables
1570
+ containing (only) cells with mutliple paragraphs, bullet or enumerated
1571
+ lists, or line blocks, will now better fit to their contents (if not
1572
+ rendered by ``longtable ``). Cells with object descriptions or admonitions
1573
+ will still have a tendency to induce the table to fill the full text area
1574
+ width, but columns in that table with no such contents will be tighter.
1540
1575
1541
1576
.. hint ::
1542
1577
0 commit comments