An RTF file is text: plain characters, control words, control symbols and groups in braces. This page describes how those pieces are written and read, as the RTF Specification 1.6 (Microsoft, May 1999) defines them, and lists every control word that stands for a special character. The later pages list the control words for each part of a document.
The four kinds of element
| Element | Form | Meaning |
|---|---|---|
| Plain text | any character other than \, { and } | Text of the document, written to the current destination with the current formatting. |
| Control word | \ lower-case letters, optional number, delimiter | A command: a property change, a destination, or a special character. See control words. |
| Control symbol | \ and one character that is not a letter | A special character or marker, such as \~ for a non-breaking space. It takes no delimiter. |
| Group | { … } | Text and control words whose formatting stays inside the braces. See groups. |
The whole file is one group that starts {\rtf1. A standard RTF file can
be written with 7-bit ASCII only; Word for Windows and for the Macintosh also write
8-bit characters, so readers should accept them. There is no maximum line length. To use
a backslash or brace as text, write \\, \{ or
\}.
Control words
A control word is a backslash, a sequence of lower-case letters a to
z, and a delimiter. It is at most 32 characters long, and RTF is case
sensitive. The delimiter is one of:
| Delimiter | Effect |
|---|---|
| A space | Belongs to the control word and is not text. Any further spaces are text. |
| A digit or a hyphen | A numeric parameter follows, positive or negative, and becomes part of the control word. The number itself ends at a space (which is swallowed) or at any character that is not a letter or digit. |
| Any other character that is not a letter or digit | Ends the control word and is read as the next character of the file. |
Parameters are generally in the range −32,767 to 32,767; Word tends to keep them
within −31,680 to 31,680, but allows a full 32-bit range for a few words
(\bin, \revdttm and some picture properties). A reader must
accept any string of digits. A parameter that is needed but missing takes the word’s
default, or 0 when the word gives none.
Words that switch a property on and off, such as \b for bold, turn it on
with no parameter or a non-zero one and off with 0: \b starts bold and
\b0 ends it. On these pages such words are marked “toggle”.
Word 97 and 2000 broke the lower-case rule for a few table words. Writers should use
lower case, but readers should accept these exceptions: \clFitText,
\clftsWidthN, \clNoWrap, \clwWidthN,
\tdfrmtxtBottomN, \tdfrmtxtLeftN,
\tdfrmtxtRightN, \tdfrmtxtTopN,
\trftsWidthAN, \trftsWidthBN, \trftsWidthN,
\trwWidthAN, \trwWidthBN, \trwWidthN and
\sectspecifygenN.
Lengths are in twips, twentieths of a point (1,440 to the inch), unless a word says otherwise. Font sizes are in half-points.
Groups and destinations
A group is text and control information in braces. Formatting set inside a group
applies only inside it; at the closing brace the reader goes back to the state it had
at the opening brace. Text in a group otherwise inherits the formatting before it, but
Microsoft’s readers do not let footnote, annotation, header and footer groups inherit,
so writers should reset them with \sectd, \pard and
\plain.
A destination is a control word that starts a block of text that
belongs somewhere else, or is not shown at all: \footnote,
\header, \pict, \info,
\fonttbl, \stylesheet, \colortbl and many
more. A destination must come straight after an opening brace, and the group holds
nothing else. Page breaks cannot occur in destination text.
The control symbol \* before a destination word means “skip this whole
group if you do not know the destination”. Every destination added after the March
1987 specification is written with it, for example {\*\bkmkstart Name}.
A destination that should still be shown by a reader that does not know it is written
without \*. Every RTF reader must handle \*.
The groups that make up the header (font, file, colour, style, list and revision tables) and the document formatting properties must all come before the first character of text.
How a reader works
The specification describes a reader that takes the file one character at a time:
{: push the current state (destination and character, paragraph, section and table properties) on a stack.}: pop the state back off the stack.\: read the control word or symbol and its parameter, look it up and do what it says. An unknown control word is ignored; an unknown destination after\*is skipped up to its closing brace.- Anything else: write it as text to the current destination. Carriage returns and line feeds are ignored, unless they follow a backslash.
A known control word does one of four things:
| Action | What the reader does | Example |
|---|---|---|
| Change destination | Sends the text that follows elsewhere, often after resetting properties. Only legal straight after {. | \footnote, \fonttbl |
| Change formatting property | Changes a property of the current group state. | \b, \qc |
| Insert special character | Puts a character in the text. | \tab, \emdash |
| Insert special character and act | Puts a character in and does something more. | \par ends a paragraph and records its properties |
Formal syntax notation
The specification describes the grammar of each part in a form of Backus-Naur notation, which these pages repeat where it helps:
| Notation | Meaning |
|---|---|
#PCDATA | Text, without control words. |
#SDATA | Hexadecimal data. |
#BDATA | Binary data. |
'c' | A literal character. |
<text> | A non-terminal, defined elsewhere. |
\a, \aN | The control word a, without or with a parameter. |
a? | Optional. |
a+ | One or more. |
a* | Zero or more. |
a b | a followed by b. |
a | b | a or b. |
a & b | a and/or b, in any order. |
The top level is <File> = '{' <header> <document> '}'.
Characters outside ASCII and binary data
| Control word | Meaning |
|---|---|
\'hh | One byte, given as two hexadecimal digits, in the document’s character set (or the current font’s, see \fcharset and \cpg). Used for 8-bit characters, and in pairs for double-byte characters. |
\uN | One Unicode character, N in decimal. Values above 32,767 are written as negative numbers (subtract 65,536). It is followed by a fallback in the document’s code page, which a Unicode reader skips. |
\ucN | The number of fallback characters that follow each \u (default 1). It is scoped like a character property: leaving the group restores the previous value. For skipping, a control word or symbol counts as one character, a \bin word and its data count as one, a keyword-ending space does not count, and a brace ends the skip early. |
\upr | A destination holding two versions of the same destination: first one in ANSI, then one in Unicode inside {\*\ud …}. It is written without \* so that old readers take the ANSI copy. Syntax: {\upr{keyword ansi_text}{\*\ud{keyword Unicode_text}}}. |
\ud | The Unicode copy inside \upr, written with \u escapes and usually \uc0. |
\binN | N bytes of raw binary data follow the space after the word. The reader takes them as data without looking for braces or backslashes. N can be a full 32-bit number. |
Example: the text LabΓValue is written Lab\u915Gvalue when
\uc1 is in force: Γ is U+0393, 915 in decimal, and G is
the one-byte fallback.
Special characters
These control words and symbols stand for a character or a break. A reader that does not know one ignores it and reads what follows as text.
| Control word | Meaning |
|---|---|
\chdate | Current date (as in headers). |
\chdpl | Current date in long format, such as Thursday, October 28, 1997. |
\chdpa | Current date in abbreviated format, such as Thu, Oct 28, 1997. |
\chtime | Current time (as in headers). |
\chpgn | Current page number (as in headers). |
\sectnum | Current section number (as in headers). |
\chftn | Automatic footnote reference; the footnote follows in a group. |
\chatn | Annotation (comment) reference; the annotation follows in a group. |
\chftnsep | Anchoring character for the footnote separator. |
\chftnsepc | Anchoring character for the footnote continuation separator. |
\cell | End of a table cell. |
\nestcell | End of a nested table cell. |
\row | End of a table row. |
\nestrow | End of a nested table row. |
\par | End of a paragraph. |
\sect | End of a section and of its paragraph. |
\page | Required page break. |
\column | Required column break. |
\line | Required line break, without a new paragraph. |
\lbrN | Text-wrapping break: 0 default line break (like \line), 1 clear left, 2 clear right, 3 clear all. Writers also emit \line for old readers. |
\softpage | Non-required page break, as shown in Galley view. |
\softcol | Non-required column break, as shown in Galley view. |
\softline | Non-required line break, as shown in Galley view. |
\softlheightN | Non-required line height, written before each line. |
\tab | Tab character. |
\emdash | Em dash (—). |
\endash | En dash (–). |
\emspace | Non-breaking space as wide as “m” in the current font. Old writers use {\emspace } with two spaces; read it as an em space and a normal space. |
\enspace | Non-breaking space as wide as “n” in the current font; the same two-space construct occurs. |
\qmspace | Quarter-em space. |
\bullet | Bullet character (•). |
\lquote | Left single quotation mark (‘). |
\rquote | Right single quotation mark (’). |
\ldblquote | Left double quotation mark (“). |
\rdblquote | Right double quotation mark (”). |
\| | Formula character, used by Word 5.1 for the Macintosh to start a string of formula typesetting commands. |
\~ | Non-breaking space. |
\- | Optional hyphen. |
\_ | Non-breaking hyphen. |
\: | Marks a sub-entry in an index entry. |
\* | Marks a destination to be skipped if the reader does not know it. |
\'hh | A byte in hexadecimal, in the current character set. |
\ltrmark | The characters that follow are shown left to right; usually at the start of \ltrch runs. |
\rtlmark | The characters that follow are shown right to left; usually at the start of \rtlch runs. |
\zwbo | Zero-width break opportunity: a line may break here. |
\zwnbo | Zero-width non-break opportunity: a line may not break here. |
\zwj | Zero-width joiner, for joining characters into a ligature. |
\zwnj | Zero-width non-joiner, to stop characters joining. |
A carriage return (13) or line feed (10) after a backslash means \par; on
its own it is ignored, so writers can break lines anywhere, and the specification
suggests a line break at least every 255 characters. A tab character (9) should be read
as \tab, but writers should always write the control word.
The quotation marks, dashes and bullet have these byte values in the two character sets that Word uses:
| Control word | Windows and OS/2 (decimal) | Macintosh |
|---|---|---|
\bullet | 149 | 0xA5 |
\endash | 150 | 0xD1 |
\emdash | 151 | 0xD0 |
\lquote | 145 | 0xD4 |
\rquote | 146 | 0xD5 |
\ldblquote | 147 | 0xD2 |
\rdblquote | 148 | 0xD3 |
Example
A complete document whose only text is “This is plain text.”, after the example in the specification (shortened colour table; lines broken only where a line break means nothing):
{\rtf1\ansi\deff0
{\fonttbl{\f0\froman Tms Rmn;}{\f1\fdecor Symbol;}{\f2\fswiss Helv;}}
{\colortbl;\red0\green0\blue0;\red0\green0\blue255;}
{\stylesheet{\fs20 \snext0 Normal;}}
{\info{\author John Doe}{\creatim\yr1990\mo7\dy30\hr10\min48}
{\version1}{\edmins0}{\nofpages1}{\nofwords0}{\nofchars0}{\vern8351}}
\widowctrl\ftnbj \sectd\linex0\endnhere \pard\plain \fs20 This is plain text.\par
}
Read in order: the version and character set (\rtf1,
\ansi), the default font (\deff0), the
font table, colour table and style
sheet, the information group,
document properties (\widowctrl, \ftnbj),
section properties
(\sectd\linex0\endnhere), the paragraph and character resets
(\pard\plain), a font size of 10 points (\fs20), the text and
the paragraph mark. The space after \fs20 ends the control word and is not
part of the text.