alternating background table color row

468 views
Skip to first unread message

Michael P. Soulier

unread,
Aug 2, 2011, 8:25:15 AM8/2/11
to sphin...@googlegroups.com
Hi,

I want to style my html and latex output such that each table header has a
particular background color, and each row alternates between two colors.

This is built-in in rst2pdf, but beyond hacking the output I'm not sure how to
do this in Sphinx.

Is this possible?

Thanks,
mps

Michael P. Soulier

unread,
Aug 2, 2011, 8:44:20 AM8/2/11
to sphin...@googlegroups.com

This simple patch to the latex writer shows how it might be done.

msoulier@anton:...es/sphinx/writers$ diff -u latex.py.orig latex.py
--- latex.py.orig 2011-08-02 08:38:24.000000000 -0400
+++ latex.py 2011-08-02 08:42:12.000000000 -0400
@@ -48,6 +48,11 @@
\newcommand{\sphinxlogo}{%(logo)s}
\renewcommand{\releasename}{%(releasename)s}
%(makeindex)s
+\usepackage{color}
+\usepackage{colortbl}
+\definecolor{tableheader}{rgb}{0,0,1}
+\definecolor{row1}{rgb}{0.7,0.7,0.7}
+\definecolor{row2}{rgb}{0.9,0.9,0.9}
'''

BEGIN_DOC = r'''
@@ -712,6 +717,10 @@
self.body.append('\\hline\n')

def visit_row(self, node):
+ if self.table.rowcount % 2 == 0:
+ self.body.append('\\rowcolor{row1}\n')
+ else:
+ self.body.append('\\rowcolor{row2}\n')
self.table.col = 0
def depart_row(self, node):
self.body.append('\\\\\n')

mps

Michael P. Soulier

unread,
Aug 2, 2011, 9:02:46 AM8/2/11
to sphin...@googlegroups.com

And this patch does it with hardcoded header, and row colors, with a custom
text color in the header.

msoulier@anton:...es/sphinx/writers$ diff -u latex.py.orig latex.py
--- latex.py.orig 2011-08-02 08:38:24.000000000 -0400

+++ latex.py 2011-08-02 08:56:21.000000000 -0400


@@ -48,6 +48,11 @@
\newcommand{\sphinxlogo}{%(logo)s}
\renewcommand{\releasename}{%(releasename)s}
%(makeindex)s
+\usepackage{color}
+\usepackage{colortbl}
+\definecolor{tableheader}{rgb}{0,0,1}
+\definecolor{row1}{rgb}{0.7,0.7,0.7}
+\definecolor{row2}{rgb}{0.9,0.9,0.9}
'''

BEGIN_DOC = r'''

@@ -712,6 +717,12 @@


self.body.append('\\hline\n')

def visit_row(self, node):

+ if self.table.rowcount == 0:
+ self.body.append('\\rowcolor{tableheader}\n')
+ elif self.table.rowcount % 2 == 0:


+ self.body.append('\\rowcolor{row1}\n')
+ else:
+ self.body.append('\\rowcolor{row2}\n')
self.table.col = 0
def depart_row(self, node):
self.body.append('\\\\\n')

@@ -726,8 +737,8 @@
self.body.append(' & ')
self.table.col += 1
if isinstance(node.parent.parent, nodes.thead):
- self.body.append('\\textbf{')
- self.context.append('}')
+ self.body.append('\\textcolor{white}{\\textbf{')
+ self.context.append('}}')
else:
self.context.append('')
def depart_entry(self, node):


I don't want to maintain a patched latex writer though, so is there a chance
of putting such hooks into Sphinx?

Thanks,
mps

sffjunkie

unread,
Aug 11, 2011, 5:29:17 AM8/11/11
to sphinx-dev
On Aug 2, 1:25 pm, "Michael P. Soulier" <msoul...@digitaltorque.ca>
wrote:
> Hi,
>
> I want to style my html and latex output such that each table header has a
> particular background color, and each row alternates between two colors.

For the HTML case you can use the CSS3 nth-child selector, if your
browser supports it, to get the row colouring.

<html>
<head>
<style>
table {
border: 1px solid black;
}
th {
background-color: #f4c;
}
tr:nth-child(odd) {
background-color: #4cf;
}
tr:nth-child(even) {
background-color: #4fc;
}
td {
padding: 0;
}
</style>
</head>
<body>
<table>
<tr><th>Number</th> <th>Value</th></tr>
<tr><td>Row 1</td> <td>A</td></tr>
<tr><td>Row 2</td> <td>B</td></tr>
<tr><td>Row 3</td> <td>C</td></tr>
<tr><td>Row 4</td> <td>D</td></tr>
<tr><td>Row 5</td> <td>E</td></tr>
</table>
</body>
</html>

Regards
Simon
Reply all
Reply to author
Forward
0 new messages