The general syntax for writing groff documents is relatively easy, but
writing extensions to the roff language can be a bit harder.

The roff language is line-oriented.
There are only two kinds of lines, control lines and text lines.
The control lines start with a control character, by default a period

Text

"
"

or a single quote

Text

"
"

all other lines are text lines.

Control lines

represent commands, optionally with arguments.
They have the following syntax.
The leading control character can be followed by a command name;
arguments, if any, are separated by blanks from the command name and
among themselves, for example,

For indentation, any number of space or tab characters can be inserted
between the leading control character and the command name, but the
control character must be on the first position of the line.

Text lines

represent the parts that will be printed.
They can be modified by escape sequences, which are recognized by a
leading backslash

Text

"
"

These are in-line or even in-word formatting elements or functions.
Some of these take arguments separated by single quotes

Text

"
"

others are regulated by a length encoding introduced by an open
parenthesis

Text

"
"

or enclosed in brackets

Text

"
"

and

Text

"
"

The roff language provides flexible instruments for writing language
extension, such as macros.
When interpreting macro definitions, the roff system enters a special
operating mode, called thecopy mode.

The copy mode behavior can be quite tricky, but there are some rules
that ensure a safe usage.

1.

Printable backslashes must be denoted as

e.

To be more precise,

e

represents the current escape character.
To get a backslash glyph, use

(rs

or

[rs].

2.

Double all backslashes.

3.

Begin all text lines with the special non-spacing character

&.

This does not produce the most efficient code, but it should work as a
first measure.
For better strategies, see the groff info file andgroff_tmac(5).

Reading roff source files is easier, just reduce all double backslashes
to a single one in all macro definitions.

GROFF ELEMENTS

The roff language elements add formatting information to a text file.
The fundamental elements are predefined commands and variables that
make roff a full-blown programming language.

There are two kinds of roff commands, possibly with arguments.Requests

are written on a line of their own starting with a dot

Text

"
"

or a

Text

"
"

whereasEscape sequences

are in-line functions and in-word formatting elements starting with a
backslash

Text

"
"

The user can define her own formatting commands using the

de

request.
These commands are calledmacros,

but they are used exactly like requests.
Macro packages are pre-defined sets of macros written in the groff
language.
A user’s possibilities to create escape sequences herself is very
limited, only special characters can be mapped.

The groff language provides several kinds of variables with
different interfaces.
There are pre-defined variables, but the user can define her own
variables as well.

String

variables store character sequences.
They are set with the

ds

request and retrieved by the

*

escape sequences.
Strings can have variables.

Register

variables can store numerical values, numbers with a scale unit, and
occasionally string-like objects.
They are set with the

nr

request and retrieved by the

n

escape sequences.

Environments

allow the user to temporarily store global formatting parameters like
line length, font size, etc. for later reuse.
This is done by the

ev

request.

Fonts

are identified either by a name or by an internal number.
The current font is chosen by the

ft

request or by the

f

escape sequences.
Each device has special fonts, but the following fonts are available
for all devices.R

is the standard font Roman.B

is itsbold

counterpart.
Theitalic

font is calledI

and is available everywhere, but on text devices it is displayed as an
underlined Roman font.
For the graphical output devices, there exist constant-width pendants
of these fonts,CR,

CI,

andCB.

On text devices, all characters have a constant width anyway.

Moreover, there are some advanced roff elements.
Adiversion

stores information into a macro for later usage.
Atrap

is a positional condition like a certain number of lines from page top
or in a diversion or in the input.
Some action can be prescribed to be run automatically when the
condition is met.

More detailed information and examples can be found in the groff info
file.

CONTROL CHARACTERS

There is a small set of characters that have a special controlling
task in certain conditions.

Text

"
"

A dot is only special at the beginning of a line or after the
condition in the requests

if ,

ie ,

el ,

and

while .

There it is the control character that introduces a request (or macro).
The special behavior can be delayed by using the

.

escape.
By using the

cc

request, the control character can be set to a different character,
making the dot

Text

"
"

a non-special character.

In all other positions, it just means a dot character.
In text paragraphs, it is advantageous to start each sentence at a
line of its own.

Text

"
"

The single quote has two controlling tasks.
At the beginning of a line and in the conditional requests it is the
non-breaking control character.
That means that it introduces a request like the dot, but with the
additional property that this request doesn’t cause a linebreak.
By using the

c2

request, the non-break control character can be set to a different
character.

As a second task, it is the most commonly used argument separator in
some functional escape sequences (but any pair of characters not part
of the argument will work).
In all other positions, it denotes the single quote or apostrophe
character.
Groff provides a printable representation with the

(cq

escape sequence.

Text

"
"

The double quote is used to enclose arguments in requests, macros, and
strings.
In the

ds

and

as

requests, a leading double quote in the argument will be stripped off,
making everything else afterwards the string to be defined (enabling
leading whitespace).
The escaped double quote

"

introduces a comment.
Otherwise, it is not special.
Groff provides a printable representation with the

(dq

escape sequence.

Text

"
"

The backslash usually introduces an escape sequence (this can be
changed with the

ec

request).
A printed version of the escape character is the

e

escape; a backslash glyph can be obtained by

(rs.

Text

"
"

The open parenthesis is only special in escape sequences when
introducing an escape name or argument consisting of exactly two
characters.
In groff, this behavior can be replaced by the [] construct.

Text

"
"

The opening bracket is only special in groff escape sequences; there
it is used to introduce a long escape name or long escape argument.
Otherwise, it is non-special, e.g. in macro calls.

Text

"
"

The closing bracket is only special in groff escape sequences; there
it terminates a long escape name or long escape argument.
Otherwise, it is non-special.

space

Space characters are only functional characters.
They separate the arguments in requests, macros, and strings, and the words
in text lines.
They are subject to groff’s horizontal spacing calculations.
To get a defined space width, escape sequences like

Text

"
"

(this is the escape character followed by a space),

|,

^,

or

h

should be used.

newline

In text paragraphs, newlines mostly behave like space characters.
Continuation lines can be specified by an escaped newline, i.e., by
specifying a backslash

Text

"
"

as the last character of a line.

tab

If a tab character occurs during text the interpreter makes a
horizontal jump to the next pre-defined tab position.
There is a sophisticated interface for handling tab positions.

NUMERICAL EXPRESSIONS

Anumerical value

is a signed or unsigned integer or float with or without an appended
scaling indicator.
Ascaling indicator

is a one-character abbreviation for a unit of measurement.
A number followed by a scaling indicator signifies a size value.
By default, numerical values do not have a scaling indicator, i.e., they
are normal numbers.

Theroff

language defines the following scaling indicators.

c

Centimeter

i

Inch

P

Pica = 1/6 inch

p

Point = 1/72 inch

m

Em = the font size in points (width of letter `m’)

M

100th of an Em

n

En = Em/2

u

Basic unit for actual output device

v

Vertical line space in basic units
scaled point = 1/sizescale of a point (defined in
font DESC file)

f

Scale by 65536.

Numerical expressions

are combinations of the numerical values defined above with the
following arithmetical operators already defined in classical troff.

+

Addition

-

Subtraction

*

Multiplication

/

Division

%

Modulo

=

Equals

==

Equals

<

Less than

>

Greater than

<=

Less or equal

>=

Greater or equal

&

Logical and

:

Logical or

!

Logical not

(

Grouping of expressions

)

Close current grouping

Moreover,groff

added the following operators for numerical expressions:

Text

"
"

The maximum ofe1

ande2.

Text

"
"

The minimum ofe1

ande2.

Text

"
"

Evaluatee

usingc

as the default scaling indicator.

For details see the groff info file.

CONDITIONS

Conditions

occur in tests raised by the

if ,

ie ,

and the

while

requests.
The following table characterizes the different types of conditions.

N

A numerical expressionN

yields true if its value is greater than~0.

!N

True if the value ofI

is~0.

‘s1‘s2‘

True if string~s1

is identical to string~s2.

!’s1‘s2‘

True if string~s1

is not identical to string~s2.

cch

True if there is a character~ch

available.

dname

True if there is a string, macro, diversion, or request calledname.

e

Current page number is even.

o

Current page number is odd.

mname

True if there is a color calledname.

n

Formatter isnroff.

rreg

True if there is a register namedreg.

t

Formatter istroff.

REQUESTS

This section provides a short reference for the predefined requests.
In groff, request and macro names can be arbitrarily long.
No bracketing or marking of long names is needed.

Most requests take one or more arguments.
The arguments are separated by space characters (no tabs!); there is
no inherent limit for their length or number.
An argument can be enclosed by a pair of double quotes.
This is very handy if an argument contains space characters, e.g.,
"arg with space"

denotes a single argument.

Some requests have optional arguments with a different behaviour.
Not all of these details are outlined here.
Refer to the groff info file andgroff_diff(7)

for all details.

In the following request specifications, most argument names were
chosen to be descriptive.
Only the following denotations need clarification.

c

denotes a single character.

font

a font either specified as a font name or a font number.

anything

all characters up to the end of the line or within

{

and

}.

n

is a numerical expression that evaluates to an integer value.

N

is an arbitrary numerical expression, signed or unsigned.

±N

has three meanings depending on its sign, described below.

If an expression defined as±N

starts with a

Text

"
"

sign the resulting value of the expression will be added to an already
existing value inherent to the related request, e.g. adding to a number
register.
If the expression starts with a

Text

"
"

the value of the expression will be subtracted from the request value.

Without a sign,N

replaces the existing value directly.
To assign a negative number either prepend~0 or enclose the negative
number in parentheses.

Request Short Reference

Empty line, ignored.
Useful for structuring documents.

Complete line is a comment.

Printstring

on standard error, exit program.

Begin line adjustment for output lines in current adjust mode.

Start line adjustment in modec

(c=l,r,b,n).

Assign formatc

toregister

(c=l,i,I,a,A).

Create alias name forregister.

Create alias name for request, string, macro, or diversionobject.

Append tomacro

until..

is encountered.

Append tomacro

until

.end

is called.

Append to a macro whose name is contained in the string registermacro

until..

is encountered.

Append to a macro indirectly.macro

andend

are string registers whose contents are interpolated for the macro name
and the end macro, respectively.

Besides these standard groff requests, there might be further macro
calls.
They can originate from a macro package (seeroff(7)

for an overview) or from a preprocessor.

Preprocessor macros are easy to be recognized.
They enclose their code into a pair of characteristic macros.

preprocessor

start macro

end macro

eqn

.PS

.PE

grap

.G1

.G2

grn

.GS

.GE

pic

.PS

.PE

refer

.R1

.R2

soelim

none

none

tbl

.TS

.TE

ESCAPE SEQUENCES

Escape sequences are in-line language elements usually introduced by a
backslash

Text

"
"

and followed by an escape name and sometimes by a required argument.
Input processing is continued directly after the escaped character or
the argument resp. without an intervening separation character.
So there must be a way to determine the end of the escape name and the
end of the argument.

This is done by enclosing names (escape name and arguments consisting
of a variable name) by a pair of brackets[name]