Text in the colour of you choosing

Last-Updated: 2021-08-30

Sources

Using groff macros

For PostScript and PDF outputs, groff uses \m as an escape sequence to print coloured text. The argument to \m can be any PostScript colour (cf. grops(1)). You can also define any colour you need with the .defcolor macro (cf. groff(7)).

This is normal text. \m[salmon]This is salmon.\m[] Back to normal.

Some macro sets include macros for coloured output. For example, the MOM macro set includes .XCOLOR, .COLOR, .NEWCOLOR. There is also \*[] for inline coloured text (momdoc/color). You can also create coloured links with the pdfmark macros.

Using PostScript requests

This solution (2000-06/msg00036) by Ted Harding only work if the output device is PostScript.

In the following example we use \X to output PostScript code and set gray values. Note that in PostScript gray = 0 means black and gray = 1 means white.

\X'ps: exec 0.7 setgray'GREY
\X'ps: exec 0 setgray'BLACK
\X'ps: exec 0.4 setgray'DARK GREY

You could define strings or macros for this, e.g.

.de grch
.ds ngr \\$1
.shift
\X'ps: exec \\*[ngr] setgray'\\$*
..
.grch 0.7 This is gray
.grch 0 This is black
.grch 0.4 This is dark gray

This also works for coloured text, e.g.

.ds RED "\X'ps: exec 1 0 0 setrgbcolor'
.ds BLACK "\X'ps: exec 0 0 0 setrgbcolor'
This is black
\*[RED]This is red
\*[BLACK]This is black again

This technique can be useful to write text on a coloured background. Here is an example of white writing on a gray background.

.nr bwd \w'\0WHITE ON GRAY\0'u
.ce
\D'f 400u'\h'-400u'\
\Z'\v'0.5m'\D'P 0 -1.7m \n[bwd]u 0 0 1.7m -\n[bwd]u 0'\v'-0.5m''\
\X'ps: exec 1 setgray'\0WHITE ON GRAY\0