lilypond-learning: Organizing pieces with variables

 
 2.4.1 Organizing pieces with variables
 --------------------------------------
 
 When all of the elements discussed earlier are combined to produce
 larger files, the music expressions get a lot bigger.  In polyphonic
 music with many staves, the input files can become very confusing.  We
 can reduce this confusion by using _variables_.
 
    With variables (also known as identifiers or macros), we can break up
 complex music expressions.  A variable is assigned as follows:
 
      namedMusic = { ... }
 
    The contents of the music expression ‘namedMusic’ can be used later
 by placing a backslash in front of the name (‘\namedMusic’, just like a
 normal LilyPond command).
 
      violin = \new Staff {
        \relative c'' {
          a4 b c b
        }
      }
      
      cello = \new Staff {
        \relative c {
          \clef "bass"
          e2 d
        }
      }
      
      {
        <<
          \violin
          \cello
        >>
      }
      [image src="" alt="[image of music]" text="image of music"]
 
 The name of a variable must have alphabetic characters only, no numbers,
 underscores, or dashes.
 
    Variables must be defined _before_ the main music expression, but may
 be used as many times as required anywhere after they have been defined.
 They may even be used in a later definition of another variable, giving
 a way of shortening the input if a section of music is repeated many
 times.
 
      tripletA = \tuplet 3/2 { c,8 e g }
      barA = { \tripletA \tripletA \tripletA \tripletA }
      
      \relative c'' {
        \barA \barA
      }
      [image src="" alt="[image of music]" text="image of music"]
 
    Variables may be used for many other types of objects in the input.
 For example,
 
      width = 4.5\cm
      name = "Wendy"
      aFivePaper = \paper { paperheight = 21.0 \cm }
 
    Depending on its contents, the variable can be used in different
 places.  The following example uses the above variables:
 
      \paper {
        \aFivePaper
        line-width = \width
      }
 
      {
        c4^\name
      }