Skip to content

Commit

Permalink
Cherry-pick: Add support for zebra-striped tables to LaTeX builder
Browse files Browse the repository at this point in the history
Render tables with alternating background colors for even and odd rows
(so called "zebra striping").
  • Loading branch information
Stefan Wiehler authored and jfbu committed Aug 8, 2022
1 parent a3498c6 commit a1745d4
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 1 deletion.
12 changes: 12 additions & 0 deletions doc/latex.rst
Expand Up @@ -863,6 +863,18 @@ Do not use quotes to enclose values, whether numerical or strings.
Starting with this colour, and for all others following, the
names declared to "color" or "xcolor" are prefixed with "sphinx".

``RowEvenColor``
default ``{rgb}{0.85,0.85,0.85}``. Background color for even rows in
tables, if option :confval:`latex_zebra_stripes` is set to ``True``.

.. versionadded:: 2.1

``RowOddColor``
default ``{rgb}{1,1,1}``. Background color for odd rows in tables, if
option :confval:`latex_zebra_stripes` is set to ``True``.

.. versionadded:: 2.1

``verbatimsep``
The separation between code lines and the frame.

Expand Down
9 changes: 9 additions & 0 deletions doc/usage/configuration.rst
Expand Up @@ -2251,6 +2251,15 @@ These options influence LaTeX output.

.. versionadded:: 5.2.0

.. confval:: latex_zebra_stripes

If ``True``, render tables with alternating background colors for even and
odd rows (so called "zebra striping"). See :ref:`latexsphinxsetup`
``RowEvenColor`` and ``RowOddColor`` for changing the default white-grey
color scheme.

.. versionadded:: 2.1

.. confval:: latex_elements

.. versionadded:: 0.5
Expand Down
1 change: 1 addition & 0 deletions sphinx/builders/latex/__init__.py
Expand Up @@ -519,6 +519,7 @@ def setup(app: Sphinx) -> Dict[str, Any]:
app.add_config_value('latex_use_latex_multicolumn', False, None)
app.add_config_value('latex_use_xindy', default_latex_use_xindy, None, [bool])
app.add_config_value('latex_use_booktabs', False, None)
app.add_config_value('latex_zebra_stripes', False, None)
app.add_config_value('latex_toplevel_sectioning', None, None,
ENUM(None, 'part', 'chapter', 'section'))
app.add_config_value('latex_domain_indices', True, None, [list])
Expand Down
4 changes: 3 additions & 1 deletion sphinx/texinputs/sphinx.sty
Expand Up @@ -43,7 +43,7 @@
% checked at 5.0.0) \fcolorbox is used for admonitions (sphinxheavybox)
% and appears also in Pygmentize output mark-up.
\IfFileExists{xcolor.sty}{
\RequirePackage{xcolor}
\RequirePackage[table]{xcolor}
}{
\RequirePackage{color}
}
Expand Down Expand Up @@ -153,6 +153,8 @@
\sphinxDeclareColorOption{OuterLinkColor}{{rgb}{0.216,0.439,0.388}}
\sphinxDeclareColorOption{VerbatimColor}{{rgb}{1,1,1}}
\sphinxDeclareColorOption{VerbatimBorderColor}{{rgb}{0,0,0}}
\sphinxDeclareColorOption{RowEvenColor}{{rgb}{0.85,0.85,0.85}}
\sphinxDeclareColorOption{RowOddColor}{{rgb}{1,1,1}}
% now the colours defined with "sphinx" prefix in their names
\newcommand*{\sphinxDeclareSphinxColorOption}[2]{%
% set the initial default
Expand Down
8 changes: 8 additions & 0 deletions sphinx/writers/latex.py
Expand Up @@ -926,6 +926,14 @@ def visit_row(self, node: Element) -> None:
self.table.col = 0
_colsep = '' if self.table.booktabs else '|'

if self.builder.config.latex_zebra_stripes and not isinstance(node.parent, nodes.thead):
if self.table.row % 2 == 0:
self.body.append(
'\\rowcolor{RowEvenColor}[\\dimexpr\\tabcolsep+0.1pt\\relax]')
else:
self.body.append(
'\\rowcolor{RowOddColor}[\\dimexpr\\tabcolsep+0.1pt\\relax]')

# fill columns if the row starts with the bottom of multirow cell
while True:
cell = self.table.cell(self.table.row, self.table.col)
Expand Down

0 comments on commit a1745d4

Please sign in to comment.