Easing Into RTF

      Microsoft explains that "Rich Text Format (RTF) is a method of encoding formatted text and graphics for easy transfer between applications." More specifically, it's a language like HTML that gets interpreted by various programs. One of these programs is Word, and in what follows I'm only going to speak about Word.

      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.

      will yield this:

      I only want a few words to be bold.

      Note that there is a space after the \b tag: you have to end the tag with a space, a line ending, or another tag.

      Here are a few other codes:

      RTF codemeaning
      \iItalic
      \uUnderline
      \parEnd paragraph (carriage return)
      \qjJustify text
      \qlFlush left
      \qrFlush right
      \qcCenter 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
          /b0
      
      yields this:

      This is plain text. This is bold.

      This is bold italic and underlined.

      You handle size changes with \fsxx, where xx is the font size in half-points. So, \fs20 is size 10, \fs28 is size 14, etc.

      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

      Now, you don't really need to understand what this means, but anyway: The first tag says this is using the current RTF format, which is version 1. The second says that everything in this file is using the ANSI character set, which is what you want. The third one says that the default font is font zero. If you try to use a font that turns out to be unavailable, Word will use font zero instead. For example, if you specify that font 0 is Times Roman and font 3 is Palatino, if Word can't find Palatino it will substitute Times Roman.

      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.\par
      
      So 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

      Software References Kevinations home page