LilyPond Notation Reference: 5.1.6 Defining new contexts

### 5.1.6 Defining new contexts

Specific contexts, like `Staff` and `Voice`, are made of 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`, we want commands that work on (existing) `Voice`s to remain working. This is achieved by giving the new context an alias `Voice`,

>     \alias Voice

The context will print notes and instructive texts, so we need to add the engravers which provide this functionality,

>     \consists Note_heads_engraver
>     \consists Text_engraver

but we only need this on the center line,

>     \consists Pitch_squash_engraver
>     squashedPosition = #0

The Pitch_squash_engraver modifies note heads (created by 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
>     \override Stem #'transparent = ##t

All these plug-ins have to cooperate, and this is achieved with a special plug-in, which must be marked with the keyword `\type`. This should always be `Engraver_group`.

>     \type "Engraver_group"

Put together, we get

>     \context {
>       \name ImproVoice
>       \type "Engraver_group"
>       \consists "Note_heads_engraver"
>       \consists "Text_engraver"
>       \consists Pitch_squash_engraver
>       squashedPosition = #0
>       \override NoteHead #'style = #'slash
>       \override Stem #'transparent = ##t
>       \alias Voice
>     }

Contexts form hierarchies. We want to hang the `ImproVoice` under `Staff`, just like normal `Voice`s. Therefore, we modify the `Staff` definition with the `\accepts` command,

>     \context {
>       \Staff
>       \accepts ImproVoice
>     }

The opposite of `\accepts` is `\denies`, which is sometimes needed when reusing existing context definitions.

Putting both into a `\layout` block, like

>     \layout {
>       \context {
>         \name ImproVoice
>         …
>       }
>       \context {
>         \Staff
>         \accepts "ImproVoice"
>       }
>     }

Then the output at the start of this subsection can be entered as

>     \relative c'' {
>       a4 d8 bes8
>       \new ImproVoice {
>         c4^"ad lib" c
>         c4 c^"undress"
>         c c_"while playing :)"
>       }
>       a1
>     }

Jiné jazyky: deutsch, español, français, 日本語.

#### LilyPond — Notation Reference