LilyPond Notation Reference: 5.1.3 Keeping contexts alive

### 5.1.3 Keeping contexts alive

Contexts are usually terminated at the first musical moment in which they have nothing to do. So `Voice` contexts die as soon as they contain no events, `Staff` contexts die as soon as all the `Voice` contexts within them contain no events, etc. This can cause difficulties if earlier contexts which have died have to be referenced, for example, when changing staves with `\change` commands, associating lyrics with a voice with `\lyricsto` commands, or when adding further musical events to an earlier context.

There is an exception to this general rule: inside of an `{…}` construct (sequential music), the construct’s notion of the “current context” will descend whenever an element of the sequence ends in a subcontext of the previous current context. This avoids spurious creation of implicit contexts in a number of situations but means that the first context descended into will be kept alive until the end of the expression.

In contrast, the contexts of a `<<…>>` construct’s (simultaneous music) expression are not carried forth, so enclosing a context creating command in an extra pair of `<<…>>` will keep the context from persisting through all of the enclosing `{…}` sequence.

Any context can be kept alive by ensuring it has something to do at every musical moment. `Staff` contexts are kept alive by ensuring one of their voices is kept alive. One way of doing this is to add spacer rests to a voice in parallel with the real music. These need to be added to every `Voice` context which needs to be kept alive. If several voices are to be used sporadically it is safest to keep them all alive rather than attempting to rely on the exceptions mentioned above.

In the following example, both voice A and voice B are kept alive in this way for the duration of the piece:

>     musicA = \relative { d''4 d d d }
>     musicB = \relative { g'4 g g g }
>     keepVoicesAlive = {
>       <<
>         \new Voice = "A" { s1*5 }  % Keep Voice "A" alive for 5 bars
>         \new Voice = "B" { s1*5 }  % Keep Voice "B" alive for 5 bars
>       >>
>     }

>     music = {
>       \context Voice = "A" {
>         \voiceOneStyle
>         \musicA
>       }
>       \context Voice = "B" {
>         \voiceTwoStyle
>         \musicB
>       }
>       \context Voice = "A" { \musicA }
>       \context Voice = "B" { \musicB }
>       \context Voice = "A" { \musicA }
>     }

>     \score {
>       \new Staff <<
>         \keepVoicesAlive
>         \music
>       >>
>     }

The following example shows how a sporadic melody line with lyrics might be written using this approach. In a real situation the melody and accompaniment would consist of several different sections, of course.

>     melody = \relative { a'4 a a a }
>     accompaniment = \relative { d'4 d d d }
>     words = \lyricmode { These words fol -- low the mel -- o -- dy }
>     \score {
>       <<
>         \new Staff = "music" {
>           <<
>             \new Voice = "melody" {
>               \voiceOne
>               s1*4  % Keep Voice "melody" alive for 4 bars
>             }
>             {
>               \new Voice = "accompaniment" {
>                 \voiceTwo
>                 \accompaniment
>               }
>               <<
>                 \context Voice = "melody" { \melody }
>                 \context Voice = "accompaniment" { \accompaniment }
>               >>
>               \context Voice = "accompaniment" { \accompaniment }
>               <<
>                 \context Voice = "melody" { \melody }
>                 \context Voice = "accompaniment" { \accompaniment }
>               >>
>             }
>           >>
>         }
>         \new Lyrics \with { alignAboveContext = "music" }
>         \lyricsto "melody" { \words }
>       >>
>     }

An alternative way, which may be better in many circumstances, is to keep the melody line alive by simply including spacer notes to line it up correctly with the accompaniment:

>     melody = \relative {
>       s1  % skip a bar
>       a'4 a a a
>       s1  % skip a bar
>       a4 a a a
>     }
>     accompaniment = \relative {
>       d'4 d d d
>       d4 d d d
>       d4 d d d
>       d4 d d d
>     }
>     words = \lyricmode { These words fol -- low the mel -- o -- dy }

>     \score {
>       <<
>         \new Staff = "music" {
>           <<
>             \new Voice = "melody" {
>               \voiceOne
>               \melody
>             }
>             \new Voice = "accompaniment" {
>               \voiceTwo
>               \accompaniment
>             }
>           >>
>         }
>         \new Lyrics \with { alignAboveContext = "music" }
>         \lyricsto "melody" { \words }
>       >>
>     }

#### LilyPond — Notation Reference v2.22.2 (stable-branch).