diff options
| author | milde <milde@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2021-09-30 10:15:45 +0000 |
|---|---|---|
| committer | milde <milde@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2021-09-30 10:15:45 +0000 |
| commit | 03130753046e2d6ffdc1a0629cce3598e644dc11 (patch) | |
| tree | 9fd3f1fb96189f28f6e2cf8791cab7c5b6e9f771 | |
| parent | 4d57465c4dad9d79834f407ac9af37d9d5807ad1 (diff) | |
| download | docutils-03130753046e2d6ffdc1a0629cce3598e644dc11.tar.gz | |
LaTeX writer: New algorithm for table colum widths. Fixes bug #422.
New configuration setting "legacy_column_widths" for backwards
compatibility.
Only write "continued on next page..." if it fits without
making the table columns wider.
Table `width` option overrides conflicting "auto" column `widths`.
git-svn-id: https://svn.code.sf.net/p/docutils/code/trunk@8835 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
| -rw-r--r-- | docutils/HISTORY.txt | 14 | ||||
| -rw-r--r-- | docutils/RELEASE-NOTES.txt | 28 | ||||
| -rw-r--r-- | docutils/docs/ref/doctree.txt | 3 | ||||
| -rw-r--r-- | docutils/docs/ref/rst/directives.txt | 33 | ||||
| -rw-r--r-- | docutils/docs/user/config.txt | 59 | ||||
| -rw-r--r-- | docutils/docs/user/latex.txt | 152 | ||||
| -rw-r--r-- | docutils/docutils/parsers/rst/directives/tables.py | 4 | ||||
| -rw-r--r-- | docutils/docutils/writers/latex2e/__init__.py | 135 | ||||
| -rw-r--r-- | docutils/test/functional/expected/latex_cornercases.tex | 225 | ||||
| -rw-r--r-- | docutils/test/functional/expected/latex_memoir.tex | 13 | ||||
| -rw-r--r-- | docutils/test/functional/expected/standalone_rst_latex.tex | 13 | ||||
| -rw-r--r-- | docutils/test/functional/expected/standalone_rst_xetex.tex | 13 | ||||
| -rw-r--r-- | docutils/test/functional/input/data/tables_latex.txt | 117 | ||||
| -rw-r--r-- | docutils/test/functional/tests/latex_cornercases.py | 1 | ||||
| -rwxr-xr-x | docutils/test/test_writers/test_latex2e.py | 2 |
15 files changed, 474 insertions, 338 deletions
diff --git a/docutils/HISTORY.txt b/docutils/HISTORY.txt index 83b6f536f..0294af9f2 100644 --- a/docutils/HISTORY.txt +++ b/docutils/HISTORY.txt @@ -111,7 +111,6 @@ Changes Since 0.17.1 - Overwrite methods in _html_base.HTMLTranslator that use HTM5 tags (details, aside, nav, ...) and attributes (role, aria-level). - * docutils/writers/latex2e/__init__.py - The setting `legacy_class_functions`_ now defaults to "False". @@ -129,6 +128,19 @@ Changes Since 0.17.1 - Improve spacing and allow customization of Docutils-generated table of contents. + - New algorithm for table colum widths. Fixes bug #422. + New configuration setting legacy_column_widths_. + + Table.set_table_style() arguments changed. + + Only write "continued on next page..." if it fits + without making the table columns wider. + + Table `width` option overrides conflicting "auto" column `widths`. + +.. _legacy_column_widths: docs/user/config.html#legacy-column-widths + + * docutils/writers/latex2e/docutils.sty - Fix excessive padding above sidebar titles. diff --git a/docutils/RELEASE-NOTES.txt b/docutils/RELEASE-NOTES.txt index bf42d35b1..5fb0b4abf 100644 --- a/docutils/RELEASE-NOTES.txt +++ b/docutils/RELEASE-NOTES.txt @@ -31,11 +31,12 @@ Future changes deprecated ``<colgroup><col width="...">...``. The `html5` writer will default to ``:widths: auto`` and use - ``style="width: ..%"`` if the `"widths" option`__ sets explicite table - column widths. Use ``:widths: grid`` to keep the current default - column widths. + ``style="width: ..%"`` if the `"widths" option`__ is used. + Use the table-style__ setting with value "colwidths-grid" to + keep the current default. - __ docs/ref/rst/directives.html#table + __ docs/ref/rst/directives.html#table + __ docs/user/config.html#table-style - Move attribution behind the blockquote to comply with the `"living standard"`__? @@ -48,10 +49,12 @@ Future changes * `latex2e` writer: - - Change default of ``use_latex_citations`` to True. + - Change default of use_latex_citations_ setting to True. - - Remove ``--use-verbatim-when-possible`` option - (use ``--literal-block-env=verbatim``). + - Change default of legacy_column_widths_ setting to False. + + - Remove ``use_verbatim_when_possible`` setting + (use literal_block_env_: verbatim). * Remove the "rawsource" attribute and argument from nodes.Text: we store the null-escaped text in Text nodes since 0.16 so there is no @@ -78,6 +81,8 @@ Future changes .. _rst2html.py: docs/user/tools.html#rst2html-py .. _reference name: docs/ref/rst/restructuredtext.html#reference-names .. _html_writer: docs/user/config.html#html-writer +.. _literal_block_env: docs/user/config.html#literal-block-env +.. _use_latex_citations: docs/user/config.html#use-latex-citations Release 0.18.dev @@ -146,8 +151,10 @@ Release 0.18.dev * docutils/utils/math/math2html.py, docutils/utils/math/latex2mathml.py - Major refactoring and fixes/additions - (cf. `LaTeX syntax for mathematics`_). + - Major refactoring and fixes/additions + (cf. `LaTeX syntax for mathematics`_). + +* New configuration setting: [latex writers] legacy_column_widths_. * Various bugfixes and improvements (see HISTORY_). @@ -157,8 +164,9 @@ __ docs/ref/doctree.html#meta .. _id_prefix: docs/user/config.html#id-prefix .. _auto_id_prefix: docs/user/config.html#auto-id-prefix .. _details disclosure elements: - https://www.w3.org/TR/html52/interactive-elements.html#the-details-element + https://www.w3.org/TR/html52/interactive-elements.html#the-details-element .. _LaTeX syntax for mathematics: docs/ref/rst/mathematics.html +.. _legacy_column_widths: docs/user/config.html#legacy-column-widths Release 0.17.1 (2021-04-16) diff --git a/docutils/docs/ref/doctree.txt b/docutils/docs/ref/doctree.txt index 633ee796d..8d3f31481 100644 --- a/docutils/docs/ref/doctree.txt +++ b/docutils/docs/ref/doctree.txt @@ -978,6 +978,9 @@ Details In contrast to the definition in the exchange-table-model_, unitless values of the "colwidth" are interpreted as proportional values, not fixed values with unit "pt". + + .. The reference implementation `html4css2` converts column + widths values to percentages. Future versions of Docutils may use the standard form ``number*``, e.g., “5*” for 5 times the proportion. diff --git a/docutils/docs/ref/rst/directives.txt b/docutils/docs/ref/rst/directives.txt index c9dac074d..17cf56baa 100644 --- a/docutils/docs/ref/rst/directives.txt +++ b/docutils/docs/ref/rst/directives.txt @@ -947,18 +947,11 @@ Example:: The following options are recognized: -``widths`` : integer [integer...] or "auto" - A list of relative column widths. - The default is equal-width columns (100%/#columns). - - "auto" delegates the determination of column widths to the backend - (LaTeX, the HTML browser, ...). - -``width`` : `length`_ or `percentage`_ - Sets the width of the table to the specified length or percentage - of the line width. If omitted, the renderer determines the width - of the table based on its contents or the column ``widths``. +``align`` : "left", "center", or "right" + The horizontal alignment of the table. + (New in Docutils 0.13) + ``header-rows`` : integer The number of rows of list data to use in the table header. Defaults to 0. @@ -967,9 +960,21 @@ The following options are recognized: The number of table columns to use as stubs (row titles, on the left). Defaults to 0. -``align`` : "left", "center", or "right" - The horizontal alignment of the table. - (New in Docutils 0.13) + .. _table width: + +``width`` : `length`_ or `percentage`_ + Sets the width of the table to the specified length or percentage + of the line width. If omitted, the renderer determines the width + of the table based on its contents or the column ``widths``. + + .. _column widths: + +``widths`` : integer [integer...] or "auto" + A list of relative column widths. + The default is equal-width columns (100%/#columns). + + "auto" delegates the determination of column widths to the backend + (LaTeX, the HTML browser, ...). and the common options class_ and name_. diff --git a/docutils/docs/user/config.txt b/docutils/docs/user/config.txt index d84f02c92..9c2d8d412 100644 --- a/docutils/docs/user/config.txt +++ b/docutils/docs/user/config.txt @@ -1221,6 +1221,13 @@ In addition, the HTML writers support: (leave out the ``<colgroup>`` column specification). Overridden by the "widths" option of the `table directive`_. +.. TODO: the HTML5 writer also supports + + colwidths-grid + Backwards compatibility setting. Write column widths + determined from the source to the HTML file. + Overridden by the "widths" option of the `table directive`_. + Default: "". Option: ``--table-style``. .. _table directive: ../ref/rst/directives.html#table @@ -1588,6 +1595,7 @@ Default: "". Option: ``--hyperref-options``. .. _hyperref TeX package: http://tug.org/applications/hyperref/ + latex_preamble ~~~~~~~~~~~~~~ @@ -1607,13 +1615,33 @@ comma-separated list of class values as optional argument. If `False`, class values are handled with wrappers and admonitions use the ``DUadmonition`` environment. See `Generating LaTeX with Docutils`__ for details. -Default: False. +Default: False (changed in Docutils 0.18). Options: ``--legacy-class-functions``, ``--new-class-functions``. New in Docutils 0.17. __ latex.html#classes + +legacy_column_widths +~~~~~~~~~~~~~~~~~~~~ + +Use "legacy algorithm" or new algorithm to determine table column widths. + +The new algorithm limits the table width to the text width or specified +table width and keeps the ratio of specified column widths. + +Custom table and/or column widths can be set with the respective options +of the `table directive`_. See also `Generating LaTeX with Docutils`__. + +Default: True (will change to False in 0.19). +Options: ``--legacy-column-widths``, ``--new-column-widths``. + +New in Docutils 0.18. + +__ latex.html#table-style + + literal_block_env ~~~~~~~~~~~~~~~~~ @@ -1628,6 +1656,7 @@ Option: ``--literal-block-env``. .. _parsed literal: ../ref/rst/directives.html#parsed-literal + reference_label ~~~~~~~~~~~~~~~ @@ -1744,33 +1773,13 @@ table_style Specify the default style for tables_. See also `table_style [html writers]`_. -Supported values: - -standard - Borders around all cells. - -booktabs - A line above and below the table and one after the head. - -borderless - No borders. - -align-left, align-center, align-right - Align tables. - -colwidths-auto, colwidths-given - Column width determination by LaTeX or Docutils (default). - Overridden by the `table directive`_'s :widths: option. - - .. warning:: - - ``colwidths-auto`` is only suited for tables with simple cell content. - - LaTeX puts the content of auto-sized columns on one line (merging - paragraphs) and may fail with complex content. +Supported values: "booktabs", "borderless", "colwidths-auto", and "standard". +See `Generating LaTeX with Docutils`__ for details. Default: "standard". Option: ``--table-style``. +__ latex.html#tables + .. _template [latex writers]: diff --git a/docutils/docs/user/latex.txt b/docutils/docs/user/latex.txt index e42bca89d..7858c7a45 100644 --- a/docutils/docs/user/latex.txt +++ b/docutils/docs/user/latex.txt @@ -250,7 +250,7 @@ Example 2: ``language-`` set the elements language property. * The table element recognizes some special class values. See section - table_. + `table style`_. * If the legacy-class-functions_ setting is True, the special macros ``\DUadmonition`` and ``\DUtitle`` are written with a comma separated @@ -1614,62 +1614,52 @@ __ http://tex.stackexchange.com/questions/41370/ .. _CSS length unit ``px``: http://www.w3.org/TR/css-values-3/#px .. _reference pixel: http://www.w3.org/TR/css-values-3/#reference-pixel -table ------ +table style +------------ -A pre-configured table style can be globally selected via the table-style_ +A pre-configured *table style* can be globally selected via the table_style_ setting or set for individual tables via a `class directive`_ or the class option of the `table directive`_. -.. _table-style: config.html#table-style-latex-writers -.. _table directive: ../ref/rst/directives.html#table +Supported values: -topic element -------------- +standard + Borders around all cells. -A topic_ is like a block quote with a title, or a self-contained section -with no subsections. Topics and rubrics can be used at places where a -`section title`_ is not allowed (e.g. inside a directive). +booktabs + A line above and below the table and one after the head. -Example: - Use a standard paragraph for a topic:: +borderless + No borders. - \newcommand{\DUCLASStopic}{% - \renewenvironment{quote}{}{}% - } +colwidths-auto + Column width determination by LaTeX. + Overridden by the `table directive`_'s "widths" option. -.. _topic: ../ref/rst/directives.html#topic -.. _section title: ../ref/rst/restructuredtext.html#sections + .. warning:: + ``colwidths-auto`` is only suited for tables with simple cell content. -titles ------- + LaTeX puts the content of auto-sized columns on one line (merging + paragraphs) and may fail with complex content. -The titles of admonitions_, sidebar_, and `topic element`_ use -the ``\DUtitle`` command. +.. eventually in future -Example 1: - a centered and somewhat larger title for topcis:: + align-left, align-center, align-right + Align tables. - \newcommand*{\DUCLASStopic}{ - \renewcommand*{\DUtitle}[1]{\subsection*{\centering #1} - } - -Example 2: - a right-pointing hand as title for the "attention" directive:: +By default, *column widths* are computed from the source column widths. +The `legacy_column_widths`_ setting selects the conversion algorithm. +Custom column widths can be set with the "widths" option of the `table +directive`_. - \usepackage{pifont} - \newcommand*{\DUCLASSattention}{ - \renewcommand*{\DUtitle}[1]{\ding{43}} - } - - The title argument is "swallowed" by the command. - To have both, hand and title use:: +See also the section on problems with tables_ below. - \usepackage{pifont} - \newcommand*{\DUCLASSattention}{ - \newcommand*{\DUtitle}[1]{\ding{43} #1} - } +.. _new_column_widths: +.. _legacy_column_widths: config.html#legacy-column-widths +.. _table_style: config.html#table-style-latex-writers +.. _"widths" option: +.. _table directive: ../ref/rst/directives.html#table table of contents @@ -1734,6 +1724,36 @@ Example: .. _interpreted text: ../ref/rst/restructuredtext.html#interpreted-text +titles +------ + +The titles of admonitions_, sidebar_, and `topic element`_ use +the ``\DUtitle`` command. + +Example 1: + a centered and somewhat larger title for topcis:: + + \newcommand*{\DUCLASStopic}{ + \renewcommand*{\DUtitle}[1]{\subsection*{\centering #1} + } + +Example 2: + a right-pointing hand as title for the "attention" directive:: + + \usepackage{pifont} + \newcommand*{\DUCLASSattention}{ + \renewcommand*{\DUtitle}[1]{\ding{43}} + } + + The title argument is "swallowed" by the command. + To have both, hand and title use:: + + \usepackage{pifont} + \newcommand*{\DUCLASSattention}{ + \newcommand*{\DUtitle}[1]{\ding{43} #1} + } + + text encoding ------------- @@ -1778,6 +1798,24 @@ Note: .. _ucs: http://ctan.org/pkg/unicode +topic element +------------- + +A topic_ is like a block quote with a title, or a self-contained section +with no subsections. Topics and rubrics can be used at places where a +`section title`_ is not allowed (e.g. inside a directive). + +Example: + Use a standard paragraph for a topic:: + + \newcommand{\DUCLASStopic}{% + \renewenvironment{quote}{}{}% + } + +.. _topic: ../ref/rst/directives.html#topic +.. _section title: ../ref/rst/restructuredtext.html#sections + + transition element ------------------ @@ -1849,7 +1887,7 @@ footnote is put at the bottom of the page where the footnote text is located, maybe far away from the footnote mark (see e.g. `<rst/demo.txt>`_). To get footnote mark and text at the same page, keep footnote mark and -footnote text close together! +footnote text close together. non-breaking hyperlinks @@ -1908,7 +1946,8 @@ image inclusion Images__ are included in LaTeX with the help of the `graphicx` package. The supported file formats depend on the used driver: -* pdflatex_ and xelatex_ work with PNG, JPG, or PDF, but **not EPS**. +* pdflatex_, lualatex, and xelatex_ work with PNG, JPG, or PDF, + but **not EPS**. * Standard latex_ can include **only EPS** graphics, no other format. * latex + dvipdfmx works with EPS and JPG (add 'dvipdfmx' to the documentoptions_ or graphicx-option_ setting @@ -2045,8 +2084,7 @@ This results in footnotes, citations, and figures possibly being mixed at page foot. Workaround: - Select footnote and citation handling with the docutils-footnotes_ and - use-latex-citations_ options. + Select citation handling with the use_latex_citations_ option. If ``use-latex-citations`` is used, a bibliography is inserted right at the end of the document. *This should be customizable*. @@ -2056,34 +2094,20 @@ only by a single space or a newline) are combined to a single citation group, i.e. ``[cite1]_ [cite2]_`` results in ``\cite{cite1,cite2}``. The appearance in the output can be configured in a `style sheet`_. -.. _docutils-footnotes: config.html#docutils-footnotes -.. _use-latex-citations: config.html#use-latex-citations +.. _use_latex_citations: config.html#use-latex-citations Tables `````` -* reST-documents line length is assumed to be 80 characters. The - *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 `:widths:` option to manually set the table column widths. +* Too wide tables (cf. `bug #422`_): -* Table: multicol cells are always left aligned. + Try the new_column_widths_ algorithm or use the `"widths" option`_ to + manually set the table column widths. * Table cells with both multirow and multicolumn are currently not possible. -* literal-blocks in table cells: - - - If verbatim or flushleft is used one gets vertical space above and below. - - This is bad for the topmost paragraph in a cell, therefore the writer - uses raggedright. - - Ragged right fails on followup paragraphs as the vertical space would be - missing. - -* ``--table-style=booktabs``, ``..class:: booktab``: `booktabs` version - 1.00 does not work with `longtable`. This is solved in newer versions - (current is 2005/04/14 v1.61803). +.. _bug #422: https://sourceforge.net/p/docutils/bugs/422/ Figures diff --git a/docutils/docutils/parsers/rst/directives/tables.py b/docutils/docutils/parsers/rst/directives/tables.py index 08d107c20..ed343d54e 100644 --- a/docutils/docutils/parsers/rst/directives/tables.py +++ b/docutils/docutils/parsers/rst/directives/tables.py @@ -164,7 +164,7 @@ class RSTTable(Table): colspec['colwidth'] = col_width if self.widths == 'auto': table_node['classes'] += ['colwidths-auto'] - elif self.widths: # override "table-style" setting + elif self.widths: # "grid" or list of integers table_node['classes'] += ['colwidths-given'] self.add_name(table_node) if title: @@ -485,7 +485,7 @@ class ListTable(Table): table = nodes.table() if self.widths == 'auto': table['classes'] += ['colwidths-auto'] - elif self.widths: # override "table-style" setting + elif self.widths: # explicitely set column widths table['classes'] += ['colwidths-given'] tgroup = nodes.tgroup(cols=len(col_widths)) table += tgroup diff --git a/docutils/docutils/writers/latex2e/__init__.py b/docutils/docutils/writers/latex2e/__init__.py index fe3731a50..9e79f73f2 100644 --- a/docutils/docutils/writers/latex2e/__init__.py +++ b/docutils/docutils/writers/latex2e/__init__.py @@ -52,8 +52,10 @@ class Writer(writers.Writer): r'\usepackage{mathptmx} % Times', r'\usepackage[scaled=.90]{helvet}', r'\usepackage{courier}']) - table_style_values = ('standard', 'booktabs', 'nolines', 'borderless', - 'colwidths-auto', 'colwidths-given') + table_style_values = (# TODO: align-left, align-center, align-right, + 'booktabs', 'borderless', 'colwidths-auto', + 'colwidths-given', # set by parser if "widths" option is specified + 'nolines', 'standard') settings_spec = ( 'LaTeX-Specific Options', @@ -226,6 +228,17 @@ class Writer(writers.Writer): {'dest': 'legacy_class_functions', 'action': 'store_false', 'validator': frontend.validate_boolean}), + ('Use legacy algorithm to determine table column widths (default).', + ['--legacy-column-widths'], + {'default': True, + 'action': 'store_true', + 'validator': frontend.validate_boolean}), + ('Use new algorithm to determine table column widths ' + '(future default).', + ['--new-column-widths'], + {'dest': 'legacy_column_widths', + 'action': 'store_false', + 'validator': frontend.validate_boolean}), # TODO: implement "latex footnotes" alternative ('Footnotes with numbers/symbols by Docutils. (default) ' '(The alternative, --latex-footnotes, is not implemented yet.)', @@ -498,7 +511,7 @@ class SortableDict(dict): class PreambleCmds(object): """Building blocks for the latex preamble.""" -# Requirements +# Requirements and Setup PreambleCmds.color = r"""\usepackage{color}""" @@ -518,10 +531,8 @@ PreambleCmds.minitoc = r"""%% local table of contents 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.table_columnwidth = r"""\newcommand{\DUcolumnwidth}[1]{\dimexpr#1\DUtablewidth-2\tabcolsep\relax}""" PreambleCmds.textcomp = r"""\usepackage{textcomp} % text symbol macros""" # TODO? Options [force,almostfull] prevent spurious error messages, @@ -827,9 +838,6 @@ class DocumentClass(object): class Table(object): """Manage a table while traversing. - Maybe change to a mixin defining the visit/departs, but then - class Table internal variables are in the Translator. - Table style might be :standard: horizontal and vertical lines @@ -838,11 +846,11 @@ class Table(object): :nolines: alias for borderless :colwidths-auto: column widths determined by LaTeX - :colwidths-given: use colum widths from rST source """ def __init__(self, translator, latex_type): self._translator = translator self._latex_type = latex_type + self.legacy_column_widths = False self.close() self._colwidths = [] @@ -867,15 +875,20 @@ class Table(object): def is_open(self): return self._open - def set_table_style(self, table_style, classes): + def set_table_style(self, node, settings): + self.legacy_column_widths = settings.legacy_column_widths + if 'align' in node: + self.set('align', node['align']) + # TODO: elif 'align' in classes/settings.table-style: + # self.set('align', ...) borders = [cls.replace('nolines', 'borderless') - for cls in ['standard'] + table_style + classes - if cls in ('standard', 'booktabs', 'borderless', 'nolines')] + for cls in ['standard'] + settings.table_style + node['classes'] + if cls in ('standard', 'booktabs', 'borderless', 'nolines')] self.borders = borders[-1] - self.colwidths_auto = (('colwidths-auto' in classes - and 'colwidths-given' not in table_style) - or ('colwidths-auto' in table_style - and ('colwidths-given' not in classes))) + self.colwidths_auto = (('colwidths-auto' in node['classes'] + or 'colwidths-auto' in settings.table_style) + and 'colwidths-given' not in node['classes'] + and 'width' not in node) def get_latex_type(self): if self._latex_type == 'longtable' and not self.caption: @@ -911,7 +924,12 @@ class Table(object): else: opening = [r'\begin{%s}%s' % (latex_type, align)] if not self.colwidths_auto: - opening.insert(-1, r'\setlength{\DUtablewidth}{%s}%%'%width) + if self.borders == 'standard' and not self.legacy_column_widths: + opening.insert(-1, r'\setlength{\DUtablewidth}' + r'{\dimexpr%s-%i\arrayrulewidth\relax}%%' + % (width, len(self._col_specs)+1)) + else: + opening.insert(-1, r'\setlength{\DUtablewidth}{%s}%%' % width) return '\n'.join(opening) def get_closing(self): @@ -932,49 +950,67 @@ class Table(object): def get_colspecs(self, node): """Return column specification for longtable. - - Assumes reST line length being 80 characters. - Table width is hairy. - - === === - ABC DEF - === === - - usually gets too narrow, therefore we add 1 (fiddlefactor). """ bar = self.get_vertical_bar() - self._rowspan= [0] * len(self._col_specs) + self._rowspan = [0] * len(self._col_specs) if self.colwidths_auto: self._colwidths = [] latex_colspecs = ['l'] * len(self._col_specs) - else: + elif self.legacy_column_widths: + # use old algorithm for backwards compatibility width = 80 # assumed standard line length + factor = 0.93 # do not make it full linewidth # 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: + if total_width > width: factor *= width / total_width - self._colwidths = [(factor * float(node['colwidth']+1)/width) + self._colwidths = [(factor * (node['colwidth']+1)/width) + 0.005 for node in self._col_specs] latex_colspecs = ['p{%.3f\\DUtablewidth}' % colwidth for colwidth in self._colwidths] + else: + # No of characters corresponding to table width = 100% + # Characters/line with LaTeX article, A4, Times, default margins + # depends on character: M: 40, A: 50, x: 70, i: 120. + norm_length = 40 + # Allowance to prevent unpadded columns like + # === == + # ABC DE + # === == + # getting too narrow: + if 'colwidths-given' not in node.parent.parent['classes']: + allowance = 1 + else: + allowance = 0 # "widths" option specified, use exact ratio + self._colwidths = [(node['colwidth']+allowance)/norm_length + for node in self._col_specs] + total_width = sum(self._colwidths) + # Limit to 100%, force 100% if table width is specified: + if total_width > 1 or 'width' in node.parent.parent.attributes: + self._colwidths = [colwidth/total_width + for colwidth in self._colwidths] + latex_colspecs = ['p{\\DUcolumnwidth{%.3f}}' % 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._colwidths[self._cell_in_row] + if self.legacy_column_widths: + return '%.2f\\DUtablewidth' % self._colwidths[self._cell_in_row] + return '\\DUcolumnwidth{%.2f}' % self._colwidths[self._cell_in_row] except IndexError: return '*' def get_multicolumn_width(self, start, len_): """Return sum of columnwidths for multicell.""" try: - mc_width = sum([width - for width in ([self._colwidths[start + co] - for co in range(len_)])]) - return 'p{%.2f\\DUtablewidth}' % mc_width + multicol_width = sum([width + for width in ([self._colwidths[start + co] + for co in range(len_)])]) + if self.legacy_column_widths: + return 'p{%.2f\\DUtablewidth}' % multicol_width + return 'p{\\DUcolumnwidth{%.3f}}' % multicol_width except IndexError: return 'l' @@ -1009,11 +1045,17 @@ class Table(object): if 1 == self._translator.thead_depth(): a.append('\\endfirsthead\n') else: + n_c = len(self._col_specs) a.append('\\endhead\n') - a.append(r'\multicolumn{%d}{r}' % len(self._col_specs) + - r'{... continued on next page} \\') - a.append('\n\\endfoot\n\\endlastfoot\n') - # for longtable one could add firsthead, foot and lastfoot + # footer on all but last page (if it fits): + twidth = sum([node['colwidth']+2 for node in self._col_specs]) + if twidth > 30 or (twidth > 12 and not self.colwidths_auto): + a.append(r'\multicolumn{%d}{%s}' + % (n_c, self.get_multicolumn_width(0, n_c)) + + r'{\raggedleft\ldots continued on next page}\\' + + '\n') + a.append('\\endfoot\n\\endlastfoot\n') + # for longtable one could add firsthead, foot and lastfoot self._in_thead -= 1 return a @@ -2906,6 +2948,8 @@ class LaTeXTranslator(nodes.NodeVisitor): def visit_table(self, node): self.duclass_open(node) self.requirements['table'] = PreambleCmds.table + if not self.settings.legacy_column_widths: + self.requirements['table1'] = PreambleCmds.table_columnwidth if self.active_table.is_open(): self.table_stack.append(self.active_table) # nesting longtable does not work (e.g. 2007-04-18) @@ -2918,10 +2962,7 @@ class LaTeXTranslator(nodes.NodeVisitor): self.d_class.section(self.section_level).find('paragraph') != -1): self.out.append('\\leavevmode') self.active_table.open() - self.active_table.set_table_style(self.settings.table_style, - node['classes']) - if 'align' in node: - self.active_table.set('align', node['align']) + self.active_table.set_table_style(node, self.settings) if self.active_table.borders == 'booktabs': self.requirements['booktabs'] = r'\usepackage{booktabs}' self.push_output_collector([]) diff --git a/docutils/test/functional/expected/latex_cornercases.tex b/docutils/test/functional/expected/latex_cornercases.tex index 7e9c9a69b..5f7b79c44 100644 --- a/docutils/test/functional/expected/latex_cornercases.tex +++ b/docutils/test/functional/expected/latex_cornercases.tex @@ -18,6 +18,7 @@ \usepackage{longtable,ltcaption,array} \setlength{\extrarowheight}{2pt} \newlength{\DUtablewidth} % internal use in tables +\newcommand{\DUcolumnwidth}[1]{\dimexpr#1\DUtablewidth-2\tabcolsep\relax} \usepackage{textcomp} % text symbol macros %%% Custom LaTeX preamble @@ -61,20 +62,6 @@ } \fi -% numerical or symbol footnotes with hyperlinks and backlinks -\providecommand*{\DUfootnotemark}[3]{% - \raisebox{1em}{\hypertarget{#1}{}}% - \hyperlink{#2}{\textsuperscript{#3}}% -} -\providecommand{\DUfootnotetext}[4]{% - \begingroup% - \renewcommand{\thefootnote}{% - \protect\raisebox{1em}{\protect\hypertarget{#1}{}}% - \protect\hyperlink{#2}{#3}}% - \footnotetext{#4}% - \endgroup% -} - % legend environment (in figures and formal tables) \ifthenelse{\isundefined{\DUlegend}}{ \newenvironment{DUlegend}{\small}{} @@ -334,8 +321,6 @@ Image with 100\% width: In contrast to HTML, LaTeX does not support line-breaks in tables with \textquotedbl{}automatic\textquotedbl{} column widths. Each cell has just one line, paragraphs are merged (the writer emits a warning). -To provide for arbitrary cell content, the LaTeX writer defaults to -specifying column widths computed from the source: \begin{longtable}{|l|l|} \caption{problems with \textquotedbl{}auto\textquotedbl{} widths}\\ @@ -349,22 +334,19 @@ linebreak & 22 \\ \hline \end{longtable} -Docutils stores table column widths in the \textquotedbl{}doctree\textquotedbl{} (in characters, -excluding the separator). In the .tex source, column widths are written -as percent of the \texttt{\textbackslash{}linewidth}, computed on the assumption that an -average text line is 80 characters wide. An allowance added to the stored -column widths compensates the padding (\texttt{\textbackslash{}tabcolsep}) added by LaTeX. -The algorightm provides a working solution in many cases.\DUfootnotemark{footnote-reference-1}{footnote-1}{1} +To provide for arbitrary cell content, the LaTeX writer defaults to +specifying column widths computed from the source column widths. This +works sufficiently in many cases: -\setlength{\DUtablewidth}{\linewidth}% -\begin{longtable}{|p{0.133\DUtablewidth}|p{0.179\DUtablewidth}|p{0.179\DUtablewidth}|p{0.110\DUtablewidth}|p{0.121\DUtablewidth}|p{0.145\DUtablewidth}|} -\caption{a table with multi-paragraph multicolumn cells}\\ +\setlength{\DUtablewidth}{\dimexpr\linewidth-7\arrayrulewidth\relax}% +\begin{longtable}{|p{\DUcolumnwidth{0.153}}|p{\DUcolumnwidth{0.208}}|p{\DUcolumnwidth{0.208}}|p{\DUcolumnwidth{0.125}}|p{\DUcolumnwidth{0.139}}|p{\DUcolumnwidth{0.167}}|} +\caption{a table with multi-paragraph multi-column cells}\\ \hline test & \textbf{bold hd} - & \multicolumn{3}{p{0.41\DUtablewidth}|}{% + & \multicolumn{3}{p{\DUcolumnwidth{0.472}}|}{% multicolumn 1 With a second paragraph @@ -372,7 +354,7 @@ With a second paragraph \emph{emph hd} \\ \hline -\multicolumn{2}{|p{0.31\DUtablewidth}|}{% +\multicolumn{2}{|p{\DUcolumnwidth{0.361}}|}{% multicolumn 2 With a second paragraph @@ -388,7 +370,7 @@ cell \hline cell - & \multicolumn{2}{p{0.36\DUtablewidth}|}{% + & \multicolumn{2}{p{\DUcolumnwidth{0.417}}|}{% multicolumn 3 (one line, but very very very very very looooong) @@ -406,28 +388,17 @@ cell cell & cell - & \multicolumn{3}{p{0.38\DUtablewidth}|}{% + & \multicolumn{3}{p{\DUcolumnwidth{0.431}}|}{% Short multicolumn 4 } \\ \hline \end{longtable} -% -\DUfootnotetext{footnote-1}{footnote-reference-1}{1}{% -Currently, computed column widths are too wide. -TODO: Account for the \texttt{\textbackslash{}tabcolsep}. -} - -For good typographic results experimenting with the \textquotedbl{}width\textquotedbl{} and \textquotedbl{}widths\textquotedbl{} -options of the \href{https://docutils.sourceforge.io/docs/ref/rst/directives.html\#table}{table directive} may be required. (Unfortunately, the -interpretation of these values differs between LaTeX writer and HTML -writer.) -Simple tables use often no padding in the source while grid tables -without padding look clumsy. Setting \texttt{:widths: auto} in the source -whenever possible is recommended.\DUfootnotemark{footnote-reference-2}{footnote-2}{2} +A problem with the source-derived column widths is that simple tables +often use no padding while grid tables without padding look cramped: -\setlength{\DUtablewidth}{\linewidth}% -\begin{longtable}{|p{0.051\DUtablewidth}|p{0.028\DUtablewidth}|p{0.028\DUtablewidth}|p{0.028\DUtablewidth}|} +\setlength{\DUtablewidth}{\dimexpr\linewidth-5\arrayrulewidth\relax}% +\begin{longtable}{|p{\DUcolumnwidth{0.100}}|p{\DUcolumnwidth{0.050}}|p{\DUcolumnwidth{0.050}}|p{\DUcolumnwidth{0.050}}|} \caption{simple table, not padded in the source}\\ \hline \textbf{% @@ -454,7 +425,7 @@ D } \\ \hline \endhead -\multicolumn{4}{r}{... continued on next page} \\ +\multicolumn{4}{p{\DUcolumnwidth{0.250}}}{\raggedleft\ldots continued on next page}\\ \endfoot \endlastfoot @@ -479,8 +450,8 @@ d \hline \end{longtable} -\setlength{\DUtablewidth}{\linewidth}% -\begin{longtable}{|p{0.075\DUtablewidth}|p{0.051\DUtablewidth}|p{0.051\DUtablewidth}|p{0.051\DUtablewidth}|} +\setlength{\DUtablewidth}{\dimexpr\linewidth-5\arrayrulewidth\relax}% +\begin{longtable}{|p{\DUcolumnwidth{0.150}}|p{\DUcolumnwidth{0.100}}|p{\DUcolumnwidth{0.100}}|p{\DUcolumnwidth{0.100}}|} \caption{grid table, padded cells}\\ \hline \textbf{% @@ -507,7 +478,7 @@ D } \\ \hline \endhead -\multicolumn{4}{r}{... continued on next page} \\ +\multicolumn{4}{p{\DUcolumnwidth{0.450}}}{\raggedleft\ldots continued on next page}\\ \endfoot \endlastfoot @@ -532,6 +503,9 @@ d \hline \end{longtable} +For better typographic results, setting the \DUroletitlereference{width} and/or +\DUroletitlereference{widths} options of the \href{https://docutils.sourceforge.io/docs/ref/rst/directives.html\#table}{table directive} is recommended. + \begin{longtable}{|l|l|l|l|} \caption{grid table, auto-width columns}\\ \hline @@ -543,7 +517,6 @@ d \textbf{A} & \textbf{B} & \textbf{C} & \textbf{D} \\ \hline \endhead -\multicolumn{4}{r}{... continued on next page} \\ \endfoot \endlastfoot 100 & 2 & 3 & 4 \\ @@ -551,41 +524,35 @@ d EUR & b & c & d \\ \hline \end{longtable} -% -\DUfootnotetext{footnote-2}{footnote-reference-2}{2}{% -Currently, the last column is extra wide because the -writer adds a hint \textquotedbl{}... continued on next page\textquotedbl{} to longtables with a -header row. The hint is only shown if the table does not fit on one -page but always affects the table width. -} -If a specific total width is desired, \textquotedbl{}auto\textquotedbl{} column widths fail, - -\begin{longtable}{|l|l|l|l|} -\caption{75\% wide table with \textquotedbl{}auto\textquotedbl{} column widths}\\ +\begin{longtable}{|l|l|} +\caption{table with multi-row header and \textquotedbl{}auto\textquotedbl{} column-widths}\\ \hline -\textbf{A} & \textbf{B} & \textbf{C} & \textbf{D} \\ +\multirow{2}{*}{\textbf{XXX}} & \textbf{Variable Summary} \\ +\cline{2-2} + & \textbf{Description} \\ \hline \endfirsthead -\caption[]{75\% wide table with \textquotedbl{}auto\textquotedbl{} column widths (... continued)}\\ +\caption[]{table with multi-row header and \textquotedbl{}auto\textquotedbl{} column-widths (... continued)}\\ \hline -\textbf{A} & \textbf{B} & \textbf{C} & \textbf{D} \\ +\multirow{2}{*}{\textbf{XXX}} & \textbf{Variable Summary} \\ +\cline{2-2} + & \textbf{Description} \\ \hline \endhead -\multicolumn{4}{r}{... continued on next page} \\ +\multicolumn{2}{l}{\raggedleft\ldots continued on next page}\\ \endfoot \endlastfoot -100 & 2 & 3 & 4 \\ -\hline -EUR & b & c & d \\ +\multicolumn{2}{|l|}{multi-column cell} \\ \hline \end{longtable} -but \textquotedbl{}grid\textquotedbl{} based or explicit column widths should work:\DUfootnotemark{footnote-reference-3}{footnote-3}{3} +The \DUroletitlereference{width} option overrides \textquotedbl{}auto\textquotedbl{} \DUroletitlereference{widths} as standard LaTeX tables +don't have a global width setting: -\setlength{\DUtablewidth}{0.750\linewidth}% -\begin{longtable}{|p{0.051\DUtablewidth}|p{0.040\DUtablewidth}|p{0.040\DUtablewidth}|p{0.040\DUtablewidth}|} -\caption{75\% wide table with explicit column widths}\\ +\setlength{\DUtablewidth}{\dimexpr0.600\linewidth-5\arrayrulewidth\relax}% +\begin{longtable}{|p{\DUcolumnwidth{0.400}}|p{\DUcolumnwidth{0.200}}|p{\DUcolumnwidth{0.200}}|p{\DUcolumnwidth{0.200}}|} +\caption{This table has \DUroletitlereference{widths} \textquotedbl{}auto\textquotedbl{} (ignored) and \DUroletitlereference{width} 60\%.}\\ \hline \textbf{% A @@ -598,7 +565,7 @@ D } \\ \hline \endfirsthead -\caption[]{75\% wide table with explicit column widths (... continued)}\\ +\caption[]{This table has \DUroletitlereference{widths} \textquotedbl{}auto\textquotedbl{} (ignored) and \DUroletitlereference{width} 60\%. (... continued)}\\ \hline \textbf{% A @@ -611,7 +578,7 @@ D } \\ \hline \endhead -\multicolumn{4}{r}{... continued on next page} \\ +\multicolumn{4}{p{\DUcolumnwidth{1.000}}}{\raggedleft\ldots continued on next page}\\ \endfoot \endlastfoot @@ -635,43 +602,105 @@ d \\ \hline \end{longtable} -% -\DUfootnotetext{footnote-3}{footnote-reference-3}{3}{% -Bug: The table \textquotedbl{}width\textquotedbl{} option is currently ignored. + + +\subsection{Nested tables% + \label{nested-tables}% } -\begin{longtable}{|l|l|} -\caption{table with multirow header and \textquotedbl{}auto\textquotedbl{} column-widths}\\ +\setlength{\DUtablewidth}{\dimexpr\linewidth-3\arrayrulewidth\relax}% +\begin{longtable*}{|p{\DUcolumnwidth{0.700}}|p{\DUcolumnwidth{0.300}}|} \hline -\multirow{2}{*}{\textbf{XXX}} & \textbf{Variable Summary} \\ -\cline{2-2} - & \textbf{Description} \\ + +Lorem ipsum dolor sit amet, consectetur + & +adipisicing elit + \\ \hline -\endfirsthead -\caption[]{table with multirow header and \textquotedbl{}auto\textquotedbl{} column-widths (... continued)}\\ + +\noindent\makebox[\linewidth][r]{% +\setlength{\DUtablewidth}{\dimexpr\linewidth-3\arrayrulewidth\relax}% +\begin{tabular}{|p{\DUcolumnwidth{0.150}}|p{\DUcolumnwidth{0.150}}|} \hline -\multirow{2}{*}{\textbf{XXX}} & \textbf{Variable Summary} \\ -\cline{2-2} - & \textbf{Description} \\ + +1 + & +2 + \\ \hline -\endhead -\multicolumn{2}{r}{... continued on next page} \\ -\endfoot -\endlastfoot -\multicolumn{2}{|l|}{multicollumn cell} \\ +\end{tabular} +} + & +cell 1, 2 + \\ +\hline + +table width depends on parent column + +\noindent\makebox[\linewidth][c]{% +\setlength{\DUtablewidth}{\dimexpr\linewidth-3\arrayrulewidth\relax}% +\begin{tabular}{|p{\DUcolumnwidth{0.150}}|p{\DUcolumnwidth{0.150}}|} \hline -\end{longtable} + +1 + & +2 + \\ +\hline +\end{tabular} +} + +better use \textquotedbl{}auto\textquotedbl{} widths, see below + & +same table + +\setlength{\DUtablewidth}{\dimexpr\linewidth-3\arrayrulewidth\relax}% +\begin{tabular}{|p{\DUcolumnwidth{0.150}}|p{\DUcolumnwidth{0.150}}|} +\hline + +1 + & +2 + \\ +\hline +\end{tabular} + +in narrow column + \\ +\hline + +\noindent\makebox[\linewidth][r]{% +\begin{tabular}{|l|l|} +\hline +1 & 2 \\ +\hline +\end{tabular} +} + +\begin{description} +\item[{definition:}] +list + +\end{description} + & +cell 3, 2 + \\ +\hline +\end{longtable*} \subsection{TODO% \label{todo}% } -Limit the total width of a table to the \texttt{\textbackslash{}linewidth}, unless the table -width is explicitely specified in the source. +\begin{itemize} +\item Tables with multi-paragraph multi-row cells currently fail due to a +LaTeX limitation (see \url{https://sourceforge.net/p/docutils/bugs/225/}). + +\item Tweak vertical spacing in table cells containing multiple elements. +\end{itemize} -Tables with multi-paragraph multirow cells currently fail due to a LaTeX -limitation (see \url{https://sourceforge.net/p/docutils/bugs/225/}). +See also \texttt{test/functional/input/data/latex-problematic.txt}. \section{Monospaced non-alphanumeric characters% @@ -844,8 +873,8 @@ In LaTeX, we must set an explicit anchor (\texttt{\textbackslash{}phantomsection \phantomsection\label{hypertarget-in-plain-text}hypertarget in plain text or in a figure but not in a longtable or caption: -\setlength{\DUtablewidth}{\linewidth}% -\begin{longtable}{|p{0.075\DUtablewidth}|p{0.075\DUtablewidth}|p{0.075\DUtablewidth}|} +\setlength{\DUtablewidth}{\dimexpr\linewidth-4\arrayrulewidth\relax}% +\begin{longtable}{|p{\DUcolumnwidth{0.150}}|p{\DUcolumnwidth{0.150}}|p{\DUcolumnwidth{0.150}}|} \caption{Table with % \label{hypertarget-in-table-title}hypertarget in table title.}\\ \hline diff --git a/docutils/test/functional/expected/latex_memoir.tex b/docutils/test/functional/expected/latex_memoir.tex index e56332837..e4fdbf88f 100644 --- a/docutils/test/functional/expected/latex_memoir.tex +++ b/docutils/test/functional/expected/latex_memoir.tex @@ -987,7 +987,7 @@ not A } \\ \hline \endhead -\multicolumn{2}{r}{... continued on next page} \\ +\multicolumn{2}{p{0.15\DUtablewidth}}{\raggedleft\ldots continued on next page}\\ \endfoot \endlastfoot @@ -1024,7 +1024,7 @@ not A } \\ \hline \endhead -\multicolumn{2}{r}{... continued on next page} \\ +\multicolumn{2}{p{0.15\DUtablewidth}}{\raggedleft\ldots continued on next page}\\ \endfoot \endlastfoot @@ -1061,7 +1061,7 @@ not A } \\ \hline \endhead -\multicolumn{2}{r}{... continued on next page} \\ +\multicolumn{2}{p{0.15\DUtablewidth}}{\raggedleft\ldots continued on next page}\\ \endfoot \endlastfoot @@ -1091,7 +1091,6 @@ writer/backend). \textbf{A} & \textbf{B} & \textbf{A or B} \\ \hline \endhead -\multicolumn{3}{r}{... continued on next page} \\ \endfoot \endlastfoot False & False & False \\ @@ -1575,7 +1574,7 @@ A or B } \\ \hline \endhead -\multicolumn{3}{r}{... continued on next page} \\ +\multicolumn{3}{p{0.24\DUtablewidth}}{\raggedleft\ldots continued on next page}\\ \endfoot \endlastfoot @@ -1643,7 +1642,7 @@ Header 3 } \\ \hline \endhead -\multicolumn{3}{r}{... continued on next page} \\ +\multicolumn{3}{p{0.68\DUtablewidth}}{\raggedleft\ldots continued on next page}\\ \endfoot \endlastfoot @@ -1703,7 +1702,7 @@ Description } \\ \hline \endhead -\multicolumn{3}{r}{... continued on next page} \\ +\multicolumn{3}{p{0.49\DUtablewidth}}{\raggedleft\ldots continued on next page}\\ \endfoot \endlastfoot \textbf{% diff --git a/docutils/test/functional/expected/standalone_rst_latex.tex b/docutils/test/functional/expected/standalone_rst_latex.tex index 0045fd15e..92822cd64 100644 --- a/docutils/test/functional/expected/standalone_rst_latex.tex +++ b/docutils/test/functional/expected/standalone_rst_latex.tex @@ -988,7 +988,7 @@ not A } \\ \hline \endhead -\multicolumn{2}{r}{... continued on next page} \\ +\multicolumn{2}{p{0.15\DUtablewidth}}{\raggedleft\ldots continued on next page}\\ \endfoot \endlastfoot @@ -1025,7 +1025,7 @@ not A } \\ \hline \endhead -\multicolumn{2}{r}{... continued on next page} \\ +\multicolumn{2}{p{0.15\DUtablewidth}}{\raggedleft\ldots continued on next page}\\ \endfoot \endlastfoot @@ -1062,7 +1062,7 @@ not A } \\ \hline \endhead -\multicolumn{2}{r}{... continued on next page} \\ +\multicolumn{2}{p{0.15\DUtablewidth}}{\raggedleft\ldots continued on next page}\\ \endfoot \endlastfoot @@ -1092,7 +1092,6 @@ writer/backend). \textbf{A} & \textbf{B} & \textbf{A or B} \\ \hline \endhead -\multicolumn{3}{r}{... continued on next page} \\ \endfoot \endlastfoot False & False & False \\ @@ -1597,7 +1596,7 @@ A or B } \\ \hline \endhead -\multicolumn{3}{r}{... continued on next page} \\ +\multicolumn{3}{p{0.24\DUtablewidth}}{\raggedleft\ldots continued on next page}\\ \endfoot \endlastfoot @@ -1665,7 +1664,7 @@ Header 3 } \\ \hline \endhead -\multicolumn{3}{r}{... continued on next page} \\ +\multicolumn{3}{p{0.68\DUtablewidth}}{\raggedleft\ldots continued on next page}\\ \endfoot \endlastfoot @@ -1725,7 +1724,7 @@ Description } \\ \hline \endhead -\multicolumn{3}{r}{... continued on next page} \\ +\multicolumn{3}{p{0.49\DUtablewidth}}{\raggedleft\ldots continued on next page}\\ \endfoot \endlastfoot \textbf{% diff --git a/docutils/test/functional/expected/standalone_rst_xetex.tex b/docutils/test/functional/expected/standalone_rst_xetex.tex index 4e5a06602..41acad56f 100644 --- a/docutils/test/functional/expected/standalone_rst_xetex.tex +++ b/docutils/test/functional/expected/standalone_rst_xetex.tex @@ -1022,7 +1022,7 @@ not A } \\ \hline \endhead -\multicolumn{2}{r}{... continued on next page} \\ +\multicolumn{2}{p{0.15\DUtablewidth}}{\raggedleft\ldots continued on next page}\\ \endfoot \endlastfoot @@ -1059,7 +1059,7 @@ not A } \\ \hline \endhead -\multicolumn{2}{r}{... continued on next page} \\ +\multicolumn{2}{p{0.15\DUtablewidth}}{\raggedleft\ldots continued on next page}\\ \endfoot \endlastfoot @@ -1096,7 +1096,7 @@ not A } \\ \hline \endhead -\multicolumn{2}{r}{... continued on next page} \\ +\multicolumn{2}{p{0.15\DUtablewidth}}{\raggedleft\ldots continued on next page}\\ \endfoot \endlastfoot @@ -1126,7 +1126,6 @@ writer/backend). \textbf{A} & \textbf{B} & \textbf{A or B} \\ \hline \endhead -\multicolumn{3}{r}{... continued on next page} \\ \endfoot \endlastfoot False & False & False \\ @@ -1635,7 +1634,7 @@ A or B } \\ \hline \endhead -\multicolumn{3}{r}{... continued on next page} \\ +\multicolumn{3}{p{0.24\DUtablewidth}}{\raggedleft\ldots continued on next page}\\ \endfoot \endlastfoot @@ -1703,7 +1702,7 @@ Header 3 } \\ \hline \endhead -\multicolumn{3}{r}{... continued on next page} \\ +\multicolumn{3}{p{0.68\DUtablewidth}}{\raggedleft\ldots continued on next page}\\ \endfoot \endlastfoot @@ -1763,7 +1762,7 @@ Description } \\ \hline \endhead -\multicolumn{3}{r}{... continued on next page} \\ +\multicolumn{3}{p{0.49\DUtablewidth}}{\raggedleft\ldots continued on next page}\\ \endfoot \endlastfoot \textbf{% diff --git a/docutils/test/functional/input/data/tables_latex.txt b/docutils/test/functional/input/data/tables_latex.txt index 4b48bd296..7bd55b478 100644 --- a/docutils/test/functional/input/data/tables_latex.txt +++ b/docutils/test/functional/input/data/tables_latex.txt @@ -4,8 +4,6 @@ Tables In contrast to HTML, LaTeX does not support line-breaks in tables with "automatic" column widths. Each cell has just one line, paragraphs are merged (the writer emits a warning). -To provide for arbitrary cell content, the LaTeX writer defaults to -specifying column widths computed from the source: .. table:: problems with "auto" widths :widths: auto @@ -20,14 +18,11 @@ specifying column widths computed from the source: | linebreak | | +-----------+------------------+ -Docutils stores table column widths in the "doctree" (in characters, -excluding the separator). In the .tex source, column widths are written -as percent of the ``\linewidth``, computed on the assumption that an -average text line is 80 characters wide. An allowance added to the stored -column widths compensates the padding (``\tabcolsep``) added by LaTeX. -The algorightm provides a working solution in many cases. [#]_ +To provide for arbitrary cell content, the LaTeX writer defaults to +specifying column widths computed from the source column widths. This +works sufficiently in many cases: -.. table:: a table with multi-paragraph multicolumn cells +.. table:: a table with multi-paragraph multi-column cells +----------+--------------+---------------------------------+-----------+ | test | **bold hd** | multicolumn 1 | *emph hd* | @@ -45,17 +40,9 @@ The algorightm provides a working solution in many cases. [#]_ | cell | cell | cell | Short multicolumn 4 | +----------+--------------+--------------+------------------------------+ -.. [#] Currently, computed column widths are too wide. - TODO: Account for the ``\tabcolsep``. -For good typographic results experimenting with the "width" and "widths" -options of the `table directive`_ may be required. (Unfortunately, the -interpretation of these values differs between LaTeX writer and HTML -writer.) - -Simple tables use often no padding in the source while grid tables -without padding look clumsy. Setting ``:widths: auto`` in the source -whenever possible is recommended. [#]_ +A problem with the source-derived column widths is that simple tables +often use no padding while grid tables without padding look cramped: .. table:: simple table, not padded in the source @@ -76,6 +63,8 @@ whenever possible is recommended. [#]_ | EUR | b | c | d | +-----+---+---+---+ +For better typographic results, setting the `width` and/or +`widths` options of the `table directive`_ is recommended. .. table:: grid table, auto-width columns :widths: auto @@ -88,17 +77,23 @@ whenever possible is recommended. [#]_ | EUR | b | c | d | +-----+---+---+---+ +.. table:: table with multi-row header and "auto" column-widths + :widths: auto -.. [#] Currently, the last column is extra wide because the - writer adds a hint "... continued on next page" to longtables with a - header row. The hint is only shown if the table does not fit on one - page but always affects the table width. + +------------+-------------------+ + | XXX | Variable Summary | + | +-------------------+ + | | Description | + +============+===================+ + | multi-column cell | + +--------------------------------+ -If a specific total width is desired, "auto" column widths fail, +The `width` option overrides "auto" `widths` as standard LaTeX tables +don't have a global width setting: -.. table:: 75% wide table with "auto" column widths +.. table:: This table has `widths` "auto" (ignored) and `width` 60%. :widths: auto - :width: 75% + :width: 60% === = = = A B C D @@ -107,41 +102,53 @@ If a specific total width is desired, "auto" column widths fail, EUR b c d === = = = -but "grid" based or explicit column widths should work: [#]_ - -.. table:: 75% wide table with explicit column widths - :width: 75% - :widths: 3 2 2 2 - - === = = = - A B C D - === = = = - 100 2 3 4 - EUR b c d - === = = = +.. _table directive: + https://docutils.sourceforge.io/docs/ref/rst/directives.html#table -.. [#] Bug: The table "width" option is currently ignored. -.. table:: table with multirow header and "auto" column-widths - :widths: auto +Nested tables +------------- + ++-----------------------------------------+-----------------+ +| Lorem ipsum dolor sit amet, consectetur | adipisicing elit| ++-----------------------------------------+-----------------+ +| .. table:: | cell 1, 2 | +| :align: right | | +| | | +| +-----+-----+ | | +| | 1 | 2 | | | +| +-----+-----+ | | ++-----------------------------------------+-----------------+ +| table width depends on parent column | same table | +| | | +| .. table:: | | +| :align: center | | +| | | +| +-----+-----+ | +-----+-----+ | +| | 1 | 2 | | | 1 | 2 | | +| +-----+-----+ | +-----+-----+ | +| | | +| better use "auto" widths, see below | in narrow column| ++-----------------------------------------+-----------------+ +| .. table:: | cell 3, 2 | +| :align: right | | +| :widths: auto | | +| | | +| +-----+-----+ | | +| | 1 | 2 | | | +| +-----+-----+ | | +| | | +| definition: | | +| list | | ++-----------------------------------------+-----------------+ - +------------+-------------------+ - | XXX | Variable Summary | - | +-------------------+ - | | Description | - +============+===================+ - | multicollumn cell | - +--------------------------------+ TODO ---- -Limit the total width of a table to the ``\linewidth``, unless the table -width is explicitely specified in the source. +* Tables with multi-paragraph multi-row cells currently fail due to a + LaTeX limitation (see https://sourceforge.net/p/docutils/bugs/225/). -Tables with multi-paragraph multirow cells currently fail due to a LaTeX -limitation (see https://sourceforge.net/p/docutils/bugs/225/). +* Tweak vertical spacing in table cells containing multiple elements. - -.. _table directive: - https://docutils.sourceforge.io/docs/ref/rst/directives.html#table +See also ``test/functional/input/data/latex-problematic.txt``. diff --git a/docutils/test/functional/tests/latex_cornercases.py b/docutils/test/functional/tests/latex_cornercases.py index 396f0cc6d..3b6bbbbac 100644 --- a/docutils/test/functional/tests/latex_cornercases.py +++ b/docutils/test/functional/tests/latex_cornercases.py @@ -8,3 +8,4 @@ parser_name = "rst" writer_name = "latex" # Extra setting we need +settings_overrides['legacy_column_widths'] = False diff --git a/docutils/test/test_writers/test_latex2e.py b/docutils/test/test_writers/test_latex2e.py index d6544ab3a..a1434a458 100755 --- a/docutils/test/test_writers/test_latex2e.py +++ b/docutils/test/test_writers/test_latex2e.py @@ -875,7 +875,7 @@ Title } & \\ \hline \endhead -\multicolumn{2}{r}{... continued on next page} \\ +\multicolumn{2}{p{0.16\DUtablewidth}}{\raggedleft\ldots continued on next page}\\ \endfoot \endlastfoot |
