Gray background on alternate rows

Last-Updated: 2021-12-12

Sources

Introduction

Large or unusual tables can be hard to read. Adding a gray background on alternate rows can help mitigate this issue. Implementations of this feature can be found in HDTBL or can be crafted with a custom macro.

Using HDTBL

The HDTBL macro package provides many options and two of them are of interest here, fgc and bgc. As specified by the groff_hdtbl(7) man page these options control the background and foreground colour of the table cells.

For the background colour of the table cells, use 'bgc=[c]'. This includes the area specified with the ‘csp’ argument. The argument ‘bgc=’ (no value) suppresses a background colour; this makes the background transparent.

Default: ‘bgc=bisque’ (string ‘t*bgc’).

For the foreground colour of the cell contents, use 'fgc=c'

Default: ‘fgc=red4’ (string ‘t*fgc’).

A custom macro by Tadziu Hoffmann

One can also use the tbl(1) preprocessor with a custom macro. Tadziu Hoffmann did just that and, after some back and forth with Blake McBride who proposed some corrections (2021-11/msg00103), shared it (2021-11/msg00110).

.\" tbl mm
.\" ----------------------------------------------------------------
.de TP
'SP 1i
..
.\" ----------------------------------------------------------------
.nr TW 0
.nr F 0
.defcolor tblfill1 rgb .9 1 .8
.defcolor tblfill2 rgb 1 .9 .8
.ds X \\?\R'F 1'\\?
.ds Y \\?\R'F 1'\\*(F2\\?
.ds Z \\?\R'F 1-\\\\nF'\\\\*(F\\\\nF\\?
.ds F0
.ds F1 \Z'\v'0.3v'\M[tblfill1]\D'P 0 -1v \\n(TWu 0 0 1v''
.ds F2 \Z'\v'0.3v'\M[tblfill2]\D'P 0 -1v \\n(TWu 0 0 1v''
.\" ----------------------------------------------------------------
.TS H
tab(~);
L1 Lb Lb Lb 1L
L  Lb Lb Lb  L
L  N  N  N   L.
\*Y~AAAAA~BBBBB~CCCCC
\*Y~DDDD~EEEE~FFFF
_
.TH
\*Z~1~2~3
\*Z~4~5~6
\*Z~7~8~9
\*Z~11~22~33
\*Z~44~55~66
\*Z~77~88~99
\*Z~111~222~333
\*Z~444~555~666
\*Z~777~888~999
\*Z~1~2~3
\*Z~4~5~6
\*Z~7~8~9
\*Z~11~22~33
\*Z~44~55~66
\*Z~77~88~99
\*Z~111~222~333
\*Z~444~555~666
\*Z~777~888~999
.TE
← Go back