Why should you care about RTF? Because it provides an easy way to create files that look like Word documents: ASCII files that look and act just like fancy-formatted Word files.
RTF solves several problems: how do you pass Word files between Macs and PCs? Easy: just "Save As" an RTF file and pass that RTF file. How do you format output from a database into a Word document? Easy: write it as an RTF file.
RTF provides codes that will let you do anything you can do in Word and a lot of things you can't do in HTML: tables, hanging indents, font changes, margins, tabs, page dimensions...
All of the RTF codes begin with a backslash. For example, \b means "bold"; \b0 means "turn off bold". You can also group elements with curly brackets, so that this:
I only want {\b a few words} to be bold.
Here are a few other codes:
RTF code | meaning |
---|---|
\i | Italic |
\u | Underline |
\par | End paragraph (carriage return) |
\qj | Justify text |
\ql | Flush left |
\qr | Flush right |
\qc | Center text |
Italic and underline work the same way that bold does: you can turn them off by adding zero to the tag: (\i0 and \u0) or by enclosing the affected words in curly brackets:
/qc This is plain text. /b This is bold.\par \par {/i This is bold italic and /u underlined.}/u0/par /b0yields this:
This is bold italic and underlined.
The next obvious question is: how do you select fonts? To answer that, we first have to talk about the overall look of an RTF file.
Just as every good HTML page begins with an tag and ends with an tag, every RTF file begins with a curly bracket and ends with a curly bracket. The RTF file also has a header and a body, just like an HTML page. The body part is easy: that's your text. The header can be complicated, but you can get away with a minimum of tags. Your RTF files need to start off with these tags:
\rtf1\ansi\deff0
Okay, so where do the fonts get defined? The next tag, which creates the font table:
{\fonttbl{\f0\froman Times New Roman;}{\f1\fswiss Arial;}{f2\ftech Symbol;} {\f3\froman Palatino;}}Note that the whole font table is enclosed in curly brackets. Each font definition is also enclosed in curly brackets and has three elements: the font number (\fx), the font family (\ffamily_name), and the name of the font, followed by a semicolon.
That ends the header, in our miminal, easy approach. There are other tags that can go in the font table; there are other tags that can go into the header, but let's leave them aside for now.
The way you call the fonts is by number:
\f0 This would be Times Roman.\par \f3 And this would be Palatino.\par Still Palatino, then \f2 crazy symbols.\parSo let's put it all together in a sample file:
{\rtf1\ansi\deff0{\fonttbl{\f0\froman Times New Roman;}{\f1\froman Palatino;}} \f1\fs20 this is just plain text. \par \par \fs28{\b Nice activity} \par \fs16 Some little explanation \par Mon, Wed, Jan 12, 14, 16, 2-4 pm. We will all meet downtown and then go to the park. \par \fs20 \par The weather is supposed to be good, but if it rains we will take the subway back and forth until the sun comes out or goes down, whichever happens first.\par \par \par \fs28{\b Header with {\i emphasized} word} \par \fs16 Another little explanation \par Thu, Jan 8, 2-4 pm. \par If you are crazy enough to go the first activity, come to this one. Bring your wallet.\par \par \fs20 Shares in various public buildings will be auctioned off. High bidders welcome.\par \par }Note the curly brackets enclosing the whole. You can copy the above text into a file; call it sample.rtf and try opening it with Word to see how it looks. Play with the tags to see how it changes. Remember that if you want to edit the tags, you need to use a text editor, not Word.
You can create RTF documents on the web: you can make CGI programs that write RTF files. If the file's extension ends in ".rtf" (for example, sample.rtf the browser will prompt you to save the file to your hard disk if you click on it.
To do more exotic stuff, like tables, lists with bullets and so on, you will need to learn more RTF. Get the RTF Specification; you will have to read the parts that interest you. Another way to learn more is to take a Word document, save it as an RTF file, and use a text editor to look at the tags. Just keep in mind that Word will insert a truckload of tags; it might take some study to find the ones you want.
Don't expect to learn all of RTF at once. There is enough on this page to keep you busy and do quite a bit of formatting. You may have to go through the Specification a number of times before it makes any sense at all.
Please feel free to send me any feedback or suggestions about this document. (I didn't write the specification.)
Kevin Kelleher