latex: Fix colwidths without :widths: option

This commit is contained in:
Takeshi KOMIYA 2017-02-04 13:06:12 +09:00
parent a542ca2e78
commit 10ae47d7b9
4 changed files with 75 additions and 1 deletions

View File

@ -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]

View File

@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
master_doc = 'index'
latex_documents = [
(master_doc, 'test.tex', 'The basic Sphinx documentation for testing', 'Sphinx', 'report')
]

View File

@ -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
======= =======

View File

@ -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)