From 75653d85ac58c6e425627ea5498ddfb89f5013cc Mon Sep 17 00:00:00 2001 From: jfbu Date: Sun, 30 Apr 2017 18:38:43 +0200 Subject: [PATCH] Fix #3479: add latex support for docutils table stub columns This implementation in latex writer allows arbitrary columns to be declared "stub columns". But currently Docutils option ``:stub-columns:`` only allows a number telling the how many initial columns are "stub columns". --- sphinx/writers/latex.py | 5 ++++- tests/roots/test-latex-table/longtable.rst | 19 +++++++++++++++++++ tests/roots/test-latex-table/tabular.rst | 18 ++++++++++++++++++ tests/test_build_latex.py | 14 ++++++++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index cbcadee9f..302d324fe 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -332,6 +332,7 @@ class Table(object): self.caption = None # type: List[unicode] self.caption_footnotetexts = [] # type: List[unicode] self.header_footnotetexts = [] # type: List[unicode] + self.stubs = [] # type: List[int] # current position self.col = 0 @@ -1334,6 +1335,8 @@ class LaTeXTranslator(nodes.NodeVisitor): self.table.colcount += 1 if 'colwidth' in node: self.table.colwidths.append(node['colwidth']) + if 'stub' in node: + self.table.stubs.append(self.table.colcount - 1) def depart_colspec(self, node): # type: (nodes.Node) -> None @@ -1444,7 +1447,7 @@ class LaTeXTranslator(nodes.NodeVisitor): self.needs_linetrimming = 1 if len(node.traverse(nodes.paragraph)) >= 2: self.table.has_oldproblematic = True - if isinstance(node.parent.parent, nodes.thead): + if isinstance(node.parent.parent, nodes.thead) or (cell.col in self.table.stubs): if len(node) == 1 and isinstance(node[0], nodes.paragraph) and node.astext() == '': pass else: diff --git a/tests/roots/test-latex-table/longtable.rst b/tests/roots/test-latex-table/longtable.rst index 316dab775..370226f21 100644 --- a/tests/roots/test-latex-table/longtable.rst +++ b/tests/roots/test-latex-table/longtable.rst @@ -130,3 +130,22 @@ longtable having problematic cell - cell2-2 * - cell3-1 - cell3-2 + +longtable having both stub columns and problematic cell +------------------------------------------------------- + +.. list-table:: + :class: longtable + :header-rows: 1 + :stub-columns: 2 + + * - header1 + - header2 + - header3 + * - + instub1-1a + + instub1-1b + - instub1-2 + - notinstub1-3 + * - cell2-1 + - cell2-2 + - cell2-3 diff --git a/tests/roots/test-latex-table/tabular.rst b/tests/roots/test-latex-table/tabular.rst index 5577c496e..24091a3e1 100644 --- a/tests/roots/test-latex-table/tabular.rst +++ b/tests/roots/test-latex-table/tabular.rst @@ -133,3 +133,21 @@ table having problematic cell - cell2-2 * - cell3-1 - cell3-2 + +table having both stub columns and problematic cell +--------------------------------------------------- + +.. list-table:: + :header-rows: 1 + :stub-columns: 2 + + * - header1 + - header2 + - header3 + * - + instub1-1a + + instub1-1b + - instub1-2 + - notinstub1-3 + * - cell2-1 + - cell2-2 + - cell2-3 diff --git a/tests/test_build_latex.py b/tests/test_build_latex.py index 6b5cfef5b..ae9080d13 100644 --- a/tests/test_build_latex.py +++ b/tests/test_build_latex.py @@ -916,6 +916,13 @@ def test_latex_table_tabulars(app, status, warning): table = tables['table having both :widths: and problematic cell'] assert ('\\begin{tabular}[t]{|\\X{30}{100}|\\X{70}{100}|}' in table) + # table having both stub columns and problematic cell + table = tables['table having both stub columns and problematic cell'] + assert ('&\\sphinxstylethead{\\sphinxstyletheadfamily \n' + 'instub1-2\n\\unskip}\\relax &\nnotinstub1-3\n\\\\\n' + '\\hline\\sphinxstylethead{\\sphinxstyletheadfamily \n' + 'cell2-1\n\\unskip}\\relax &' in table) + @pytest.mark.skipif(docutils.__version_info__ < (0, 13), reason='docutils-0.13 or above is required') @@ -982,6 +989,13 @@ def test_latex_table_longtable(app, status, warning): table = tables['longtable having both :widths: and problematic cell'] assert ('\\begin{longtable}{|\\X{30}{100}|\\X{70}{100}|}' in table) + # longtable having both stub columns and problematic cell + table = tables['longtable having both stub columns and problematic cell'] + assert ('&\\sphinxstylethead{\\sphinxstyletheadfamily \n' + 'instub1-2\n\\unskip}\\relax &\nnotinstub1-3\n\\\\\n' + '\\hline\\sphinxstylethead{\\sphinxstyletheadfamily \n' + 'cell2-1\n\\unskip}\\relax &' in table) + @pytest.mark.skipif(docutils.__version_info__ < (0, 13), reason='docutils-0.13 or above is required')