Fix LaTeX builder crash on tables with no body rows - #14291
Open
veeceey wants to merge 1 commit into
Open
Conversation
The LaTeXFootnoteVisitor.depart_table() method called next(node.findall(nodes.tbody)) which raises StopIteration when a table has header rows but no body rows (as produced by e.g. myst_parser from Markdown). Handle the missing tbody gracefully by placing any collected footnotes after the table node instead. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Author
|
Friendly ping - any chance someone could take a look at this when they get a chance? Happy to make any changes if needed. |
veeceey
force-pushed
the
fix/latex-empty-table-stopiteration
branch
from
February 24, 2026 07:47
795d52a to
6bbbc96
Compare
picnixz
requested changes
Jun 7, 2026
Comment on lines
+2108
to
+2110
| from docutils.utils import new_document | ||
|
|
||
| from sphinx.builders.latex.transforms import LaTeXFootnoteVisitor |
| fntext = footnotetext('', *footnote.children, ids=footnote['ids']) | ||
| tbody.insert(0, fntext) | ||
| tbody = next(node.findall(nodes.tbody), None) | ||
| if tbody is not None: |
Member
There was a problem hiding this comment.
Swap the if-blocks (more natural to have if tbody is None first)
| # If there is no tbody (e.g. a table with only header rows), | ||
| # place any collected footnotes after the table node instead. | ||
| table_parent = node.parent | ||
| if table_parent is not None: |
Member
There was a problem hiding this comment.
Check that the parent is indeed a table.
| assert app.warning.getvalue() == '' | ||
|
|
||
|
|
||
| def test_latex_table_empty_body() -> None: |
Member
There was a problem hiding this comment.
Please add a new test case but not handwritten one.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #14271.
LaTeXFootnoteVisitor.depart_table()unconditionally callsnext(node.findall(nodes.tbody)), which raisesStopIterationwhen a table has header rows but no body rows. This happens when external parsers like myst_parser produce a docutils table node tree from Markdown tables that have only a header row (e.g.| Key | P | Summary |\n| --- | --- | --- |).The fix:
next(..., None)to safely handle the missingtbodytbodyis present, behavior is unchanged (footnotes are inserted at the head of tbody)tbodyis absent, any collected table footnotes are placed after the table node instead, preserving footnote renderingTest:
test_latex_table_empty_bodywhich constructs a table node withtheadbut notbody(mirroring what myst_parser produces) and verifies thatLaTeXFootnoteVisitorprocesses it without raisingStopIterationStopIterationon the unfixed code and passes with the fixTest plan
test_latex_table_empty_bodypassesruff checkpasses on both modified files