LilyPond Learning Manual: 3.4.3 Building a score from scratch

### 3.4.3 Building a score from scratch

After gaining some facility with writing LilyPond code, you may find that it is easier to build a score from scratch rather than modifying one of the templates. You can also develop your own style this way to suit the sort of music you like. Let’s see how to put together the score for an organ prelude as an example.

We begin with a header section. Here go the title, name of composer, etc, then come any variable definitions, and finally the score block. Let’s start with these in outline and fill in the details later.

We’ll use the first two bars of Bach’s prelude based on Jesu, meine Freude which is written for two manuals and pedal organ. You can see these two bars of music at the bottom of this section. The top manual part has two voices, the lower and pedal organ one each. So we need four music definitions and one to define the time signature and key:

>     \version "2.14.1"
>     \header {
>       title = "Jesu, meine Freude"
>       composer = "J S Bach"
>     }
>     keyTime = { \key c \minor \time 4/4 }
>     ManualOneVoiceOneMusic = { s1 }
>     ManualOneVoiceTwoMusic = { s1 }
>     ManualTwoMusic = { s1 }
>     PedalOrganMusic = { s1 }

>     \score {
>     }

For now we’ve just used a spacer note, `s1`, instead of the real music. We’ll add that later.

Next let’s see what should go in the score block. We simply mirror the staff structure we want. Organ music is usually written on three staves, one for each manual and one for the pedals. The manual staves should be bracketed together, so we need to use a PianoStaff for them. The first manual part needs two voices and the second manual part just one.

>     \new PianoStaff <<
>       \new Staff = "ManualOne" <<
>         \new Voice {
>           \ManualOneVoiceOneMusic
>         }
>         \new Voice {
>           \ManualOneVoiceTwoMusic
>         }
>       >>  % end ManualOne Staff context
>       \new Staff = "ManualTwo" <<
>         \new Voice {
>           \ManualTwoMusic
>         }
>       >>  % end ManualTwo Staff context
>     >>  % end PianoStaff context

Next we need to add a staff for the pedal organ. This goes underneath the PianoStaff, but it must be simultaneous with it, so we need angle brackets around the two. Missing these out would generate an error in the log file. It’s a common mistake which you’ll make sooner or later! Try copying the final example at the end of this section, remove these angle brackets, and compile it to see what errors it generates.

>     <<  % PianoStaff and Pedal Staff must be simultaneous
>       \new PianoStaff <<
>         \new Staff = "ManualOne" <<
>           \new Voice {
>             \ManualOneVoiceOneMusic
>           }
>           \new Voice {
>             \ManualOneVoiceTwoMusic
>           }
>         >>  % end ManualOne Staff context
>         \new Staff = "ManualTwo" <<
>           \new Voice {
>             \ManualTwoMusic
>           }
>         >>  % end ManualTwo Staff context
>       >>  % end PianoStaff context
>       \new Staff = "PedalOrgan" <<
>         \new Voice {
>           \PedalOrganMusic
>         }
>       >>
>     >>

It is not necessary to use the simultaneous construct `<< .. >>` for the manual two staff and the pedal organ staff, since they contain only one music expression, but it does no harm, and always using angle brackets after `\new Staff` is a good habit to cultivate in case there are multiple voices. The opposite is true for Voices: these should habitually be followed by braces `{ .. }` in case your music is coded in several variables which need to run consecutively.

Let’s add this structure to the score block, and adjust the indenting. We also add the appropriate clefs, ensure stems, ties and slurs in each voice on the upper staff point to the right direction with `\voiceOne` and `\voiceTwo`, and enter the key and time signature to each staff using our predefined variable, `\keyTime`.

>     \score {
>       <<  % PianoStaff and Pedal Staff must be simultaneous
>         \new PianoStaff <<
>           \new Staff = "ManualOne" <<
>             \keyTime  % set key and time signature
>             \clef "treble"
>             \new Voice {
>               \voiceOne
>               \ManualOneVoiceOneMusic
>             }
>             \new Voice {
>               \voiceTwo
>               \ManualOneVoiceTwoMusic
>             }
>           >>  % end ManualOne Staff context
>           \new Staff = "ManualTwo" <<
>             \keyTime
>             \clef "bass"
>             \new Voice {
>               \ManualTwoMusic
>             }
>           >>  % end ManualTwo Staff context
>         >>  % end PianoStaff context
>         \new Staff = "PedalOrgan" <<
>           \keyTime
>           \clef "bass"
>           \new Voice {
>             \PedalOrganMusic
>           }
>         >>  % end PedalOrgan Staff
>       >>
>     }  % end Score context

The above layout of the organ staves is almost perfect; however, there is a slight defect which is not visible by looking at just a single system: The distance of the pedal staff to the left hand staff should behave approximately the same as the right hand staff to the left hand staff. In particular, the stretchability of staves in a `PianoStaff` context is limited (so that the distance between the staves for the left and right hand can’t become too large), and the pedal staff should behave similarly.

Stretchability of staves can be controlled with the `staff-staff-spacing` property of the `VerticalAxisGroup` ‘graphical object’ (commonly called ‘grob’s within the lilypond documentation) – don’t worry about the details right now; this is fully explained later. For the curious, have a look at Overview of modifying properties. In this case, we want to modify the `stretchability` sub-property only. Again, for the curious, you can find the default values for the staff-staff-spacing property in file ‘`scm/define-grobs.scm`’ by looking up the definition of the `VerticalAxisGroup` grob. The value for `stretchability` is taken from the definition of the `PianoStaff` context (in file ‘`ly/engraver-init.ly`’) so that the values are identical.

>     \score {
>       <<  % PianoStaff and Pedal Staff must be simultaneous
>         \new PianoStaff <<
>           \new Staff = "ManualOne" <<
>             \keyTime  % set key and time signature
>             \clef "treble"
>             \new Voice {
>               \voiceOne
>               \ManualOneVoiceOneMusic
>             }
>             \new Voice {
>               \voiceTwo
>               \ManualOneVoiceTwoMusic
>             }
>           >>  % end ManualOne Staff context
>           \new Staff = "ManualTwo" \with {
>             \override VerticalAxisGroup
>               #'staff-staff-spacing  #'stretchability = 5
>           } <<
>             \keyTime
>             \clef "bass"
>             \new Voice {
>               \ManualTwoMusic
>             }
>           >>  % end ManualTwo Staff context
>         >>  % end PianoStaff context
>         \new Staff = "PedalOrgan" <<
>           \keyTime
>           \clef "bass"
>           \new Voice {
>             \PedalOrganMusic
>           }
>         >>  % end PedalOrgan Staff
>       >>
>     }  % end Score context

That completes the structure. Any three-staff organ music will have a similar structure, although the number of voices may vary. All that remains now is to add the music, and combine all the parts together.

> >     \version "2.14.1"

>     \header {
>       title = "Jesu, meine Freude"
>       composer = "J S Bach"
>     }
>     keyTime = { \key c \minor \time 4/4 }
>     ManualOneVoiceOneMusic = \relative g' {
>       g4 g f ees |
>       d2 c |
>     }
>     ManualOneVoiceTwoMusic = \relative c' {
>       ees16 d ees8~ ees16 f ees d c8 d~ d c~ |
>       c8 c4 b8 c8. g16 c b c d |
>     }
>     ManualTwoMusic = \relative c' {
>       c16 b c8~ c16 b c g a8 g~ g16 g aes ees |
>       f16 ees f d g aes g f ees d e8~ ees16 f ees d |
>     }
>     PedalOrganMusic = \relative c {
>       r8 c16 d ees d ees8~ ees16 a, b g c b c8 |
>       r16 g ees f g f g8 c,2 |
>     }

>     \score {
>       <<  % PianoStaff and Pedal Staff must be simultaneous
>         \new PianoStaff <<
>           \new Staff = "ManualOne" <<
>             \keyTime  % set key and time signature
>             \clef "treble"
>             \new Voice {
>               \voiceOne
>               \ManualOneVoiceOneMusic
>             }
>             \new Voice {
>               \voiceTwo
>               \ManualOneVoiceTwoMusic
>             }
>           >>  % end ManualOne Staff context
>           \new Staff = "ManualTwo" \with {
>             \override VerticalAxisGroup
>               #'staff-staff-spacing #'stretchability = 5
>           } <<
>             \keyTime
>             \clef "bass"
>             \new Voice {
>               \ManualTwoMusic
>             }
>           >>  % end ManualTwo Staff context
>         >>  % end PianoStaff context
>         \new Staff = "PedalOrgan" <<
>           \keyTime
>           \clef "bass"
>           \new Voice {
>             \PedalOrganMusic
>           }
>         >>  % end PedalOrgan Staff context
>       >>
>     }  % end Score context

#### See also

Music Glossary: system.

Other languages: česky, deutsch, español, français, magyar, italiano, 日本語, nederlands.

#### LilyPond — Learning Manual