#### 4.2.6 Defining new contexts

Specific contexts, like `Staff` and `Voice`, are made of simple building blocks, and it is possible to compose engraver plug-ins in different combinations, thereby creating new types of contexts.

The next example shows how to build a different type of `Voice` context from scratch. It will be similar to `Voice`, but print centered slash noteheads only. It can be used to indicate improvisation in Jazz pieces,

These settings are again done within a `\context` block inside a `\paper` block,

           \paper {
             \context {
               ...
             }
           }

In the following discussion, the example input shown should go on the ... in the previous fragment.

First, name the context gets a name. Instead of `Voice` it will be called `ImproVoice` ,

      \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

      \consists Note_heads_engraver
      \consists Text_engraver

but only 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, without a 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_engraver,

          \type "Engraver_group_engraver"

Putting together, we get

      \context {
        \name ImproVoice
        \type "Engraver_group_engraver"
        \consists "Note_heads_engraver"
        \consists "Text_script_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,1

      \context {
        \StaffContext
        \accepts ImproVoice
      }

Putting both into a `\paper` block, like

           \paper {
             \context {
               \name ImproVoice
               ...
             }
           \context {
             \StaffContext
             \accepts "ImproVoice"
           }
         }

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

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

#### Footnotes

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

Read comments on this page, or add one. This page is for LilyPond-2.2.2 (stable-branch).