lilypond-learning: Saving typing with variables and functions

 
 3.4.4 Saving typing with variables and functions
 ------------------------------------------------
 
 By this point, you’ve seen this kind of thing:
 
      hornNotes = \relative c'' { c4 b dis c }
      
      \score {
        {
          \hornNotes
        }
      }
      [image src="" alt="[image of music]" text="image of music"]
 
    You may even realize that this could be useful in minimalist music:
 
      fragmentA = \relative c'' { a4 a8. b16 }
      fragmentB = \relative c'' { a8. gis16 ees4 }
      
      violin = \new Staff {
        \fragmentA \fragmentA |
        \fragmentB \fragmentA |
      }
      
      \score {
        {
          \violin
        }
      }
      [image src="" alt="[image of music]" text="image of music"]
 
    However, you can also use these variables (also known as macros, or
 user-defined commands) for tweaks:
 
      dolce = \markup { \italic \bold dolce }
      
      padText = { \once \override TextScript.padding = #5.0 }
      fthenp =_\markup {
        \dynamic f \italic \small { 2nd } \hspace #0.1 \dynamic p
      }
      
      violin = \relative c'' {
        \repeat volta 2 {
          c4._\dolce b8 a8 g a b |
          \padText
          c4.^"hi there!" d8 e' f g d |
          c,4.\fthenp b8 c4 c-. |
        }
      }
      
      \score {
        {
          \violin
        }
        \layout { ragged-right = ##t }
      }
      [image src="" alt="[image of music]" text="image of music"]
 
    These variables are obviously useful for saving typing.  But they’re
 worth considering even if you only use them once – they reduce
 complexity.  Let’s look at the previous example without any variables.
 It’s a lot harder to read, especially the last line.
 
      violin = \relative c'' {
        \repeat volta 2 {
          c4._\markup { \italic \bold dolce } b8 a8 g a b |
          \once \override TextScript.padding = #5.0
          c4.^"hi there!" d8 e' f g d |
          c,4.\markup {
            \dynamic f \italic \small { 2nd } \hspace #0.1 \dynamic p
          }
          b8 c4 c-. |
        }
      }
 
    So far we’ve seen static substitution – when LilyPond sees
 ‘\padText’, it replaces it with the stuff that we’ve defined it to be
 (ie the stuff to the right of ‘padtext=’).
 
    LilyPond can handle non-static substitution, too (you can think of
 these as functions).
 
      padText =
      #(define-music-function
           (parser location padding)
           (number?)
         #{
           \once \override TextScript.padding = #padding
         #})
      
      \relative c''' {
        c4^"piu mosso" b a b |
        \padText #1.8
        c4^"piu mosso" d e f |
        \padText #2.6
        c4^"piu mosso" fis a g |
      }
      [image src="" alt="[image of music]" text="image of music"]
 
    Using variables is also a good way to reduce work if the LilyPond
 input syntax changes (see See(lilypond-usage)Updating files with
 convert-ly).  If you have a single definition (such as ‘\dolce’) for
 all your input files (see SeeStyle sheets), then if the syntax
 changes, you only need to update your single ‘\dolce’ definition,
 instead of making changes throughout every ‘.ly’ file.