5.23.4 Conditional Blocks

Escape sequence: \{
Escape sequence: \}

It is frequently desirable for a control structure to govern more than one request, macro call, text line, or a combination of the foregoing. The opening and closing brace escape sequences \{ and \} perform such grouping; such conditional blocks can be nested. Brace escape sequences outside of control structures have no meaning and produce no output.

\{ should appear (after optional spaces and tabs) immediately subsequent to the request’s conditional expression. \} should appear on a line with other occurrences of itself as necessary to match \{ sequences. It can be preceded by a control character, spaces, and tabs. Input after any quantity of \} sequences on the same line is processed only if all of the preceding conditions to which they correspond are true. Furthermore, a \} closing the body of a while request (discussed below) must be the last such escape sequence on an input line.

A
.if 0 \{ B
C
D
\}E
F
    ⇒ A F
N
.if 1 \{ O
.  if 0 \{ P
Q
R\} S\} T
U
    ⇒ N O U

The above behavior may challenge the intuition; it was implemented to retain compatibility with AT&T troff. For clarity, it is idiomatic to end input lines with \{, followed by \RET if desired to prevent the newline from being interpreted as a blank text line, and to precede \} on an input line with nothing more than a control character, spaces, tabs, and other instances of itself.

.de DEBUG
debug =
.ie \\$1 \{\
ON,
development
\}
.el \{\
OFF,
production
\}
version
..
.DEBUG 0
.br
.DEBUG 1

Try omitting the \RETs from the foregoing example and see how the output changes. Remember that, as noted above, after a true conditional (or after the el request if its counterpart ie condition was false) the remainder of the input line is interpreted as if it were on an input line by itself.

We can use ie, el, and conditional blocks to simulate the multi-way “switch” or “case” control structures of other languages. The following example is adapted from the groff man package. Indentation is used to clarify the logic.

.\" Simulate switch/case in roff.
.      ie '\\$2'1' .ds title General Commands\"
.el \{.ie '\\$2'2' .ds title System Calls\"
.el \{.ie '\\$2'3' .ds title Library Functions\"
.el \{.ie '\\$2'4' .ds title Kernel Interfaces\"
.el \{.ie '\\$2'5' .ds title File Formats\"
.el \{.ie '\\$2'6' .ds title Games\"
.el \{.ie '\\$2'7' .ds title Miscellaneous Information\"
.el \{.ie '\\$2'8' .ds title System Management\"
.el \{.ie '\\$2'9' .ds title Kernel Development\"
.el                .ds title \" empty
.\}\}\}\}\}\}\}\}