|
From: <mi...@us...> - 2021-08-10 14:14:16
|
Revision: 8806
http://sourceforge.net/p/docutils/code/8806
Author: milde
Date: 2021-08-10 14:14:13 +0000 (Tue, 10 Aug 2021)
Log Message:
-----------
LaTeX writer: Refactor computation of table column widths.
Prepare for fixes (cf. bug #422).
Update documentation.
No change to the output.
Modified Paths:
--------------
trunk/docutils/docs/ref/rst/directives.txt
trunk/docutils/docs/user/config.txt
trunk/docutils/docs/user/latex.txt
trunk/docutils/docutils/writers/latex2e/__init__.py
Modified: trunk/docutils/docs/ref/rst/directives.txt
===================================================================
--- trunk/docutils/docs/ref/rst/directives.txt 2021-08-10 14:13:35 UTC (rev 8805)
+++ trunk/docutils/docs/ref/rst/directives.txt 2021-08-10 14:14:13 UTC (rev 8806)
@@ -777,12 +777,13 @@
``widths`` : "auto", "grid", or a list of integers
A comma- or space-separated list of column widths.
- The default is the width of the input columns (in characters).
+ Defaults to the widths of the input columns in characters.
- The special values "auto" or "grid" may be used by writers to decide
- whether to delegate the determination of column widths to the backend
- (LaTeX, the HTML browser, ...).
- See also the `table_style`_ configuration option.
+ The special value "auto" directs writers to delegate the
+ determination of column widths to the backend (LaTeX, the HTML
+ browser, ...), if possible. "grid" forces the default behaviour
+ (overwriting a `table_style`_ configuration setting value
+ "colwidths-auto").
``width`` : `length`_ or `percentage`_
Forces the width of the table to the specified length or percentage
@@ -856,9 +857,9 @@
A comma- or space-separated list of relative column widths. The
default is equal-width columns (100%/#columns).
- The special value "auto" may be used by writers to decide
- whether to delegate the determination of column widths to the backend
- (LaTeX, the HTML browser, ...).
+ The special value "auto" directs writers to delegate the
+ determination of column widths to the backend (LaTeX, the HTML
+ browser, ...), if possible.
``width`` : `length`_ or `percentage`_
Forces the width of the table to the specified length or percentage
Modified: trunk/docutils/docs/user/config.txt
===================================================================
--- trunk/docutils/docs/user/config.txt 2021-08-10 14:13:35 UTC (rev 8805)
+++ trunk/docutils/docs/user/config.txt 2021-08-10 14:14:13 UTC (rev 8806)
@@ -1205,11 +1205,6 @@
align-left, align-center, align-right
Align the tables
- colwidths-auto
- Delegate the determination of table column widths to the backend
- (leave out the ``<colgroup>`` column specification).
- See also the "widths" option of the `table directive`_.
-
The HTML5 stylesheets also define:
booktabs
@@ -1219,6 +1214,13 @@
Place the table caption below the table
(New in 0.17).
+In addition, the HTML writers support:
+
+ colwidths-auto
+ Delegate the determination of table column widths to the backend
+ (leave out the ``<colgroup>`` column specification).
+ Overridden by the "widths" option of the `table directive`_.
+
Default: "". Option: ``--table-style``.
.. _table directive: ../ref/rst/directives.html#table
@@ -1746,8 +1748,10 @@
standard
Borders around all cells.
+
booktabs
A line above and below the table and one after the head.
+
borderless
No borders.
@@ -1755,9 +1759,8 @@
Align tables.
colwidths-auto, colwidths-given
- Default value for column width determination by
- LaTeX or Docutils.
- Override with the `table directive`_'s :widths: option.
+ Column width determination by LaTeX or Docutils (default).
+ Overridden by the `table directive`_'s :widths: option.
.. warning::
Modified: trunk/docutils/docs/user/latex.txt
===================================================================
--- trunk/docutils/docs/user/latex.txt 2021-08-10 14:13:35 UTC (rev 8805)
+++ trunk/docutils/docs/user/latex.txt 2021-08-10 14:14:13 UTC (rev 8806)
@@ -2082,7 +2082,7 @@
*tablewidth* is set relative to this value. If someone produces documents
with line length of 132 this will lead to suboptimal results.
- You may use the `:width:` option to manually set the table column widths.
+ You may use the `:widths:` option to manually set the table column widths.
* Table: multicol cells are always left aligned.
Modified: trunk/docutils/docutils/writers/latex2e/__init__.py
===================================================================
--- trunk/docutils/docutils/writers/latex2e/__init__.py 2021-08-10 14:13:35 UTC (rev 8805)
+++ trunk/docutils/docutils/writers/latex2e/__init__.py 2021-08-10 14:14:13 UTC (rev 8806)
@@ -6,6 +6,8 @@
"""LaTeX2e document tree Writer."""
+from __future__ import division
+
__docformat__ = 'reStructuredText'
# code contributions from several people included, thanks to all.
@@ -517,6 +519,10 @@
PreambleCmds.table = r"""\usepackage{longtable,ltcaption,array}
\setlength{\extrarowheight}{2pt}
\newlength{\DUtablewidth} % internal use in tables"""
+# if booktabs:
+# \newcommand{\DUcolumnwidth}[1]{\dimexpr #1\DUtablewidth-2\tabcolsep\relax}
+# else:
+# \newcommand{\DUcolumnwidth}[1]{\dimexpr #1\DUtablewidth-2\tabcolsep-\arrayrulewidth\relax}
PreambleCmds.textcomp = r"""\usepackage{textcomp} % text symbol macros"""
# TODO? Options [force,almostfull] prevent spurious error messages,
@@ -829,13 +835,10 @@
def __init__(self, translator, latex_type):
self._translator = translator
self._latex_type = latex_type
- self._open = False
- # miscellaneous attributes
- self._attrs = {}
- self._col_width = []
+
+ self.close()
+ self._colwidths = []
self._rowspan = []
- self.stubs = []
- self.colwidths_auto = False
self._in_thead = 0
def open(self):
@@ -844,6 +847,7 @@
self.caption = []
self._attrs = {}
self._in_head = False # maybe context with search
+
def close(self):
self._open = False
self._col_specs = None
@@ -857,12 +861,9 @@
def set_table_style(self, table_style, classes):
borders = [cls.replace('nolines', 'borderless')
- for cls in table_style+classes
+ for cls in ['standard'] + table_style + classes
if cls in ('standard', 'booktabs', 'borderless', 'nolines')]
- try:
- self.borders = borders[-1]
- except IndexError:
- self.borders = 'standard'
+ self.borders = borders[-1]
self.colwidths_auto = (('colwidths-auto' in classes
and 'colwidths-given' not in table_style)
or ('colwidths-auto' in table_style
@@ -876,6 +877,7 @@
def set(self, attr, value):
self._attrs[attr] = value
+
def get(self, attr):
if attr in self._attrs:
return self._attrs[attr]
@@ -930,35 +932,31 @@
ABC DEF
=== ===
- usually gets to narrow, therefore we add 1 (fiddlefactor).
+ usually gets too narrow, therefore we add 1 (fiddlefactor).
"""
bar = self.get_vertical_bar()
self._rowspan= [0] * len(self._col_specs)
- self._col_width = []
if self.colwidths_auto:
- latex_table_spec = (bar+'l')*len(self._col_specs)
- return latex_table_spec+bar
- width = 80
- total_width = 0.0
- # first see if we get too wide.
- for node in self._col_specs:
- colwidth = float(node['colwidth']+1) / width
- total_width += colwidth
- # donot make it full linewidth
- factor = 0.93
- if total_width > 1.0:
- factor /= total_width
- latex_table_spec = ''
- for node in self._col_specs:
- colwidth = factor * float(node['colwidth']+1) / width
- self._col_width.append(colwidth+0.005)
- latex_table_spec += '%sp{%.3f\\DUtablewidth}' % (bar, colwidth+0.005)
- return latex_table_spec+bar
+ self._colwidths = []
+ latex_colspecs = ['l'] * len(self._col_specs)
+ else:
+ width = 80 # assumed standard line length
+ # first see if we get too wide.
+ total_width = sum(node['colwidth']+1 for node in self._col_specs)
+ # do not make it full linewidth
+ factor = 0.93
+ if total_width > 80:
+ factor *= width / total_width
+ self._colwidths = [(factor * float(node['colwidth']+1)/width)
+ + 0.005 for node in self._col_specs]
+ latex_colspecs = ['p{%.3f\\DUtablewidth}' % colwidth
+ for colwidth in self._colwidths]
+ return bar + bar.join(latex_colspecs) + bar
def get_column_width(self):
"""Return columnwidth for current cell (not multicell)."""
try:
- return '%.2f\\DUtablewidth' % self._col_width[self._cell_in_row]
+ return '%.2f\\DUtablewidth' % self._colwidths[self._cell_in_row]
except IndexError:
return '*'
@@ -966,8 +964,8 @@
"""Return sum of columnwidths for multicell."""
try:
mc_width = sum([width
- for width in ([self._col_width[start + co]
- for co in range (len_)])])
+ for width in ([self._colwidths[start + co]
+ for co in range(len_)])])
return 'p{%.2f\\DUtablewidth}' % mc_width
except IndexError:
return 'l'
@@ -1170,10 +1168,6 @@
self.settings.graphicx_option)
# footnotes: TODO: implement LaTeX footnotes
self.docutils_footnotes = settings.docutils_footnotes
- # @@ table_style: list of values from fixed set: warn?
- # for s in self.settings.table_style:
- # if s not in Writer.table_style_values:
- # self.warn('Ignoring value "%s" in "table-style" setting.' %s)
# Output collection stacks
# ~~~~~~~~~~~~~~~~~~~~~~~~
@@ -2956,7 +2950,7 @@
content = self.out
self.pop_output_collector()
try:
- width = self.to_latex_length(node.attributes['width'])
+ width = self.to_latex_length(node['width'])
except KeyError:
width = r'\linewidth'
if isinstance(node.parent, nodes.compound):
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|