+@cindex template, writing your own
+@cindex example of writing a score
+@cindex writing a score, example
+@cindex score, example of writing
+
+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 @emph{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:
+
+@example
+\version @w{"@version{}"}
+\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 @{
+@}
+@end example
+
+For now we've just used a spacer note, @code{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.
+
+@example
+\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
+@end example
+
+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.
+
+@example
+<< % 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
+ @}
+ >>
+>>
+@end example
+
+It is not necessary to use the simultaneous construct
+@code{<< .. >>} 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 @code{\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
+@code{@{ .. @}} 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
+@code{\voiceOne} and @code{\voiceTwo}, and enter the key and time
+signature to each staff using our predefined variable, @code{\keyTime}.
+
+@example
+\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
+@end example
+
+@cindex stretchability of staves
+@cindex staves, stretchability
+
+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
+@code{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.
+
+@cindex sub-properties
+@cindex properties, sub-properties
+@cindex graphical objects
+@cindex objects, graphical
+@cindex grobs
+
+Stretchability of staves can be controlled with the
+@code{staff-staff-spacing} property of the
+@code{VerticalAxisGroup} @q{graphical object} (commonly called
+@q{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 @ruser{Overview of modifying properties}.
+In this case, we want to modify the @code{stretchability}
+sub-property only. Again, for the curious, you can find the
+default values for the staff-staff-spacing property
+in file @file{scm/define-grobs.scm} by looking up the definition
+of the @code{VerticalAxisGroup} grob. The value for
+@code{stretchability} is taken from the definition of the
+@code{PianoStaff} context (in file @file{ly/engraver-init.ly})
+so that the values are identical.
+
+@example
+\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
+@end example
+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.
+
+@lilypond[quote,verbatim,ragged-right,addversion]
+\header {
+ title = "Jesu, meine Freude"
+ composer = "J S Bach"
+}
+keyTime = { \key c \minor \time 4/4 }
+ManualOneVoiceOneMusic = \relative {
+ g'4 g f ees |
+ d2 c |
+}
+ManualOneVoiceTwoMusic = \relative {
+ ees'16 d ees8~ 16 f ees d c8 d~ d c~ |
+ 8 c4 b8 c8. g16 c b c d |
+}
+ManualTwoMusic = \relative {
+ c'16 b c8~ 16 b c g a8 g~ 16 g aes ees |
+ f16 ees f d g aes g f ees d ees8~ 16 f ees d |
+}
+PedalOrganMusic = \relative {
+ r8 c16 d ees d ees8~ 16 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
+@end lilypond
+
+@seealso
+Music Glossary:
+@rglos{system}.