Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #3378: latex: Support :widths: option of table directives #3379

Merged
merged 1 commit into from
Feb 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Features added
When specified, each template parameter will be rendered on a separate line.
* #3359: Allow sphinx.js in a user locale dir to override sphinx.js from Sphinx
* #3303: Add ``:pyversion:`` option to the doctest directive.
* #3378: latex: Support ``:widths:`` option of table directives

Bugs fixed
----------
Expand Down
9 changes: 9 additions & 0 deletions sphinx/writers/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ def __init__(self):
self.col = 0
self.colcount = 0
self.colspec = None # type: unicode
self.colwidths = [] # type: List[int]
self.rowcount = 0
self.had_head = False
self.has_problematic = False
Expand Down Expand Up @@ -1203,6 +1204,12 @@ def depart_table(self, node):
endmacro = '\\end{tabulary}\n\n'
if self.table.colspec:
self.body.append(self.table.colspec)
elif self.table.colwidths:
total = sum(self.table.colwidths)
colspec = ['p{\\dimexpr(\\linewidth-\\arrayrulewidth)*%d/%d'
'-2\\tabcolsep-\\arrayrulewidth\\relax}' % (width, total)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible to do something like

\newcolumntype{X}[2]{p{\dimexpr(\linewidth-\arrayrulewidth)*#1/#2-2\tabcolsep-\arrayrulewidth\relax}}

in sphinx.sty and then generate mark-up by writer like

|X{50}{70}|X{20}{70}|

for smaller files. I must check which letter is best available for this (i.e. not used by packages user may add to default config).

Fortunately \dimexpr handles <dimen>*A/B in double precision for the first multiplication so that even with things like 500/1000 there will be no overflow, despite the maximal dimension in TeX being only about 5.76 m.

for width in self.table.colwidths]
self.body.append('{|%s|}\n' % '|'.join(colspec))
else:
if self.table.has_problematic:
colspec = ('*{%d}{p{\\dimexpr(\\linewidth-\\arrayrulewidth)/%d'
Expand Down Expand Up @@ -1252,6 +1259,8 @@ def depart_table(self, node):
def visit_colspec(self, node):
# type: (nodes.Node) -> None
self.table.colcount += 1
if 'colwidth' in node:
self.table.colwidths.append(node['colwidth'])

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