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

Add support for zebra-striped tables to LaTeX builder #6671

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions doc/latex.rst
Expand Up @@ -646,6 +646,18 @@ macros may be significant.

.. versionadded:: 1.6.6

``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

.. note::

Starting with this colour key, and for all others coming next, the actual
Expand Down
9 changes: 9 additions & 0 deletions doc/usage/configuration.rst
Expand Up @@ -2004,6 +2004,15 @@ These options influence LaTeX output.

.. versionadded:: 1.8

.. 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 @@ -458,6 +458,7 @@ def setup(app: Sphinx) -> Dict[str, Any]:
app.add_config_value('latex_appendices', [], None)
app.add_config_value('latex_use_latex_multicolumn', False, None)
app.add_config_value('latex_use_xindy', default_latex_use_xindy, 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 @@ -201,7 +201,7 @@
\RequirePackage{framed}
% The xcolor package draws better fcolorboxes around verbatim code
\IfFileExists{xcolor.sty}{
\RequirePackage{xcolor}
\RequirePackage[table]{xcolor}
}{
\RequirePackage{color}
}
Expand Down Expand Up @@ -376,6 +376,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 @@ -1178,6 +1178,14 @@ def visit_row(self, node):
# type: (nodes.Element) -> None
self.table.col = 0

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