latex: Fix assertion error for complex table (refs: #3395)

This commit is contained in:
Takeshi KOMIYA 2017-02-11 12:19:12 +09:00
parent a79b6ec24a
commit 37f3050933
5 changed files with 103 additions and 47 deletions

View File

@ -1351,12 +1351,17 @@ class LaTeXTranslator(nodes.NodeVisitor):
# type: (nodes.Node) -> None
self.table.col = 0
# fill column if first one is a wide-multirow
cell = self.table.cell(self.table.row, 0)
if cell and cell.row != self.table.row: # bottom part of multirow cell
self.table.col += cell.width
if cell.width > 1: # use \multicolumn for wide multirow cell
self.body.append('\\multicolumn{%d}{|l|}{}\\relax ' % cell.width)
# fill columns if the row starts with the bottom of multirow cell
while True:
cell = self.table.cell(self.table.row, self.table.col)
if cell is None: # not a bottom of multirow cell
break
else: # a bottom of multirow cell
self.table.col += cell.width
if cell.col != 0:
self.body.append('&')
if cell.width > 1: # use \multicolumn for wide multirow cell
self.body.append('\\multicolumn{%d}{|l|}{}\\relax ' % cell.width)
def depart_row(self, node):
# type: (nodes.Node) -> None
@ -1427,13 +1432,16 @@ class LaTeXTranslator(nodes.NodeVisitor):
cell = self.table.cell()
self.table.col += cell.width
# fill column if next one is a wide-multirow
nextcell = self.table.cell()
if nextcell and nextcell.row != self.table.row: # bottom part of multirow cell
self.table.col += nextcell.width
self.body.append('&')
if nextcell.width > 1: # use \multicolumn for wide multirow cell
self.body.append('\\multicolumn{%d}{l|}{}\\relax ' % nextcell.width)
# fill columns if next ones are a bottom of wide-multirow cell
while True:
nextcell = self.table.cell()
if nextcell is None: # not a bottom of multirow cell
break
else: # a bottom part of multirow cell
self.table.col += nextcell.width
self.body.append('&')
if nextcell.width > 1: # use \multicolumn for wide multirow cell
self.body.append('\\multicolumn{%d}{l|}{}\\relax ' % nextcell.width)
def visit_acks(self, node):
# type: (nodes.Node) -> None

View File

@ -0,0 +1,35 @@
complex tables
==============
grid table
----------
+---------+---------+---------+
| header1 | header2 | header3 |
+=========+=========+=========+
| cell1-1 | cell1-2 | cell1-3 |
+---------+ +---------+
| cell2-1 | | cell2-3 |
+ +---------+---------+
| | cell3-2 |
+---------+ |
| cell4-1 | |
+---------+---------+---------+
| cell5-1 |
+---------+---------+---------+
complex spanning cell
---------------------
table having ...
* consecutive multicol at top of row (1-1 and 1-2)
* consecutive multicol at end of row (1-4 and 1-5)
+-----------+-----------+-----------+-----------+-----------+
| | | cell1-3 | | |
| | +-----------+ | cell1-5 |
| cell1-1 | cell1-2 | | cell1-4 | |
| | | cell2-3 | +-----------+
| | | | | cell3-5 |
+-----------+-----------+-----------+-----------+-----------+

View File

@ -5,3 +5,4 @@ test-latex-table
tabular
longtable
complex

View File

@ -12,23 +12,6 @@ cell2-1 cell2-2
cell3-1 cell3-2
======= =======
grid table
----------
+---------+---------+---------+
| header1 | header2 | header3 |
+=========+=========+=========+
| cell1-1 | cell1-2 | cell1-3 |
+---------+ +---------+
| cell2-1 | | cell2-2 |
+ +---------+---------+
| | cell3-2 |
+---------+ |
| cell4-1 | |
+---------+---------+---------+
| cell5-1 |
+---------+---------+---------+
table having :widths: option
----------------------------

View File

@ -840,23 +840,6 @@ def test_latex_table_tabulars(app, status, warning):
assert ('\\hline\ncell3-1\n&\ncell3-2\n\\\\' in table)
assert ('\\hline\n\\end{tabulary}' in table)
# grid table
table = tables['grid table']
assert ('\\noindent\\begin{tabulary}{\\linewidth}{|L|L|L|}' in table)
assert ('\\hline\n'
'\\sphinxstylethead{\\relax \nheader1\n\\unskip}\\relax &'
'\\sphinxstylethead{\\relax \nheader2\n\\unskip}\\relax &'
'\\sphinxstylethead{\\relax \nheader3\n\\unskip}\\relax \\\\' in table)
assert ('\\hline\ncell1-1\n&\\multirow{2}{*}{\\relax \ncell1-2\n\\unskip}\\relax &\n'
'cell1-3\n\\\\' in table)
assert ('\\cline{1-1}\\cline{3-3}\\multirow{2}{*}{\\relax \ncell2-1\n\\unskip}\\relax &&\n'
'cell2-2\n\\\\' in table)
assert ('\\cline{2-3}&\\multicolumn{2}{l|}{\\relax \\multirow{2}{*}{\\relax \n'
'cell3-2\n\\unskip}\\relax \\unskip}\\relax \\\\' in table)
assert ('\\cline{1-1}\ncell4-1\n&\\multicolumn{2}{l|}{}\\relax \\\\' in table)
assert ('\\hline\\multicolumn{3}{|l|}{\\relax \ncell5-1\n\\unskip}\\relax \\\\\n'
'\\hline\n\\end{tabulary}' in table)
# table having :widths: option
table = tables['table having :widths: option']
assert ('\\noindent\\begin{tabular}{|\\X{30}{100}|\\X{70}{100}|}' in table)
@ -959,3 +942,49 @@ def test_latex_table_longtable(app, status, warning):
# longtable having both :widths: and problematic cell
table = tables['longtable having both :widths: and problematic cell']
assert ('\\begin{longtable}{|\\X{30}{100}|\\X{70}{100}|}' in table)
@pytest.mark.skipif(docutils.__version_info__ < (0, 13),
reason='docutils-0.13 or above is required')
@pytest.mark.sphinx('latex', testroot='latex-table')
@pytest.mark.test_params(shared_result='test_latex_table')
def test_latex_table_complex_tables(app, status, warning):
app.builder.build_all()
result = (app.outdir / 'test.tex').text(encoding='utf8')
tables = {}
for chap in re.split(r'\\section{', result)[1:]:
sectname, content = chap.split('}', 1)
tables[sectname] = content.strip()
# grid table
table = tables['grid table']
assert ('\\noindent\\begin{tabulary}{\\linewidth}{|L|L|L|}' in table)
assert ('\\hline\n'
'\\sphinxstylethead{\\relax \nheader1\n\\unskip}\\relax &'
'\\sphinxstylethead{\\relax \nheader2\n\\unskip}\\relax &'
'\\sphinxstylethead{\\relax \nheader3\n\\unskip}\\relax \\\\' in table)
assert ('\\hline\ncell1-1\n&\\multirow{2}{*}{\\relax \ncell1-2\n\\unskip}\\relax &\n'
'cell1-3\n\\\\' in table)
assert ('\\cline{1-1}\\cline{3-3}\\multirow{2}{*}{\\relax \ncell2-1\n\\unskip}\\relax &&\n'
'cell2-3\n\\\\' in table)
assert ('\\cline{2-3}&\\multicolumn{2}{l|}{\\relax \\multirow{2}{*}{\\relax \n'
'cell3-2\n\\unskip}\\relax \\unskip}\\relax \\\\' in table)
assert ('\\cline{1-1}\ncell4-1\n&\\multicolumn{2}{l|}{}\\relax \\\\' in table)
assert ('\\hline\\multicolumn{3}{|l|}{\\relax \ncell5-1\n\\unskip}\\relax \\\\\n'
'\\hline\n\\end{tabulary}' in table)
# complex spanning cell
table = tables['complex spanning cell']
assert ('\\noindent\\begin{tabulary}{\\linewidth}{|L|L|L|L|L|}' in table)
assert ('\\hline\n'
'\\multirow{3}{*}{\\relax \ncell1-1\n\\unskip}\\relax &'
'\\multirow{3}{*}{\\relax \ncell1-2\n\\unskip}\\relax &'
'\ncell1-3\n&'
'\\multirow{3}{*}{\\relax \ncell1-4\n\\unskip}\\relax &'
'\\multirow{2}{*}{\\relax \ncell1-5\n\\unskip}\\relax \\\\\n'
in table)
assert ('\\cline{3-3}&&'
'\\multirow{2}{*}{\\relax \ncell2-3\n\\unskip}\\relax &&\\\\\n'
in table)
assert ('\\cline{5-5}&&&&\ncell3-5\n\\\\\n' in table)
assert ('\\hline\n\\end{tabulary}' in table)