[ << Common notation ] | [首页][目录][索引] | [ Fundamental concepts >> ] |
[ < Final touches ] | [ 上 : Final touches ] | [ Adding titles > ] |
3.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 { a'4 b c b } } cello = \new Staff { \relative { \clef bass e2 d } } { << \violin \cello >> }
By convention, variable names consist of alphabetic characters only. For detailed information, see File structure.
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 = \relative { \tuplet 3/2 { c'8 e g } } barA = { \tripletA \tripletA \tripletA \tripletA } { \barA \barA }
Variables may be used for many other types of objects in the input. For example,
myWidth = 60 % a number to pass to a \paper variable % (the unit is millimeter) myName = "Wendy" % a string to pass to a markup aFivePaper = \paper { #(set-paper-size "a5") }
Depending on its contents, the variable can be used in different places. The following example uses the above variables:
\paper { \aFivePaper line-width = \myWidth } { c4^\myName }
[ << Common notation ] | [首页][目录][索引] | [ Fundamental concepts >> ] |
[ < Final touches ] | [ 上 : Final touches ] | [ Adding titles > ] |