From 6df55e4168a62244dec7d13b1d2eca786088186c Mon Sep 17 00:00:00 2001 From: Tim Nordell Date: Mon, 3 Nov 2025 15:28:13 -0600 Subject: [PATCH] latex tables: Fix rendering for grid-filled merged vertical cells The table from issue #9313: .. table:: :class: standard nocolorrows +--------------------+----------------+ | 2 rows and 2 cols | 1 row x 3 cols | | +----+-----+-----+ | | A | B | C | +---+----------------+----+-----+-----+ | 1 | 2 | 3 | 4 | 5 | +---+----------------+----+-----+-----+ has merged vertical cells that notably needs to draw a vertical line on the left and right-hand side of the cells in the 2x2 grid area. If you add a column to the left of this: .. table:: :class: standard nocolorrows +---+---+----------------+----------------+ | X | 2 rows and 2 cols | 1 row x 3 cols | | | +----+-----+-----+ | | | A | B | C | +---+---+----------------+----+-----+-----+ | Y | 1 | 2 | 3 | 4 | 5 | +---+---+----------------+----+-----+-----+ then the 2x2 area should *not* draw a column line to the left of the area. We can fix this by adjusting the \multicolumn invocation to adjust when it emits the colspec. Notably, this commit does *not* consider the other broken case where a person maybe just wants the table borders like: .. tabularcolumns:: |llllll| .. table:: :class: standard nocolorrows +---+---+----------------+----------------+ | | 2 rows and 2 cols | 1 row x 3 cols | | X | +----+-----+-----+ | | | A | B | C | +---+---+----------------+----+-----+-----+ | Y | 1 | 2 | 3 | 4 | 5 | +---+---+----------------+----+-----+-----+ That has an impact in this area of the code, but to correctly consider that one has to parse out the given colspec to determine which columns have borders between them. (This line of code has a bug for this case as the colspec being emitted should be conditional if the specific column actually has it enabled, but the supporting code only determines if a vertical line is enabled anywhere rather than which columns.) --- sphinx/writers/latex.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index a2a17855c18..c2b9d711f3f 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -1254,9 +1254,10 @@ def visit_row(self, node: Element) -> None: # insert suitable strut for equalizing row heights in given multirow self.body.append(r'\sphinxtablestrut{%d}' % cell.cell_id) else: # use \multicolumn for wide multirow cell + left_colsep = _colsep if cell.col == 0 else '' self.body.append( r'\multicolumn{%d}{%sl%s}{\sphinxtablestrut{%d}}' - % (cell.width, _colsep, _colsep, cell.cell_id) + % (cell.width, left_colsep, _colsep, cell.cell_id) ) def depart_row(self, node: Element) -> None: