[texhax] Coloured text with plain TeX

Hartmut Henkel hartmut_henkel at gmx.de
Sun Feb 29 14:22:38 CET 2004


On Sun, 29 Feb 2004, Rodolfo Medina wrote:

> How is it possible to get multi coloured text in the output file,
> using plain TeX, in text mode and also in math mode?

Possible on a letter-by-letter basis in pdftex by tweaking a virtual
font. Sketchy example for making a randomly colored cmr10 clone:

Start from cmr10.tfm, put this into some working directory. Then:

$ tftopl cmr10.tfm > cmr10.pl
$ cp cmr10.pl cmrcolor10.vpl

Now in the file cmrcolor10.vpl change

  (FAMILY CMR)
into
  (FAMILY CMRCOLOR)

and just before the 1st line

  (CHARACTER O 0

in cmrcolor10.vpl add a line

(MAPFONT D 0 (FONTNAME cmr10))

so that the cmrcolor10.vpl references cmr10.tfm as real font.

Then do

$ vptovf cmrcolor10.vpl

which creates
  cmrcolor10.tfm
  cmrcolor10.vf

Then do

$vftovp cmrcolor10.vf > cmrcolor10.vpl

which re-creates
   cmrcolor10.vpl

Now you have reached the first goal: A cmrcolor10.vpl file ready for
your tweaking. Then you can add funny pdfTeX specials e. g. by some awk
script:

$ export LANG="C" ; awk -f addspecial.awk cmrcolor10.vpl > temp
$ mv temp cmrcolor10.vpl

where the addspecial.awk looks like:

#begin of addspecial.awk
/SETCHAR/{
  r = 0.6 * rand();
  g = 0.6 * rand();
  b = 0.6 * rand();
  printf("      (SPECIAL PDF: %f %f %f rg)\n", r, g, b);
  print $0;
  printf("      (SPECIAL PDF: %f %f %f rg)\n", 1, 1, 1);
  next;
}
//{print}
#end of addspecial.awk

Then do again

$ vptovf cmrcolor10.vpl

This creates the working versions
  cmrcolor10.tfm
  cmrcolor10.vf

Now you can call the colored font e. g. from the following tex file:

\font\cmrcolor cmrcolor10 at 10pt
\cmrcolor
\input tufte
\bye

Don't look into the pdf file: This method is rather "expensive" :-)

Greetings, Hartmut


More information about the texhax mailing list