Skip to content

Commit e7c0881

Browse files
authored
LaTeX: support for booktabs-style and zebra-striped tables (#10759)
This is a combination of 2 + 28 + 7 + and some more commits... * Cherry-pick: Add support for booktabs-style tables to LaTeX builder * Cherry-pick: Add support for zebra-striped tables to LaTeX builder Co-authored-by: Stefan Wiehler <[email protected]> Above work originally initiated by @sephalon (thanks!) Development refactored and continued by @jfbu * latex_table_style configuration, support booktabs, colorrows, borderless Some details: - Simplify a bit a conditional in the longtable template This also puts the target for a longtable with a label but no caption above the toprule for better hyperlinking (testing shows hyperlink target can not end up alone at bottom of previous page). - Extend allowed syntax for colour assignments via 'sphinxsetup' - latex_table_style new configuration value and coloured rows For the user interface I tried to look for inspiration in https://docutils.sourceforge.io/docs/user/config.html#table-style which mentions booktabs and borderless. They also mention captionbelow which we can implement later, now that architecture is here. They don't mention coloured rows. - Test on our own document... looks fine! - Work-around an incompatibility of \cline with row colours - Reverse priority of classes to allow overruling booktabs by standard after parsing source but before letting LaTeX writer act - Closes #8220 Commit bb859c6 already improved a bit, this finishes it (as :rst:dir:`rst-class` was actually not linking to anywhere). - Let booktabs style defaults to *not* using \cmidrule. They actually don't make much sense there, as all \hline's are removed. - Add \sphinxnorowcolor which allows construct such as this one in a tabularcolumns directive: >{\columncolor{blue}\sphinxnorowcolor} else LaTeX always overrides column colour by row colour - Add TableMergeColorHeader, TableMergeColorOdd, TableMergeColorEven so single-row merged cells can be styled especially - Extend row colours to all header rows not only the first one (all header rows will share same colour settings) - Auto-adjust to a no '|'-colspec for optimal handling of merged cell - Add \sphinxcolorblend - Workaround LaTeX's \cline features and other grid tables matters - Add \sphinxbuildwarning for important warnings - Fix some white gaps in merged cells of tables with vlines and colorrows - Work around LaTeX's \cline serious deficiencies for complex grid tables This commit corrects \cline badly impacting vertical spacing and making tables look even more cramped as they usually are in LaTeX (although one sees it clearly only with \arrarrulewidth a bit more than the LaTeX default of 0.4pt). Most importantly this commit solves the problem that \cline's got masked by colour panels from the row below. - Update CHANGES for PR #10759 - Improve documentation of new latex_table_style regarding colours
1 parent c51a88d commit e7c0881

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1815
-417
lines changed

CHANGES

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Deprecated
1313
Features added
1414
--------------
1515

16+
* #10759: LaTeX: add :confval:`latex_table_style` and support the
17+
``'booktabs'``, ``'borderless'``, and ``'colorrows'`` styles.
1618
* #10840: One can cross-reference including an option value like ``:option:`--module=foobar```,
1719
``:option:`--module[=foobar]``` or ``:option:`--module foobar```.
1820
Patch by Martin Liska.

doc/conf.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,15 @@
7878
{\begin{sphinxtheindex}\end{sphinxtheindex}}
7979
''',
8080
'sphinxsetup': """%
81-
VerbatimColor={RGB}{242,242,242},%
81+
VerbatimColor=black!5,% tests 5.2.0 extended syntax
8282
VerbatimBorderColor={RGB}{32,32,32},%
8383
pre_border-radius=3pt,%
8484
pre_box-decoration-break=slice,%
8585
""",
8686
}
8787
latex_show_urls = 'footnote'
8888
latex_use_xindy = True
89+
latex_table_style = ['booktabs', 'colorrows']
8990

9091
autodoc_member_order = 'groupwise'
9192
autosummary_generate = False

doc/extdev/deprecated.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ major versions (for more details, please see :ref:`deprecation-policy`).
1010

1111
The following is a list of deprecated interfaces.
1212

13-
.. tabularcolumns:: |>{\raggedright}\Y{.4}|>{\centering}\Y{.1}|>{\centering}\Y{.12}|>{\raggedright\arraybackslash}\Y{.38}|
13+
.. tabularcolumns:: >{\raggedright}\Y{.4}>{\centering}\Y{.1}>{\sphinxcolorblend{!95!red}\centering\noindent\bfseries\color{red}}\Y{.12}>{\raggedright\arraybackslash}\Y{.38}
1414

1515
.. list-table:: deprecated APIs
1616
:header-rows: 1
@@ -19,7 +19,7 @@ The following is a list of deprecated interfaces.
1919

2020
* - Target
2121
- Deprecated
22-
- (will be) Removed
22+
- Removed
2323
- Alternatives
2424

2525
* - HTML 4 support

doc/latex.rst

+62-8
Original file line numberDiff line numberDiff line change
@@ -823,16 +823,31 @@ Do not use quotes to enclose values, whether numerical or strings.
823823
definition of the continuation symbol was changed at 1.5 to accommodate
824824
various font sizes (e.g. code-blocks can be in footnotes).
825825

826+
.. note::
827+
828+
Values for colour keys must either:
829+
830+
- obey the syntax of the ``\definecolor`` LaTeX command, e.g. something
831+
such as ``VerbatimColor={rgb}{0.2,0.3,0.5}`` or ``{RGB}{37,23,255}`` or
832+
``{gray}{0.75}`` or (only with package ``xcolor``) ``{HTML}{808080}`` or
833+
...
834+
835+
- or obey the syntax of the ``\colorlet`` command from package ``xcolor``
836+
(which then must exist in the LaTeX installation),
837+
e.g. ``VerbatimColor=red!10`` or ``red!50!green`` or ``-red!75`` or
838+
``MyPreviouslyDefinedColour`` or... Refer to xcolor_ documentation for
839+
this syntax.
840+
841+
.. _xcolor: https://ctan.org/pkg/xcolor
842+
843+
.. versionchanged:: 5.2.0
844+
Formerly only the ``\definecolor`` syntax was accepted.
845+
826846
``TitleColor``
827847
The colour for titles (as configured via use of package "titlesec".)
828848

829849
Default: ``{rgb}{0.126,0.263,0.361}``
830850

831-
.. warning::
832-
833-
Colours set via ``'sphinxsetup'`` must obey the syntax of the
834-
argument of the ``color/xcolor`` packages ``\definecolor`` command.
835-
836851
``InnerLinkColor``
837852
A colour passed to ``hyperref`` as value of ``linkcolor`` and
838853
``citecolor``.
@@ -862,10 +877,47 @@ Do not use quotes to enclose values, whether numerical or strings.
862877

863878
.. versionadded:: 1.6.6
864879

865-
.. note::
880+
.. _tablecolors:
881+
882+
``TableRowColorHeader``
883+
Sets the background colour for (all) the header rows of tables.
884+
885+
It will have an effect only if either the :confval:`latex_table_style`
886+
contains ``'colorrows'`` or if the table is assigned the ``colorrows``
887+
class. It is ignored for tables with ``nocolorrows`` class.
866888

867-
Starting with this colour, and for all others following, the
868-
names declared to "color" or "xcolor" are prefixed with "sphinx".
889+
As for the other ``'sphinxsetup'`` keys, it can also be set or modified
890+
from a ``\sphinxsetup{...}`` LaTeX command inserted via the :dudir:`raw`
891+
directive, or also from a LaTeX environment associated to a `container
892+
class <latexcontainer_>`_ and using such ``\sphinxsetup{...}``.
893+
894+
Default: ``{gray}{0.86}``
895+
896+
There is also ``TableMergeColorHeader``. If used, sets a specific colour
897+
for merged single-row cells in the header.
898+
899+
.. versionadded:: 5.2.0
900+
901+
``TableRowColorOdd``
902+
Sets the background colour for odd rows in tables (the row count starts at
903+
``1`` at the first non-header row). Has an effect only if the
904+
:confval:`latex_table_style` contains ``'colorrows'`` or for specific
905+
tables assigned the ``colorrows`` class.
906+
907+
Default: ``{gray}{0.92}``
908+
909+
There is also ``TableMergeColorOdd``.
910+
911+
.. versionadded:: 5.2.0
912+
913+
``TableRowColorEven``
914+
Sets the background colour for even rows in tables.
915+
916+
Default ``{gray}{0.98}``
917+
918+
There is also ``TableMergeColorEven``.
919+
920+
.. versionadded:: 5.2.0
869921

870922
``verbatimsep``
871923
The separation between code lines and the frame.
@@ -1425,6 +1477,8 @@ Miscellany
14251477
Formerly, use of *fncychap* with other styles than ``Bjarne`` was
14261478
dysfunctional.
14271479

1480+
.. _latexcontainer:
1481+
14281482
- Docutils :dudir:`container` directives are supported in LaTeX output: to
14291483
let a container class with name ``foo`` influence the final PDF via LaTeX,
14301484
it is only needed to define in the preamble an environment

doc/usage/configuration.rst

+95
Original file line numberDiff line numberDiff line change
@@ -2233,6 +2233,101 @@ These options influence LaTeX output.
22332233

22342234
.. versionadded:: 1.6
22352235

2236+
.. confval:: latex_table_style
2237+
2238+
A list of styling classes (strings). Currently supported:
2239+
2240+
- ``'booktabs'``: no vertical lines, and only 2 or 3 horizontal lines (the
2241+
latter if there is a header), using the booktabs_ package.
2242+
2243+
- ``'borderless'``: no lines whatsoever.
2244+
2245+
- ``'colorrows'``: the table rows are rendered with alternating background
2246+
colours. The interface to customize them is via :ref:`dedicated keys
2247+
<tablecolors>` of :ref:`latexsphinxsetup`.
2248+
2249+
.. important::
2250+
2251+
With the ``'colorrows'`` style, the ``\rowcolors`` LaTeX command
2252+
becomes a no-op (this command has limitations and has never correctly
2253+
supported all types of tables Sphinx produces in LaTeX). Please
2254+
update your project to use instead
2255+
the :ref:`latex table color configuration <tablecolors>` keys.
2256+
2257+
Default: ``[]``
2258+
2259+
.. versionadded:: 5.2.0
2260+
2261+
If using ``'booktabs'`` or ``'borderless'`` it seems recommended to also
2262+
opt for ``'colorrows'``...
2263+
2264+
Each table can override the global style via ``:class:`` option, or
2265+
``.. rst-class::`` for no-directive tables (cf. :ref:`table-directives`).
2266+
Currently recognized classes are ``booktabs``, ``borderless``,
2267+
``standard``, ``colorrows``, ``nocolorrows``. The latter two can be
2268+
combined with any of the first three. The ``standard`` class produces
2269+
tables with both horizontal and vertical lines (as has been the default so
2270+
far with Sphinx).
2271+
2272+
A single-row multi-column merged cell will obey the row colour, if it is
2273+
set. See also ``TableMergeColor{Header,Odd,Even}`` in the
2274+
:ref:`latexsphinxsetup` section.
2275+
2276+
.. note::
2277+
2278+
- It is hard-coded in LaTeX that a single cell will obey the row colour
2279+
even if there is a column colour set via ``\columncolor`` from a
2280+
column specification (see :rst:dir:`tabularcolumns`). Sphinx provides
2281+
``\sphinxnorowcolor`` which can be used like this:
2282+
2283+
.. code-block:: latex
2284+
2285+
>{\columncolor{blue}\sphinxnorowcolor}
2286+
2287+
in a table column specification.
2288+
2289+
- Sphinx also provides ``\sphinxcolorblend`` which however requires the
2290+
xcolor_ package. Here is an example:
2291+
2292+
.. code-block:: latex
2293+
2294+
>{\sphinxcolorblend{!95!red}}
2295+
2296+
It means that in this column, the row colours will be slightly tinted
2297+
by red; refer to xcolor_ documentation for more on the syntax of its
2298+
``\blendcolors`` command (a ``\blendcolors`` in place of
2299+
``\sphinxcolorblend`` would modify colours of the cell *contents*, not
2300+
of the cell *background colour panel*...). You can find an example of
2301+
usage in the :ref:`dev-deprecated-apis` section of this document in
2302+
PDF format.
2303+
2304+
.. hint::
2305+
2306+
If you want to use a special colour for the *contents* of the
2307+
cells of a given column use ``>{\noindent\color{<color>}}``,
2308+
possibly in addition to the above.
2309+
2310+
- Multi-row merged cells, whether single column or multi-column
2311+
currently ignore any set column, row, or cell colour.
2312+
2313+
- It is possible for a simple cell to set a custom colour via the
2314+
:dudir:`raw` directive and the ``\cellcolor`` LaTeX command used
2315+
anywhere in the cell contents. This currently is without effect
2316+
in a merged cell, whatever its kind.
2317+
2318+
.. hint::
2319+
2320+
In a document not using ``'booktabs'`` globally, it is possible to style
2321+
an individual table via the ``booktabs`` class, but it will be necessary
2322+
to add ``r'\usepackage{booktabs}'`` to the LaTeX preamble.
2323+
2324+
On the other hand one can use ``colorrows`` class for individual tables
2325+
with no extra package (as Sphinx since 5.2.0 always loads colortbl_).
2326+
2327+
.. _booktabs: https://ctan.org/pkg/booktabs
2328+
.. _colortbl: https://ctan.org/pkg/colortbl
2329+
.. _xcolor: https://ctan.org/pkg/xcolor
2330+
22362331
.. confval:: latex_use_xindy
22372332

22382333
If ``True``, the PDF build from the LaTeX files created by Sphinx

doc/usage/restructuredtext/basics.rst

+9-8
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,15 @@ Docutils supports the following directives:
370370
- :dudir:`include` (include reStructuredText from another file) -- in Sphinx,
371371
when given an absolute include file path, this directive takes it as
372372
relative to the source directory
373-
- :dudir:`class` (assign a class attribute to the next element) [1]_
373+
374+
.. _rstclass:
375+
376+
- :dudir:`class` (assign a class attribute to the next element)
377+
378+
.. note::
379+
380+
When the default domain contains a ``class`` directive, this directive
381+
will be shadowed. Therefore, Sphinx re-exports it as ``rst-class``.
374382

375383
* HTML specifics:
376384

@@ -621,10 +629,3 @@ There are some problems one commonly runs into while authoring reST documents:
621629

622630
* **No nested inline markup:** Something like ``*see :func:`foo`*`` is not
623631
possible.
624-
625-
626-
.. rubric:: Footnotes
627-
628-
.. [1] When the default domain contains a :rst:dir:`class` directive, this
629-
directive will be shadowed. Therefore, Sphinx re-exports it as
630-
:rst:dir:`rst-class`.

0 commit comments

Comments
 (0)