Skip to content

Commit

Permalink
Close sphinx-doc#6837: LaTeX: Support a nested table
Browse files Browse the repository at this point in the history
  • Loading branch information
tk0miya committed Nov 16, 2019
1 parent 7d0aa95 commit e0affe1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES
Expand Up @@ -31,6 +31,7 @@ Features added
* #1331: Add new config variable: :confval:`user_agent`
* #6000: LaTeX: have backslash also be an inline literal word wrap break
character
* #6837: LaTeX: Support a nested table
* #6812: Improve a warning message when extensions are not parallel safe
* #6818: Improve Intersphinx performance for multiple remote inventories.

Expand Down
29 changes: 24 additions & 5 deletions sphinx/writers/latex.py
Expand Up @@ -657,7 +657,7 @@ def __init__(self, document, builder):
latex_engine=self.config.latex_engine)
self.context = [] # type: List[Any]
self.descstack = [] # type: List[str]
self.table = None # type: Table
self.tables = [] # type: List[Table]
self.next_table_colspec = None # type: str
self.bodystack = [] # type: List[List[str]]
self.footnote_restricted = None # type: nodes.Element
Expand Down Expand Up @@ -806,6 +806,15 @@ def render(self, template_name, variables):

return renderer.render(template_name, variables)

@property
def table(self):
# type: () -> Table
"""Get current table."""
if self.tables:
return self.tables[-1]
else:
return None

def visit_document(self, node):
# type: (nodes.Element) -> None
self.curfilestack.append(node.get('docname', ''))
Expand Down Expand Up @@ -1161,11 +1170,21 @@ def visit_tabular_col_spec(self, node):

def visit_table(self, node):
# type: (nodes.Element) -> None
if self.table:
if len(self.tables) == 1:
if self.table.get_table_type() == 'longtype':
raise UnsupportedError(
'%s:%s: longtype does not support nesting a table.' %
(self.curfilestack[-1], node.line or ''))
else:
# change type of parent table to tabular
# see https://groups.google.com/d/msg/sphinx-users/7m3NeOBixeo/9LKP2B4WBQAJ
self.table.has_problematic = True
elif len(self.tables) > 2:
raise UnsupportedError(
'%s:%s: nested tables are not yet implemented.' %
'%s:%s: deeply nested tables are not yet implemented.' %
(self.curfilestack[-1], node.line or ''))
self.table = Table(node)

self.tables.append(Table(node))
if self.next_table_colspec:
self.table.colspec = '{%s}\n' % self.next_table_colspec
if 'colwidths-given' in node.get('classes', []):
Expand All @@ -1183,7 +1202,7 @@ def depart_table(self, node):
self.body.append(table)
self.body.append("\n")

self.table = None
self.tables.pop()

def visit_colspec(self, node):
# type: (nodes.Element) -> None
Expand Down

0 comments on commit e0affe1

Please sign in to comment.