5.1.2 Creating and referencing contexts

LilyPond creates lower-level contexts automatically if a music expression is encountered before a suitable context exists, but this is usually successful only for simple scores or music fragments like the ones in the documentation. For more complex scores it is advisable to specify all contexts explicitly with either the \new or \context command. The syntax of these two commands is very similar:

[\new | \context] Context [= name] [music-expression]

where either \new or \context may be specified. Context is the type of context which is to be created, name is an optional name to be given to the particular context being created, and music-expression is a single music expression that is to be interpreted by the engravers and performers in this context.

The \new prefix without a name is commonly used to create scores with many staves:

<<
  \new Staff \relative {
    % leave the Voice context to be created implicitly
    c''4 c
  }
  \new Staff \relative {
    d''4 d
  }
>>

[image of music]

and to place several voices into one staff:

\new Staff <<
  \new Voice \relative {
    \voiceOne c''8 c c4 c c
  }
  \new Voice \relative {
    \voiceTwo g'4 g g g
  }
>>

[image of music]

\new should always be used to specify unnamed contexts.

The difference between \new and \context is in the action taken:

A context must be named if it is to be referenced later, for example when lyrics are associated with music:

\new Voice = "tenor" music
…
\new Lyrics \lyricsto "tenor" lyrics

For details of associating lyrics with music, see Automatic syllable durations.

The properties of all contexts of a particular type can be modified in a \layout block (with a different syntax), see Changing all contexts of the same type. This construct also provides a means of keeping layout instructions separate from the musical content. If a single context is to be modified, a \with block must be used, see Changing just one specific context.

See also

Learning Manual: Organizing pieces with variables.

Notation Reference: Changing just one specific context, Automatic syllable durations.


LilyPond Notation Reference v2.25.22 (development-branch).