[ << Changing defaults ] | [Top][Contents][Index] | [ Notation manual tables >> ] |
[ < Bottom-level contexts – voices ] | [ Up : Interpretation contexts ] | [ Keeping contexts alive > ] |
5.1.2 Creating and referencing contexts
LilyPond will create 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 } >>
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 } >>
\new
should always be used to specify unnamed contexts.
The difference between \new
and \context
is in the
action taken:
-
\new
with or without a name will always create a fresh, distinct, context, even if one with the same name already exists:\new Staff << \new Voice = "A" \relative { \voiceOne c''8 c c4 c c } \new Voice = "A" \relative { \voiceTwo g'4 g g g } >>
-
\context
with a name specified will create a distinct context only if a context of the same type with the same name in the same context hierarchy does not already exist. Otherwise it will be taken as a reference to that previously created context, and its music expression will be passed to that context for interpretation.Named contexts may be useful in special cases such as lyrics or figured bass, as demonstrated in Working with lyrics and variables and Vocal ensembles templates for the former and Displaying figured bass for the latter. More generally, one application of named contexts is in separating the score layout from the musical content. Either of these two forms is valid:
\score { << % score layout \new Staff << \new Voice = "one" { \voiceOne } \new Voice = "two" { \voiceTwo } >> % musical content \context Voice = "one" { \relative { c''4 c c c } } \context Voice = "two" { \relative { g'8 g g4 g g } } >> }
\score { << % score layout \new Staff << \context Voice = "one" { \voiceOne } \context Voice = "two" { \voiceTwo } >> % musical content \context Voice = "one" { \relative { c''4 c c c } } \context Voice = "two" { \relative { g'8 g g4 g g } } >> }
Alternatively, variables may be employed to similar effect. See Organizing pieces with variables.
-
\context
with no name will match the first of any previously created contexts of the same type in the same context hierarchy, even one that has been given a name, and its music expression will be passed to that context for interpretation. This form is rarely useful. However,\context
with no name and no music expression is used to set the context in which a Scheme procedure specified with\applyContext
is executed:\new Staff \relative { c'1 \context Timing \applyContext #(lambda (ctx) (newline) (display (ly:context-current-moment ctx))) c1 }
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.
[ << Changing defaults ] | [Top][Contents][Index] | [ Notation manual tables >> ] |
[ < Bottom-level contexts – voices ] | [ Up : Interpretation contexts ] | [ Keeping contexts alive > ] |