Skip to content

Commit 118f4a1

Browse files
authored
LaTeX: make sure tabulary is used if colspec requires it (#13653)
1 parent 4564486 commit 118f4a1

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

sphinx/writers/latex.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,13 @@ def get_table_type(self) -> str:
171171
elif self.has_verbatim:
172172
return 'tabular'
173173
elif self.colspec:
174-
assert len(self.colspec) > 2
175-
_colspec = re.sub(r'\{.*?\}', '', self.colspec[1:-2])
176-
if any(c in 'LRCJT' for c in _colspec):
177-
# tabulary would complain "no suitable columns" if none of its
178-
# column type were used so we ensure at least one matches.
179-
# It is responsability of user to make sure not to use tabulary
180-
# column types for a column containing a problematic cell.
174+
# tabulary complains (only a LaTeX warning) if none of its column
175+
# types is used. The next test will have false positive from
176+
# syntax such as >{\RaggedRight} but it will catch *{3}{J} which
177+
# does require tabulary and would crash tabular
178+
# It is user responsability not to use a tabulary column type for
179+
# a column having a problematic cell.
180+
if any(c in 'LRCJT' for c in self.colspec):
181181
return 'tabulary'
182182
else:
183183
return 'tabular'
@@ -1217,17 +1217,22 @@ def depart_table(self, node: Element) -> None:
12171217
# We try to catch a tabularcolumns using L, R, J, C, or T.
12181218
# We can not simply test for presence in the colspec of
12191219
# one of those letters due to syntax such as >{\RaggedRight}.
1220+
# The test will not catch *{3}{J} syntax, but it would be
1221+
# overkill to try to implement LaTeX preamble mini-language.
12201222
if self.table.colspec:
12211223
assert len(self.table.colspec) > 2
1222-
_colspec = re.sub(r'\{.*?\}', '', self.table.colspec[1:-2])
1223-
if any(c in _colspec for c in 'LRJCT'):
1224+
# cf how self.table.colspec got set in visit_table().
1225+
_colspec_as_given = self.table.colspec[1:-2]
1226+
_colspec_stripped = re.sub(r'\{.*?\}', '', _colspec_as_given)
1227+
if any(c in _colspec_stripped for c in 'LRJCT'):
12241228
logger.warning(
12251229
__(
1226-
'colspec %s was given which uses '
1230+
'colspec %s was given which appears to use '
12271231
'tabulary syntax. But this table can not be '
1228-
'rendered as a tabulary; colspec will be ignored.'
1232+
'rendered as a tabulary; the given colspec will '
1233+
'be ignored.'
12291234
),
1230-
self.table.colspec[:-1],
1235+
_colspec_as_given,
12311236
type='latex',
12321237
location=node,
12331238
)

tests/roots/test-root/markup.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ With
182182
Tables
183183
------
184184

185-
.. tabularcolumns:: |L|p{5cm}|R|
185+
.. tabularcolumns:: |*{1}{L|}p{5cm}|*{1}{R}|
186186

187187
.. _my-table:
188188

0 commit comments

Comments
 (0)