Product SiteDocumentation Site

12.4.6.2. Contexts

Another aspect of LilyPond that often confuses beginners is the idea of contexts. It is an essential concept for modifying default behaviour. Like everything in LilyPond, it makes perfect sense: "context" means "context." The three primary contexts are "Voice," "Staff," and "Score." Everything that happens in a score happens in a context - it's just that simple - everything happens in a context! Everything that happens in a score happens in Score context, everything that happens on a staff happens in a Staff context, and everything that happens in a voice happens in a Voice context.
To help clear things up a little, here are three examples:
  1. Where does the title of a composition belong?
    • Does it belong on the score? Yes, so it belongs in the Score context.
    • Does it belong on a staff? No, so it doesn't belong in a Staff context.
    • Does it belong in a voice? No, since it would have to be on a staff to be in a voice.
    The composition's title doesn't belong on a staff or in a voice, but it does belong on the score, so we say that it happens in the Score context.
  2. Where does a clef belong?
    • Does it belong on the score? Yes, because it is a part of notation.
    • Does it belong on a staff? Yes, because it wouldn't make sense to have a clef off a staff.
    • Does it belong in a voice? No, because it's not related to a specific "line" or voice.
    Clefs usually belong on the staff, at the beginning of every line, so we say that they happen in a Staff context.
  3. Where does a note-head belong?
    • Does it belong on the score? Yes, because it is a part of notation.
    • Does it belong on a staff? Yes - even if it's on a ledger line, note-heads are meaningless unless they're on a staff.
    • Does it belong in a voice? Yes, because one particular musical line is indicated primarily with note-heads.
    Note-heads belong to a particular voice, so we say they happen in a Voice context.
To help further clarify, consider the following:
As in the real world, objects in LilyPond can potentially happen in any context...
  • (like a doorknob on a door)
  • (like a bus driver sitting in a bus' drivers' seat)
  • (like a person buying groceries at a supermarket)
  • (like a flat-sign next to a note-head)
... some contexts are unusual, but make sense...
  • (like a doorknob on a wall, if the wall is designed to look like a door)
  • (like a bus driver sitting in a passenger seat, if they are going to the garage)
  • (like a person buying groceries from a corner store, if the supermarkets are closed)
  • (like a flat sign in a paragraph of text, if that paragraph describes what flat signs do)
... and some contexts do not make sense, but can still happen...
  • (like a doorknob on a television)
  • (like a bus driver serving patrons at a restaurant)
  • (like a person buying groceries at a governmental office)
  • (like a flat sign in the top corner of a score, where the page number should be)
The LilyPond designers wisely decided that they could not think of all possible uses for their software, so LilyPond allows most engravers to be used in most contexts. Furthermore, a context can happen within an other context, which makes sense - a Voice context only makes sense if it appears on a Staff, and a Staff only makes sense if it appears in a Score.
So, when trying to sort out the context in which something should apply, ask yourself exactly that: "In what context does this apply?" Beams and flags happen in one voice, accidentals apply to a whole staff, and tempo markings apply to the whole score. Although it may take some careful thought to get used to the idea, contexts ultimately make perfect sense.