Simple tutorial for programmers — part 2

RTF – Fonts, Color & Tabs

Building on the basics: multiple fonts, color highlighting, tab justification, and tab leaders

This page builds on the first example. Fonts are straightforward — define them all in the font table at the top of the document and reference them by number as needed.

Multiple fonts

The font table accepts as many entries as you need. Each font is given a sequential number starting at f0. Switch fonts mid-document by writing \fN:

RTF {\rtf1\ansi\deff0 {\fonttbl {\f0 Courier;}{\f1 ProFontWindows;}} This line is font 0 which is Courier\line \f1 This line is font 1\line \f0 This line is font 0 again\line }
This line is font 0 which is Courier
This line is font 1
This line is font 0 again

Text color and highlighting

The color table can hold as many colors as needed. Colors are indexed from 1 (the first entry after the opening semicolon). Switch the foreground color with \cfN and apply a highlight with \highlightN — turn the highlight off with \highlight0.

The example below adds yellow (color index 3) to the palette from part 1:

RTF {\rtf1\ansi\deff0 {\fonttbl {\f0 Courier;}{\f1 ProFontWindows;}} {\colortbl;\red0\green0\blue0;\red255\green0\blue0;\red255\green255\blue0;} This line is font 0 which is Courier\line \f1 This line is font 1\line \f0 This line is font 0 again\line This line has a \cf2 red \cf1 word\line \highlight3 while this line has a \cf2 red \cf1 word and is highlighted in yellow\highlight0\line Finally, back to the default color.\line }
This line is font 0 which is Courier
This line is font 1
This line is font 0 again
This line has a red word
while this line has a red word and is highlighted in yellow
Finally, back to the default color.

Tab justification

Tab stops can be left-, center-, or right-aligned. Each line is wrapped in its own paragraph (\par\pard) so tab settings reset cleanly. Place the tab alignment command before the \txN stop:

RTF {\rtf1\ansi\deff0 {\fonttbl {\f0 Courier;}} \tqr\tx4320\tab Right tab\par\pard \tqc\tx4320\tab Center tab\par\pard \tx4320\tab Left tab }
right → Right tab
center → Center tab
left → Left tab

Tab leaders

Leader characters fill the space between the start of a line (or the previous tab stop) and the tab stop itself. They are commonly used in tables of contents. Combine a leader code with a tab alignment and position:

RTF {\rtf1\ansi\deff0 {\fonttbl {\f0 Courier;}} \fs28 \tqr\tldot\tx7200\tab Right tab with leading dots\par\pard \tqr\tlhyph\tx7200\tab Right tab with leading hyphens\par\pard \tqr\tlul\tx7200\tab Right tab with leading underline\par\pard \fs18 \tqr\tx1440\tab 1" tab\tldot\tx2880\tab with 1" of dots between\par\pard \tqr\tlhyph\tx1440\tab 1" tab\tx2880\tab with 1" of space between and hyphens outside\tqr\tlhyph\tx8640\tab\par\pard }
. . . . . . . . . . . . . . . . . . . . . . Right tab with leading dots
- - - - - - - - - - - - - - - - - - - - - - Right tab with leading hyphens
                             Right tab with leading underline
1" tab . . . . . . . . . . . with 1" of dots between
- - - - 1" tab      with 1" of space between and hyphens outside - - - - - - - -
Tip: Leader characters fill the space before the tab stop. To get leaders between two stops (not from the left margin), place a plain tab stop first, then a second tab stop with the leader code.

Navigation

Part 1 – RTF basics: color, fonts, layout, and formatting Rich Text Format (RTF) Version 1.5 Specification