5.1.6 Defining new contexts

Specific contexts, like Staff and Voice, are made from
simple building blocks. It is possible to create new types of
contexts with different combinations of engraver plug-ins.

The next example shows how to build a different type of
Voice context from scratch. It will be similar to
Voice, but only prints centered slash note heads. It can be used
to indicate improvisation in jazz pieces,

These settings are defined within a \context block inside a
\layout block,

\layout {
\context {
…
}
}

In the following discussion, the example input shown should go in place
of the … in the previous fragment.

First it is necessary to define a name for the new context:

\name ImproVoice

Since it is similar to the Voice context, we want commands that
work in (existing) Voice contexts to continue working. This is
achieved by giving the new context an alias of Voice,

\alias Voice

The context will print notes and instructive texts, so we need to add
the engravers which provide this functionality, plus the engraver which
groups notes, stems and rests which occur at the same musical moment
into columns,

The Pitch_squash_engraver modifies note heads (created
by the Note_heads_engraver) and sets their vertical
position to the value of squashedPosition, in this
case 0, the center line.

The notes look like a slash, and have no stem,

\override NoteHead.style = #'slash
\hide Stem

All these plug-ins have to communicate under the control of the
context. The mechanisms with which contexts communicate are
established by declaring the context \type. Within a
\layout block, most contexts will be of type
Engraver_group. Some special contexts and contexts in
\midi blocks use other context types. Copying and
modifying an existing context definition will also fill in the
type. Since this example creates a definition from scratch, it
needs to be specified explicitly.