From 10ae47d7b9e4b523f3dc94f7726f0355e1974896 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Sat, 4 Feb 2017 13:06:12 +0900 Subject: [PATCH] latex: Fix colwidths without :widths: option --- sphinx/writers/latex.py | 4 ++- tests/roots/test-latex-table/conf.py | 7 +++++ tests/roots/test-latex-table/index.rst | 27 ++++++++++++++++++ tests/test_build_latex.py | 38 ++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 tests/roots/test-latex-table/conf.py create mode 100644 tests/roots/test-latex-table/index.rst diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index 3093ca274..0799e9237 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -318,6 +318,7 @@ class ShowUrlsTransform(object): class Table(object): def __init__(self): # type: () -> None + self.classes = [] self.col = 0 self.colcount = 0 self.colspec = None # type: unicode @@ -1166,6 +1167,7 @@ class LaTeXTranslator(nodes.NodeVisitor): '%s:%s: nested tables are not yet implemented.' % (self.curfilestack[-1], node.line or '')) self.table = Table() + self.table.classes = node['classes'] self.table.longtable = 'longtable' in node['classes'] self.tablebody = [] # type: List[unicode] self.tableheaders = [] # type: List[unicode] @@ -1205,7 +1207,7 @@ class LaTeXTranslator(nodes.NodeVisitor): endmacro = '\\end{tabulary}\n\n' if self.table.colspec: self.body.append(self.table.colspec) - elif self.table.colwidths: + elif self.table.colwidths and 'colwidths-given' in self.table.classes: total = sum(self.table.colwidths) colspec = ['\\X{%d}{%d}' % (width, total) for width in self.table.colwidths] diff --git a/tests/roots/test-latex-table/conf.py b/tests/roots/test-latex-table/conf.py new file mode 100644 index 000000000..31e7a6ed4 --- /dev/null +++ b/tests/roots/test-latex-table/conf.py @@ -0,0 +1,7 @@ +# -*- coding: utf-8 -*- + +master_doc = 'index' + +latex_documents = [ + (master_doc, 'test.tex', 'The basic Sphinx documentation for testing', 'Sphinx', 'report') +] diff --git a/tests/roots/test-latex-table/index.rst b/tests/roots/test-latex-table/index.rst new file mode 100644 index 000000000..c7159398a --- /dev/null +++ b/tests/roots/test-latex-table/index.rst @@ -0,0 +1,27 @@ +test-latex-table +================ + +simple table +------------ + +======= ======= +header1 header2 +======= ======= +cell1-1 cell1-2 +cell2-1 cell2-2 +cell3-1 cell3-2 +======= ======= + +table having :widths: option +---------------------------- + +.. table:: + :widths: 30,70 + + ======= ======= + header1 header2 + ======= ======= + cell1-1 cell1-2 + cell2-1 cell2-2 + cell3-1 cell3-2 + ======= ======= diff --git a/tests/test_build_latex.py b/tests/test_build_latex.py index 6c8861f1b..758c84f73 100644 --- a/tests/test_build_latex.py +++ b/tests/test_build_latex.py @@ -20,6 +20,7 @@ import pytest from sphinx.errors import SphinxError from sphinx.util.osutil import cd, ensuredir +from sphinx.util import docutils from sphinx.writers.latex import LaTeXTranslator from util import SkipTest, remove_unicode_literals, strip_escseq, skip_if @@ -814,3 +815,40 @@ def test_maxlistdepth_at_ten(app, status, warning): print(status.getvalue()) print(warning.getvalue()) compile_latex_document(app) + + +@pytest.mark.skipif(docutils.__version_info__ < (0, 13), + reason='docutils-0.13 or above is required') +@pytest.mark.sphinx('latex', testroot='latex-table') +def test_latex_table(app, status, warning): + app.builder.build_all() + result = (app.outdir / 'test.tex').text(encoding='utf8') + tables = {} + for chap in re.split(r'\\chapter(?={.*})', result)[1:]: + sectname, content = chap.split('}', 1) + tables[sectname[1:]] = content.strip() + + # simple_table + simple_table = tables['simple table'] + assert ('\\noindent\\begin{tabulary}{\\linewidth}{|L|L|}' in simple_table) + assert ('\\hline\n' + '\\sphinxstylethead{\\relax \nheader1\n\\unskip}\\relax &' + '\\sphinxstylethead{\\relax \nheader2\n\\unskip}\\relax' in simple_table) + assert ('\\hline\ncell1-1\n&\ncell1-2\n\\\\' in simple_table) + assert ('\\hline\ncell2-1\n&\ncell2-2\n\\\\' in simple_table) + assert ('\\hline\ncell3-1\n&\ncell3-2\n\\\\' in simple_table) + assert ('\\hline\\end{tabulary}' in simple_table) + + # table having :widths: option + widths_table = tables['table having :widths: option'] + assert ('\\noindent\\begin{tabulary}{\\linewidth}{' + '|p{\\dimexpr(\\linewidth-\\arrayrulewidth)*30/100-2\\tabcolsep-\\arrayrulewidth\\relax}' + '|p{\\dimexpr(\\linewidth-\\arrayrulewidth)*70/100-2\\tabcolsep-\\arrayrulewidth\\relax}|}' + in widths_table) + assert ('\\hline\n' + '\\sphinxstylethead{\\relax \nheader1\n\\unskip}\\relax &' + '\\sphinxstylethead{\\relax \nheader2\n\\unskip}\\relax' in widths_table) + assert ('\\hline\ncell1-1\n&\ncell1-2\n\\\\' in widths_table) + assert ('\\hline\ncell2-1\n&\ncell2-2\n\\\\' in widths_table) + assert ('\\hline\ncell3-1\n&\ncell3-2\n\\\\' in widths_table) + assert ('\\hline\\end{tabulary}' in widths_table)