]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/user/fundamental.itely
Small fixes from Trevor.
[lilypond.git] / Documentation / user / fundamental.itely
1 @c -*- coding: utf-8; mode: texinfo; -*-
2 @c This file is part of lilypond-learning.tely
3
4 @node Fundamental concepts
5 @chapter Fundamental concepts
6
7 You've seen in the Tutorial how to produce beautifully printed
8 music from a simple text file.  This section introduces the
9 concepts and techniques required to produce equally beautiful
10 but more complex scores.
11
12 @menu
13 * How LilyPond files work::     
14 * Voices contain music::        
15 * Contexts and engravers::      
16 * Extending the templates::     
17 @end menu
18
19
20 @node How LilyPond files work
21 @section How LilyPond files work
22
23 The LilyPond input format is quite free-form, giving experienced
24 users a lot of flexibility to structure their files however they
25 wish.  But this flexibility can make things confusing for new
26 users.  This section will explain some of this structure, but may
27 gloss over some details in favor of simplicity.  For a complete
28 description of the input format, see @ruser{File structure}.
29
30 @menu
31 * Introduction to the LilyPond file structure::  
32 * Score is a (single) compound musical expression::  
33 * Nesting music expressions::   
34 * On the un-nestedness of brackets and ties::  
35 @end menu
36
37 @node Introduction to the LilyPond file structure
38 @subsection Introduction to the LilyPond file structure
39
40 A basic example of a lilypond input file is
41
42 @example
43 \version "2.11.23"
44 \score @{
45   @var{...compound music expression...}  % all the music goes here!
46   \header @{ @}
47   \layout @{ @}
48   \midi @{ @}
49 @}
50 @end example
51
52 @noindent
53 There are many variations of this basic pattern, but this
54 example serves as a useful starting place.
55
56 Up to this point none of the examples you have seen have used a
57 @code{\score@{@}} command.  This is because LilyPond automatically
58 adds the extra commands which are needed when you give it simple 
59 input.  LilyPond treats input like this:
60
61 @example
62 \relative c'' @{
63   c4 a d c
64 @}
65 @end example
66
67 @noindent
68 as shorthand for this:
69
70 @example
71 \book @{
72   \score @{
73     \new Staff @{
74       \new Voice @{
75         \relative c'' @{
76           c4 a b c
77         @}
78       @}
79     @}
80     \layout @{ @}
81   @}
82 @}
83 @end example
84
85 In other words, if the input contains a single music expression,
86 LilyPond will interpret the file as though the music expression
87 was wrapped up inside the commands shown above.  For now, though,
88 let us return to the first example and examine the @code{\score}
89 command, leaving the others to default.
90
91 A @code{\score} block must always contain just one music 
92 expression, and
93 this must appear first within the @code{\score @{..@}} block.
94 Remember that a music expression could be anything from a single
95 note to a huge compound expression like
96
97 @example
98 @{
99   \new GrandStaff <<
100     @var{...insert the whole score of a Wagner opera in here...}
101   >>
102 @}
103 @end example
104
105 @noindent
106 Since everything is inside @code{@{ ... @}}, it counts
107 as one music expression.
108
109 As we saw previously, the @code{\score} can contain other things,
110 such as
111
112 @example
113 \score @{
114   @{ c'4 a b c' @}
115   \header @{ @}
116   \layout @{ @}
117   \midi @{ @}
118 @}
119 @end example
120
121 @noindent
122 Some people put some of those commands outside the @code{\score}
123 block -- for example, @code{\header} is often placed above the
124 @code{\score}.  That's just another shorthand that LilyPond
125 accepts.
126
127 Two more commands you have not previously seen are
128 @code{\layout @{ @}} and @code{\midi @{@}}.  If these appear as 
129 shown they will cause LilyPond to produce a printed output and a 
130 MIDI out respectively.  They are described fully in the
131 Notation Reference -- @ruser{Score layout} and 
132 @ruser{Creating MIDI files}.
133
134 The @code{\book} command allows
135 several @code{\score} blocks to be combined into one output.
136 If there are several @code{\book} blocks each one produces a
137 separate output file.  For details see @ruser{Multiple scores
138 in a book}.
139
140 @cindex variables
141
142 Another great shorthand is the ability to define variables.  All
143 the templates use this
144
145 @example
146 melody = \relative c' @{
147   c4 a b c
148 @}
149
150 \score @{
151   \melody
152 @}
153 @end example
154
155 When LilyPond looks at this file, it takes the value of
156 @code{melody} (everything after the equals sign) and inserts it
157 whenever it sees @code{\melody}.  There's nothing special about
158 the names -- it could be @code{melody}, @code{global}, 
159 @code{TimeKey},
160 @code{pianorighthand}, or @code{foofoobarbaz}.  For more details,
161 see @ruser{Saving typing with variables and functions}.
162 Remember that you can use almost any name you like as long
163 as it contains just alphabetic characters and is distinct from
164 LilyPond command names.  The exact
165 limitations on variable names are detailed in 
166 @ruser{File structure}.
167
168
169
170 @seealso
171
172 For a complete definition of the input format, see
173 @ruser{File structure}.
174
175 @node Score is a (single) compound musical expression
176 @subsection Score is a (single) compound musical expression
177
178 @cindex Compound music expression
179 @cindex Music expression, compound
180
181 We saw the general organization of LilyPond input files in the
182 previous section, @ref{How LilyPond files work}.  But we seemed to
183 skip over the most important part: how do we figure out what to
184 write after @code{\score}?
185
186 We didn't skip over it at all.  The big mystery is simply that
187 there @emph{is} no mystery.  This line explains it all:
188
189 @quotation
190 @emph{A @code{\score} must begin with a compound music expression.}
191 @end quotation
192
193 @noindent
194 You may find it useful to review
195 @ref{Music expressions explained}.  In that section, we saw how to
196 build big music expressions from small pieces -- we started from
197 notes, then chords, etc.  Now we're going to start from a big
198 music expression and work our way down.
199
200 @example
201 \score @{
202   @{ % this brace begins the overall compound music expression
203     \new GrandStaff <<
204       @var{...insert the whole score of a Wagner opera in here...}
205     >>
206   @} % this brace ends the overall compound music expression
207   \layout @{ @}
208 @}
209 @end example
210
211 A whole Wagner opera would easily double the length of this
212 manual, so let's just add a singer and piano.  We don't need a
213 @code{GrandStaff} for this ensemble, which simply groups a number
214 of staves together with a brace at the left, so we shall remove 
215 it.  We @emph{do} need a singer and a piano, though.
216
217 @example
218 \score @{
219   @{
220     <<
221       \new Staff = "singer" <<
222       >>
223       \new PianoStaff = piano <<
224       >>
225     >>
226   @}
227   \layout @{ @}
228 @}
229 @end example
230
231 Remember that we use @code{<<} and @code{>>} to show simultaneous
232 music.  And we definitely want to show the vocal part and piano
233 part at the same time, not one after the other!  However, the
234 @code{<< .. >>} construct is not really necessary for the Singer
235 staff, as it contains only one music expression, but Staves often
236 do require simultaneous Voices within them, so using @code{<< .. >>}
237 rather than braces is a good habit to adopt.
238
239 @example
240 \score @{
241   @{
242     <<
243       \new Staff = "singer" <<
244         \new Voice = "vocal" @{ @}
245         \addlyrics @{ @}
246       >>
247       \new PianoStaff = "piano" <<
248         \new Staff = "upper" @{ @}
249         \new Staff = "lower" @{ @}
250       >>
251     >>
252   @}
253   \layout @{ @}
254 @}
255 @end example
256
257 Now we have a lot more details.  We have the singer's staff: it
258 contains a @code{Voice} (in LilyPond, this term refers to a set of
259 notes, not necessarily vocal notes -- for example, a violin
260 generally plays one voice) and some lyrics.  We also have a piano
261 staff: it contains an upper staff (right hand) and a lower staff
262 (left hand).
263
264 At this stage, we could start filling in notes.  Inside the curly
265 braces next to @code{\new Voice = vocal}, we could start writing
266
267 @example
268 \relative c'' @{
269   a4 b c d
270 @}
271 @end example
272
273 But if we did that, the @code{\score} section would get pretty
274 long, and it would be harder to understand what was happening.  So
275 let's use variables instead.
276
277 @example
278 melody = \relative c'' @{ @}
279 text   = \lyricmode @{ @}
280 upper  = \relative c'' @{ @}
281 lower  = \relative c @{ @}
282 \score @{
283   @{
284     <<
285       \new Staff = "singer" <<
286         \new Voice = "vocal" @{ \melody @}
287         \addlyrics @{ \text @}
288       >>
289       \new PianoStaff = "piano" <<
290         \new Staff = "upper" @{ \upper @}
291         \new Staff = "lower" @{ \lower @}
292       >>
293     >>
294   @}
295   \layout @{ @}
296 @}
297 @end example
298
299 Be careful about the difference between notes, which are introduced
300 with @code{\relative}, and lyrics, which are introduced with
301 @code{\lyricmode}.  These are essential to tell LilyPond
302 to interpret the following content as music and text
303 respectively.
304
305 When writing (or reading) a @code{\score} section, just take it
306 slowly and carefully.  Start with the outer layer, then work on
307 each smaller layer.  It also really helps to be strict with
308 indentation -- make sure that each item on the same layer starts
309 on the same horizontal position in your text editor.
310
311
312 @node Nesting music expressions
313 @subsection Nesting music expressions
314
315 It is not essential to declare all staves at the beginning; 
316 they may be introduced temporarily at any point.  This is
317 particularly useful for creating ossia sections 
318 (see @rglos{ossia}).  Here is a simple example showing how
319 to introduce a new staff temporarily for the duration of
320 three notes:
321
322 @lilypond[verbatim,quote,ragged-right]
323 \new Staff {
324   \relative g' {
325     r4 g8 g c4 c8 d |
326     e4 r8 
327     <<
328       { f c c }
329       \new Staff {
330         f8 f c
331       }
332     >>
333     r4 |
334   }
335 }
336 @end lilypond
337
338 @noindent
339 Note that the size of the clef is the same as a clef printed
340 following a clef change -- slightly smaller than the clef
341 at the begining of the line.  This is usual for clefs printed
342 in the middle of a line.
343
344 The ossia section may be placed above the staff
345 as follows:
346
347 @lilypond[verbatim,quote,ragged-right]
348 \new Staff ="main" {
349   \relative g' {
350     r4 g8 g c4 c8 d |
351     e4 r8
352     <<
353       { f c c }
354       \new Staff \with {
355         alignAboveContext = "main" }
356       { f8 f c }
357     >>
358     r4 |
359   }
360 }
361 @end lilypond
362
363 This example uses @code{\with}, which will be explained more 
364 fully later.  It is a means of modifying the default behaviour
365 of a Staff.  Here it says that the new staff should be placed
366 above the staff called @qq{main} instead of the default position
367 which is below.
368
369 Ossia are often written without clef and without
370 time signature and are usually in a smaller font.
371 These require further commands which
372 have not yet been introduced.  See @ref{Size of objects}
373
374 @node On the un-nestedness of brackets and ties
375 @subsection On the un-nestedness of brackets and ties
376
377 You have already met a number of different types of bracket in
378 writing the input file to LilyPond.  These obey different rules
379 which can be confusing at first.  Before we explain the rules 
380 let's first review the different types of bracket.
381
382 @c attempt to force this onto a new page
383 @need 50
384 @multitable @columnfractions .3 .7
385 @headitem Bracket Type
386   @tab Function
387 @item @code{@{ .. @}}
388   @tab Encloses a sequential segment of music
389 @item @code{< .. >}
390   @tab Encloses the notes of a chord
391 @item @code{<< .. >>}
392   @tab Encloses concurrent or simultaneous sections
393 @item @code{( .. )}
394   @tab Marks the start and end of a slur
395 @item @code{\( .. \)}
396   @tab Marks the start and end of a phrase mark
397 @item @code{[ .. ]}
398   @tab Marks the start and end of a manual beam
399 @end multitable
400
401 To these we should add other constructs which generate lines
402 between or across notes: ties (marked by a tilde, @code{~}),
403 tuplets written as @code{\times x/y @{..@}}, and grace notes
404 written as @code{\grace@{..@}}.
405
406 Outside LilyPond, the conventional use of brackets requires
407 the different types to be properly nested, like this,
408 @code{<< [ @{ ( .. ) @} ] >>}, with the closing brackets being
409 encountered in exactly the opposite order to the opening
410 brackets.  This @strong{is} a requirement for the three types of
411 bracket described by the word @q{Encloses} in the table above --
412 they must nest properly.
413 However, the remaining brackets, described with the word
414 @q{Marks} in the table above together with ties and tuplets,
415 do @strong{not} have to nest
416 properly with any of the brackets.  In fact, these are not
417 brackets in the sense that
418 they enclose something -- they are simply markers to indicate
419 where something starts and ends.
420
421 So, for example, a phrasing slur can start before a manually
422 inserted beam and end before the end of the beam -- not very
423 musical, perhaps, but possible:
424
425 @lilypond[quote,verbatim,fragment,ragged-right,relative=2]
426  { g8\( a b[ c b\) a] }
427 @end lilypond
428
429 In general, different kinds of brackets, and those implied by
430 tuplets, ties and grace notes, may be mixed freely.
431 This example shows a beam extending into a tuplet (line 1),
432 a slur extending into a tuplet (line 2),
433 a beam and a slur extending into a tuplet, a tie crossing
434 two tuplets, and a phrasing slur extending out of a tuplet
435 (lines 3 and 4).
436
437 @lilypond[quote,verbatim,fragment,ragged-right]
438 {
439   r16[ g16 \times 2/3 {r16 e'8] }
440   g16( a \times 2/3 {b d) e' }
441   g8[( a \times 2/3 {b d') e'~]}
442   \times 4/5 {e'32\( a b d' e'} a'4.\)
443 }
444 @end lilypond
445
446
447 @node Voices contain music
448 @section Voices contain music
449
450 Singers need voices to sing, and so does LilyPond.
451 The actual music for all instruments in a score 
452 is contained in Voices -- the most fundamental 
453 of all LilyPond's concepts.
454
455 @menu
456 * I'm hearing Voices::          
457 * Explicitly instantiating voices::  
458 * Voices and vocals::           
459 @end menu
460
461 @node I'm hearing Voices
462 @subsection I'm hearing Voices
463
464 @cindex polyphony
465 @cindex layers
466 @cindex Voice context
467
468 The lowest, most fundamental or innermost layers in a LilyPond 
469 score are called @q{Voice contexts} or just @q{Voices} for short.
470 Voices are sometimes called @q{layers} in other notation 
471 packages.
472
473 In fact, a Voice layer or context is the only one which can
474 contain music.  If a Voice context is not explicitly declared
475 one is created automatically, as we saw at the beginning of
476 this chapter.  Some instruments such as an 
477 Oboe can play only one note at a time.  Music written for
478 such instruments is monophonic and requires just a single
479 voice.  Instruments which can play more than one note at a
480 time like the piano will often require multiple voices to
481 encode the different concurrent notes and rhythms they are
482 capable of playing.  
483
484 A single voice can contain many notes in a chord, of course,
485 so when exactly are multiple voices needed? 
486 Let us analyse a short piece of music to see how many voices
487 are required.  Here are the notes from the first two bars
488 of the second of Chopin's Deux Nocturnes, Op 32.
489
490 @c The following should appear as music without code
491 @lilypond[quote,ragged-right]
492 \new Staff \relative c'' {
493   \key aes \major
494   <<
495     { \voiceOne
496       c2 aes4. bes8 } \\
497     { \voiceTwo
498       aes2 f4 fes } \\
499     { \voiceFour
500       \once \override NoteColumn #'force-hshift = #0 <ees c>2
501       \once \override NoteColumn #'force-hshift = #0.5 des2 }
502   >> |
503   <c ees aes c>1 |
504 }
505 @end lilypond
506
507 The direction of the stems is often used to indicate the
508 continuity of two simultaneous melodic lines.  Here the
509 stems of the highest notes are all pointing up and the
510 stems of the lower notes are all pointing down.
511 This is the first indication that more than one voice
512 is required.
513
514 But the real need for multiple voices arises when notes
515 which start at the same time have different durations.
516 Look at the notes which start at beat three in the first 
517 bar.  The a-flat is a dotted quarter note, the f is a
518 quarter note and the d-flat is a half note.  These 
519 cannot be written as a chord as all the notes in a chord
520 must have the same duration.  Neither can they be written
521 as sequential notes, as they must start at the same time.
522 This section of the bar requires three voices, and the
523 normal practice would be to write the whole bar as three
524 voices, as shown here, where we have used different noteheads
525 and colors for the three voices.
526
527 @c The following should appear as music without code
528 @c The three voice styles should be defined in -init
529 @lilypond[quote,ragged-right]
530 \new Staff \relative c'' {
531   \key aes \major
532   <<
533     { \voiceOne
534       \voiceOneStyle 
535       c2 aes4. bes8 } \\
536     { \voiceTwo
537       \voiceTwoStyle
538       aes2 f4 fes } \\
539     { \voiceFour
540       \voiceThreeStyle
541       \once \override NoteColumn #'force-hshift = #0 <ees c>2
542       \once \override NoteColumn #'force-hshift = #0.5 des2 }
543   >> |
544   <c ees aes c>1 |
545 }
546 @end lilypond
547
548 Let us see how this is done.
549
550 @funindex \\
551
552 The easiest way to enter fragments with more than one voice on a
553 staff is to enter each voice as a sequence (with @code{@{...@}}),
554 and combine them simultaneously with angle brackets, @code{<<...>>}.
555 The fragments must also be separated with double backward slashes, 
556 @code{\\}, to place them in separate voices.  Without these, the 
557 notes would be entered into a single voice, which would usually 
558 cause errors.  This technique is particularly suited to pieces of
559 music which are largely monophonic with occasional short sections
560 of polyphony.  Here's a simple example:
561
562 @lilypond[quote,verbatim,fragment,ragged-right,relative=2]
563 \key d \minor
564 %    Voice "1"             Voice "2"
565 << { r4 g g4. a8 }    \\ { d,2 d4 g }       >> |
566 << { bes4 bes c bes } \\ { g4 g g8( a) g4 } >> |
567 << { a2. r4 }         \\ { fis2. s4 }       >>
568 @end lilypond
569
570 It is not necessary to use a separate @code{<< \\ >>} construct
571 for each bar. For music with few notes in each bar this layout
572 can help the legibility of the code, but if there are many
573 notes in each bar it may be better to split out each voice
574 separately, like this:
575
576 @lilypond[quote,verbatim,fragment,ragged-right,relative=2]
577 \key d \minor
578 << {
579   % Voice "1"
580   r4 g g4. a8 |
581   bes4 bes c bes |
582   a2. r4 |
583 } \\ {
584   % Voice "2"
585   d,2 d4 g |
586   g4 g g8( a) g4 |
587   fis2. s4 |
588 } >>
589 @end lilypond
590
591
592 This example has just two voices, but the same contruct may be
593 used to encode three or more voices by adding more back-slash
594 separators.
595
596 The Voice contexts bear the names @code{"1"}, @code{"2"}, etc.  
597 In each of these contexts, the vertical direction of slurs, 
598 stems, ties, dynamics etc., is set appropriately.
599
600 @lilypond[quote,verbatim,fragment]
601 \new Staff \relative c' {
602   % Main voice
603   c16 d e f
604   %    Voice "1"     Voice "2"                 Voice "3"
605   << { g4 f e } \\ { r8 e4 d c8 ~ } >> |
606   << { d2 e2 }  \\ { c8 b16 a b8 g ~ g2 } \\ { s4 b4 c2 } >> |
607 }
608 @end lilypond
609
610 These voices are all separate from the main voice that contains
611 the notes just outside the @code{<< .. >>} construct.  Let's call
612 this the @emph{simultaneous construct}.  Slurs and ties may only
613 connect notes within the same voice, so slurs and ties cannot go
614 into or out of a simultaneous construct.  Conversely,
615 parallel voices from separate simultaneous constructs on the same
616 staff are the same voice.  Other voice-related properties also
617 carry across simultaneous constructs.  Here is the same example,
618 with different colors and noteheads for each voice.  Note that
619 changes in one Voice do not affect other voices, but they do
620 persist in the same Voice later.  Note also that tied notes may be
621 split across the same voices in two constructs, shown here in the
622 blue triangle voice.
623
624 @lilypond[quote,verbatim]
625 \new Staff \relative c' {
626   \voiceOneStyle
627   c16 d e f
628   << % Bar 1
629     { g4 f e } \\
630     {
631       \voiceTwoStyle
632       r8 e4 d c8 ~
633     }
634   >> |
635   << % Bar 2
636     { d2 e2 } \\
637     { c8 b16 a b8 g ~ g2 } \\
638     {
639       \voiceThreeStyle
640       s4 b4 c2
641      }
642   >>
643 }
644 @end lilypond
645
646 The commands @code{\voiceXXXStyle} are mainly intended for use in
647 educational documents such as this one.  They modify the color
648 of the notehead, the stem and the beams, and the style of the
649 notehead, so that the voices may be easily distinguished.
650 Voice one is set to red diamonds, voice two to blue triangles,
651 voice three to green crossed circles, and voice four (not used
652 here) to magenta crosses.  We shall see later how commands like 
653 these may be created by the user.  
654 See @ref{Visibility and color of objects}
655 TODO Add link to using variables for tweaks
656
657 Polyphony does not change the relationship of notes within a
658 @code{\relative @{ @}} block.  Each note is still calculated 
659 relative to the note immediately preceding it, or to the first 
660 note of the preceding chord.  So in
661
662 @example
663 \relative c' @{ noteA << < noteB noteC > \\ noteD >> noteE @}
664 @end example
665
666 @noindent
667 @code{noteB} is relative to @code{noteA}                      @*
668 @code{noteC} is relative to @code{noteB}, not @code{noteA};   @*
669 @code{noteD} is relative to @code{noteB}, not @code{noteA} or
670 @code{noteC}.                                                 @*
671 @code{noteE} is relative to @code{noteD}, not @code{noteA}
672
673 We are now in a position to return to the first example from
674 the Chopin Nocturne to see how this might be encoded.  As we
675 shall see, this encounters some difficulties.  We begin as
676 we have learnt, using the @code{<< \\  >>} construct to 
677 enter the music of the first bar in three voices:
678
679 @lilypond[quote,verbatim,fragment,ragged-right]
680 \new Staff \relative c'' {
681   \key aes \major
682   << 
683     { c2 aes4. bes8 } \\ { aes2 f4 fes } \\ { <ees c>2 des2 }
684   >> |
685   <c ees aes c>1 |
686 }
687 @end lilypond
688
689 @cindex stem down
690 @cindex stem up
691 @funindex \voiceOne
692 @funindex \voiceTwo
693 @funindex \voiceThree
694 @funindex \voiceFour
695
696 The stem directions are automatically assigned with the
697 odd-numbered voices taking upward stems and the even-numbered
698 voices downward ones.  The stems for voices 1 and 2 are right,
699 but the stems in voice 3 should go down in this particular piece
700 of music.  We can correct this
701 by telling LilyPond that this third voice is really a fourth
702 voice, with stems going down, using the @code{\voiceFour} 
703 command.  There are also corresponding @code{\voiceOne},
704 @code{\voiceTwo}, and @code{\voiceThree} 
705 commands.  This results in the following:
706
707 @lilypond[quote,verbatim,fragment,ragged-right]
708 \new Staff \relative c'' {
709   \key aes \major
710   << 
711     { c2 aes4. bes8 } \\ 
712     { aes2 f4 fes   } \\ 
713     { \voiceFour <ees c>2 des2 }
714   >> |
715   <c ees aes c>1 |
716 }
717 @end lilypond
718
719 @noindent
720 and exposes a problem commonly encountered with multiple
721 voices: the stems of notes can collide with noteheads
722 in other voices.  In laying out the notes, LilyPond allows the
723 notes or chords from two voices to occupy the same vertical
724 note column provided the stems are in opposite directions, but
725 the notes from a third voice are displaced to avoid the stems
726 colliding.  This often works well, but in this example the
727 notes of the third voice are clearly not well placed by default.
728 LilyPond provides several ways to adjust the horizontal placing
729 of notes.  We are not quite ready yet to see how to correct this,
730 so we shall leave this problem until a later section 
731 (see @ref{Fixing overlapping notation} )
732
733 @node Explicitly instantiating voices
734 @subsection Explicitly instantiating voices
735
736 Voice contexts can also be created manually
737 inside a @code{<< >>} block to create polyphonic music, using
738 @code{\voiceOne} ... @code{\voiceFour} to indicate the required
739 directions of stems, slurs, etc.  In longer scores this method
740 is clearer, as it permits the voices to be separated and to be
741 given more descriptive names.
742
743 Specifically, the construct @code{<< \\ >>} which we used in
744 the previous section:
745
746 @example
747 \new Staff @{
748   \relative c' @{
749     << @{ e4 f g a @} \\ @{ c,4 d e f @} >>
750   @}
751 @}
752 @end example
753
754 @noindent
755 is equivalent to 
756
757 @example
758 \new Staff <<
759   \new Voice = "1" @{ \voiceOne \relative c' @{ e4 f g a @} @}
760   \new Voice = "2" @{ \voiceTwo \relative c' @{ c4 d e f @} @}
761 >>
762 @end example
763
764 Both of the above would produce
765
766 @c The following example should not display the code
767 FIXME: weird compiling bug!  Leave for gp.
768 @c @lilypond{ragged-right,quote]
769 @c \relative c' {
770 @c %  << { e4 f g a } \\ { c,4 d e f } >>
771 @c }
772 @c @end lilypond
773
774 The @code{\voiceXXX} commands set the direction of stems, slurs,
775 ties, articulations, text annotations, augmentation dots of dotted
776 notes, and fingerings.  @code{\voiceOne} and @code{\voiceThree}
777 make these objects point upwards, while @code{\voiceTwo} and
778 @code{\voiceFour} make them point downwards.  These commands also
779 generate a horizontal shift for each voice when this is required 
780 to avoid clashes of note heads.  The command @code{\oneVoice} 
781 reverts the settings back to the normal values for a single voice.
782
783 Let us see in some simple examples exactly what effect 
784 @code{\oneVoice}, @code{\voiceOne} and @code{voiceTwo} have on
785 markup, ties, slurs, and dynamics:
786
787 @lilypond[quote,ragged-right,verbatim]
788 \relative c'{ 
789   c-"default" d8 ~ d e4 ( f g a ) b-> c
790 }
791 @end lilypond
792
793 @lilypond[quote,ragged-right,verbatim]
794 \relative c'{
795   \voiceOne
796   c d8 ~ d e4 ( f g a ) b-> c
797   \oneVoice
798   c, d8 ~ d e4 ( f g a ) b-> c
799 }
800 @end lilypond
801
802 @lilypond[quote,ragged-right,verbatim]
803 \relative c'{
804   \voiceTwo
805   c d8 ~ d e4 ( f g a ) b-> c
806   \oneVoice
807   c, d8 ~ d e4 ( f g a ) b-> c
808 }
809 @end lilypond
810
811 An expression that appears directly inside a @code{<< >>} belongs
812 to the main voice (but, note, @strong{not} in a @code{<< \\ >>}
813 construct).  This is useful when extra voices appear while the
814 main voice is playing.  Here is a more correct rendition of the
815 example from the previous section.  The red diamond-shaped notes
816 demonstrate that the main melody is now in a single voice context,
817 permitting a phrasing slur to be drawn over them.
818
819 @lilypond[quote,ragged-right,verbatim]
820 \new Staff \relative c' {
821   \voiceOneStyle
822   % The following notes are monophonic
823   c16^( d e f
824   % Start simultaneous section of three voices
825   <<
826     % Continue the main voice in parallel
827     { g4 f e | d2 e2) }
828     % Initiate second voice
829     \new Voice {
830       % Set stems, etc, down
831       \voiceTwo
832       r8 e4 d c8 ~ | c8 b16 a b8 g ~ g2
833     }
834     % Initiate third voice
835     \new Voice {
836       % Set stems, etc, up
837       \voiceThree
838       s2. | s4 b4 c2
839     }
840   >>
841 }
842 @end lilypond
843
844 @cindex nesting music expressions
845 @cindex nesting simultaneous constructs
846
847 More deeply nested polyphony constructs are possible, and if a
848 voice appears only briefly this might be a more natural way to
849 typeset the music.
850
851 @lilypond[quote,ragged-right,verbatim]
852 \new Staff \relative c' {
853   c16^( d e f
854   <<
855     { g4 f e | d2 e2) }
856     \new Voice {
857       \voiceTwo
858       r8 e4 d c8 ~ |
859       <<
860         {c8 b16 a b8 g ~ g2}
861         \new Voice {
862           \voiceThree
863           s4 b4 c2
864         }
865       >>
866     }
867   >>
868 }
869 @end lilypond
870
871
872 This method of nesting new voices briefly is useful
873 when only small sections of the music
874 are polyphonic, but when the whole staff is largely polyphonic
875 it can be clearer to use multiple voices throughout, using
876 spacing notes to step over sections where the voice is silent,
877 as here:
878
879 @lilypond[quote,ragged-right,verbatim]
880 \new Staff \relative c' <<
881   % Initiate first voice
882   \new Voice {
883     \voiceOne
884     c16^( d e f g4 f e | d2 e2) |
885   }
886   % Initiate second voice
887   \new Voice {
888     % set stems, etc down
889     \voiceTwo
890     s4 r8 e4 d c8 ~ | c8 b16 a b8 g ~ g2 |
891   }
892   % Initiate third voice
893   \new Voice {
894     % set stems, etc up
895     \voiceThree
896     s1 | s4 b4 c2 |
897   }
898 >>
899 @end lilypond
900
901 @cindex note column
902 @cindex shift commands
903 @funindex \shiftOff
904 @funindex \shiftOn
905 @funindex \shiftOnn
906 @funindex \shiftOnnn
907
908 Closely spaced notes in a chord, or notes occuring at the same
909 time in different voices, are arranged in two, occasionally more,
910 columns to prevent the noteheads overlapping.  These are called
911 note columns.  There are separate columns for each voice, and 
912 the currently specified voice-dependent shift is applied to the 
913 note column if there would otherwise be a collision.
914
915 The @code{\shiftOn}, @code{\shiftOnn}, @code{\shiftOnnn}, and
916 @code{\shiftOff} commands specify the degree to which notes and
917 chords of the voice should be shifted if a collision
918 would otherwise occur. By default, the outer voices (normally 
919 voices one and two) have @code{\shiftOff} specified, while the 
920 inner voices (three and four) have @code{\shiftOn} specified.  
921 When a shift is applied, Voices one and three are shifted to 
922 the right and voices two and four to the left.
923
924 @code{\shiftOnn} and @code{\shiftOnnn} define further shift 
925 levels which may be specified temporarily to resolve collisions 
926 in complex situations -- see
927 @c FIXME failed link
928 @c @ref{Real music example}.
929
930
931 @node Voices and vocals
932 @subsection Voices and vocals
933
934 Vocal music presents a special difficulty: we need to combine two
935 expressions -- notes and lyrics.
936
937 You have already seen the @code{\addlyrics@{@}} command, which
938 handles simple scores well.  However, this technique is
939 quite limited.  For more complex music, you must introduce the
940 lyrics in a @code{Lyrics} context using @code{\new Lyrics} and
941 explicitly link
942 the lyrics to the notes with @code{\lyricsto@{@}}, using the
943 name assigned to the Voice.
944
945 @lilypond[quote,verbatim,fragment]
946 <<
947   \new Voice = "one" \relative c'' {
948     \autoBeamOff
949     \time 2/4
950     c4 b8. a16 g4. f8 e4 d c2
951   }
952   \new Lyrics \lyricsto "one" {
953     No more let sins and sor -- rows grow.
954   }
955 >>
956 @end lilypond
957
958 The automatic beaming which LilyPond uses by default works well
959 for instrumental music, but not so well for music with lyrics,
960 where beaming is either not required at all or is used to indicate
961 melismata in the lyrics.  In the example above we use the command
962 @code{\autoBeamOff} to turn off the automatic beaming.
963
964 Let us reuse the earlier example from Judas Maccabæus to 
965 illustrate this more flexible technique.  We first recast
966 it to use variables so the music and lyrics can be separated
967 from the staff structure.  We also introduce a ChoirStaff 
968 bracket.  The lyrics themselves must be introduced with 
969 @code{\lyricmode} to ensure they are interpreted as lyrics
970 rather than music.
971
972 @lilypond[quote,verbatim]
973 global = { \time 6/8 \partial 8 \key f \major}
974 SopOneMusic = \relative c'' {
975   c8 | c([ bes)] a a([ g)] f | f'4. b, | c4.~ c4 }
976 SopTwoMusic = \relative c' {
977   r8 | r4. r4 c8 | a'([ g)] f f([ e)] d | e([ d)] c bes' }
978 SopOneLyrics = \lyricmode {
979   Let | flee -- cy flocks the | hills a -- dorn, __ }
980 SopTwoLyrics = \lyricmode {
981   Let | flee -- cy flocks the | hills a -- dorn, }
982
983 \score {
984   \new ChoirStaff <<
985     \new Staff <<
986       \new Voice = "SopOne" {
987         \global
988         \SopOneMusic
989       }
990       \new Lyrics \lyricsto "SopOne" {
991         \SopOneLyrics
992       }
993     >>
994     \new Staff <<
995       \new Voice = "SopTwo" {
996         \SopTwoMusic
997       }
998       \new Lyrics \lyricsto "SopTwo" {
999         \SopTwoLyrics
1000       }
1001     >>
1002   >>
1003 }
1004 @end lilypond
1005
1006 This is the basic structure of all vocal scores.  More staves may
1007 be added as required, more voices may be added to the staves,
1008 more verses may be added to the lyrics,
1009 and the variables containing the music can easily be placed
1010 in separate files should they become too long.
1011
1012 Here is a final example of the first line of a hymn with four
1013 verses, set for SATB.  In this case the words for all four
1014 parts are the same.
1015
1016 @lilypond[quote,verbatim]
1017 TimeKey = { \time 4/4 \partial 4 \key c \major}
1018 SopMusic   = \relative c' { c4 | e4. e8 g4  g  | a a g }
1019 AltoMusic  = \relative c' { c4 | c4. c8 e4  e  | f f e }
1020 TenorMusic = \relative c  { e4 | g4. g8 c4. b8 | a8 b c d e4 }
1021 BassMusic  = \relative c  { c4 | c4. c8 c4  c  | f8 g a b c4 }
1022 VerseOne   = \lyricmode { 
1023   E -- | ter -- nal fa -- ther, | strong to save, }
1024 VerseTwo   = \lyricmode { 
1025   O | Christ, whose voice the | wa -- ters heard, }
1026 VerseThree = \lyricmode { 
1027   O | Ho -- ly Spi -- rit, | who didst brood }
1028 VerseFour  = \lyricmode { 
1029   O | Tri -- ni -- ty of | love and pow'r }
1030
1031 \score {
1032   \new ChoirStaff <<
1033     \new Staff <<
1034       \clef "treble"
1035       \new Voice = "Sop"  { \voiceOne \TimeKey \SopMusic }
1036       \new Voice = "Alto" { \voiceTwo \AltoMusic }
1037       \new Lyrics \lyricsto "Sop" { \VerseOne   }
1038       \new Lyrics \lyricsto "Sop" { \VerseTwo   }
1039       \new Lyrics \lyricsto "Sop" { \VerseThree }
1040       \new Lyrics \lyricsto "Sop" { \VerseFour  }
1041     >>
1042     \new Staff <<
1043       \clef "bass"
1044       \new Voice = "Tenor" { \voiceOne \TenorMusic }
1045       \new Voice = "Bass"  { \voiceTwo \BassMusic  }
1046     >>
1047   >>
1048 }
1049 @end lilypond
1050
1051 @node Contexts and engravers
1052 @section Contexts and engravers
1053
1054 Contexts and engravers have been mentioned informally
1055 in earlier sections; we now must look at 
1056 these concepts in more detail, as they are important
1057 in the fine-tuning of LilyPond output.
1058
1059
1060 @menu
1061 * Contexts explained::          
1062 * Creating contexts::           
1063 * Engravers explained::         
1064 * Modifying context properties::  
1065 * Adding and removing engravers::  
1066 @end menu
1067
1068 @node Contexts explained
1069 @subsection Contexts explained
1070
1071 When music is printed, many notational elements which do not 
1072 appear explicitly in the input file must be added to the
1073 output.  For example, compare the input and output of the 
1074 following example:
1075
1076 @lilypond[quote,verbatim,relative=2,fragment]
1077 cis4 cis2. g4
1078 @end lilypond
1079
1080 The input is rather sparse, but in the output, bar lines, 
1081 accidentals, clef, and time signature have been added.  When 
1082 LilyPond @emph{interprets} the input the musical information 
1083 is inspected in time order, similar to reading a score from left 
1084 to right.  While reading the input, the program remembers where 
1085 measure boundaries are, and which pitches require explicit 
1086 accidentals.  This information must be held on several levels.  
1087 For example, the effect of an accidental is limited
1088 to a single staff, while a bar line must be synchronized across 
1089 the entire score.
1090
1091 Within LilyPond, these rules and bits of information are grouped
1092 in @emph{Contexts}.  We have already met the 
1093 @code{Voice} context. 
1094 Others are the @code{Staff} and @code{Score} contexts.  
1095 Contexts are hierarchical to reflect the heirarchical nature of 
1096 a musical score.  
1097 For example: a @code{Staff} context can contain many 
1098 @code{Voice} contexts, and a @code{Score} context can 
1099 contain many @code{Staff} contexts.
1100
1101 @quotation
1102 @image{context-example,5cm,,}
1103 @end quotation
1104
1105 Each context has the responsibility for enforcing some notation rules,
1106 creating some notation objects and maintaining the associated
1107 properties.  For example, the @code{Voice} context may introduce an
1108 accidental and then the @code{Staff} context maintains the rule to
1109 show or suppress the accidental for the remainder of the measure.
1110
1111 As another example, the synchronization of bar lines is, by default, 
1112 handled in the @code{Score} context.
1113 However, in some music we may not want the bar lines to be
1114 synchronized -- consider a polymetric score in 4/4 and 3/4 time.
1115 In such cases, we must modify the default settings of the 
1116 @code{Score} and @code{Staff} contexts.
1117
1118 For very simple scores, contexts are created implicitly, and you need
1119 not be aware of them.  For larger pieces, such as anything with more
1120 than one staff, they must be
1121 created explicitly to make sure that you get as many staves as you
1122 need, and that they are in the correct order.  For typesetting pieces
1123 with specialized notation, it is usual to modify existing, or
1124 even to define totally new, contexts.
1125
1126 In addition to the @code{Score,} @code{Staff} and 
1127 @code{Voice} contexts there are contexts which fit between
1128 the score and staff levels to control staff groups, such as the
1129 @code{PianoStaff} and @code{ChoirStaff} contexts.  There
1130 are also alternative staff and voice contexts, and contexts for
1131 lyrics, percussion, fret boards, figured bass, etc.  A complete
1132 list is shown in the Notation Reference.
1133 TODO: Add link
1134
1135 The names of all context types are formed from one or more 
1136 words, each word being capitalised and joined immediately to the 
1137 preceding word with no hyphen or underscore, e.g., 
1138 @code{GregorianTranscriptionStaff}.
1139
1140 @node Creating contexts
1141 @subsection Creating contexts
1142
1143 There can be only one top level context: the 
1144 @code{Score} 
1145 context.  This is created with the @code{\score} command, 
1146 or, in simple scores, it is created automatically.
1147
1148 For scores with only one voice and one staff, the 
1149 @code{Voice} and @code{Staff} contexts may be left to be 
1150 created automatically, but for more complex scores it is 
1151 necessary to create them by hand.  
1152 The simplest command that does this is @code{\new}.  
1153 It is prepended to a music expression, for example
1154
1155 @funindex \new
1156 @cindex new contexts
1157 @cindex Context, creating
1158
1159 @example
1160 \new @var{type} @var{music-expression}
1161 @end example
1162
1163 @noindent
1164 where @var{type} is a context name (like @code{Staff} or
1165 @code{Voice}).  This command creates a new context, and starts
1166 interpreting the @var{music-expression} within that context.
1167
1168 Note that there is no @code{\new Score} command;
1169 the single top-level @code{Score} context is introduced 
1170 with @code{\score}.  
1171
1172 The @code{\new} command may also give a identifying name to the 
1173 context to distinguish it from other contexts of the same type,
1174
1175 @example
1176 \new @var{type} = @var{id} @var{music-expression}
1177 @end example
1178
1179 Note the distinction between the name of the context type,
1180 @code{Staff}, @code{Voice}, etc, and
1181 the identifying name of a particular instance of that type,
1182 which can be any sequence of letters invented by the user.
1183 The identifying name is used to refer back to that particular
1184 instance of a context.  We saw this in use in the section on 
1185 lyrics in @ref{Voices and vocals}.
1186
1187
1188 @node Engravers explained
1189 @subsection Engravers explained
1190
1191 @cindex engravers
1192
1193 Every mark on the printed output of a score produced by LilyPond
1194 is produced by an @code{Engraver}.  Thus there is an engraver
1195 to print staves, one to print noteheads, one for stems, one for
1196 beams, etc, etc.  In total there are over 120 such engravers!
1197 Fortunately, for most scores it is not necessary to know about 
1198 more than a few, and for simple scores you do not need to know 
1199 about any.  
1200
1201 Engravers live and operate in Contexts.
1202 Engravers such as the @code{Metronome_mark_engraver}, whose
1203 action and output applies to the score as a whole, operate in
1204 the highest level context -- the @code{Score} context.
1205
1206 The @code{Clef_engraver} and @code{Key_engraver} are to be
1207 found in every Staff Context, as different staves may require 
1208 different clefs and keys.
1209
1210 The @code{Note_heads_engraver} and @code{Stem_engraver} live
1211 in each @code{Voice} context, the lowest level context of all.
1212
1213 Each engraver processes the particular objects associated
1214 with its function, and maintains the properties that relate
1215 to that function.  These properties, like the properties
1216 associated with contexts, may be modified to change the
1217 operation of the engraver or the appearance of those elements
1218 in the printed score.
1219    
1220 Engravers all have compound names formed from words which
1221 describe their function.  Just the first word is capitalised, 
1222 and the remainder are joined to it with underscores.  Thus
1223 the @code{Staff_symbol_engraver} is responsible for creating the
1224 lines of the staff, the @code{Clef_engraver} determines and sets
1225 the pitch reference point on the staff by drawing a clef symbol.
1226
1227 Here are some of the most common engravers together with their
1228 function.  You will see it is easy to guess the function from 
1229 the name, or vice versa.
1230
1231 @multitable @columnfractions .3 .7 
1232 @headitem Engraver
1233   @tab Function
1234 @item Accidental_engraver
1235   @tab Makes accidentals, cautionary and suggested accidentals
1236 @item Beam_engraver
1237   @tab Engraves beams
1238 @item Clef_engraver
1239   @tab Engraves clefs
1240 @item Dynamic_engraver
1241   @tab Creates hairpins and dynamic texts
1242 @item Key_engraver
1243   @tab Creates the key signature
1244 @item Metronome_mark_engraver
1245   @tab Engraves metronome marking
1246 @item Note_heads_engraver
1247   @tab Engraves noteheads
1248 @item Rest_engraver
1249   @tab Engraves rests
1250 @item Staff_symbol_engraver
1251   @tab Engraves the five (by default) lines of the staff
1252 @item Stem_engraver
1253   @tab Creates stems and single-stem tremulos
1254 @item Time_signature_engraver
1255   @tab Creates time signatures
1256 @end multitable
1257
1258 @smallspace
1259
1260 We shall see later how the output of LilyPond can be changed
1261 by modifying the action of Engravers.
1262   
1263
1264 @node Modifying context properties
1265 @subsection Modifying context properties
1266
1267 @cindex context properties
1268 @funindex \set
1269 @funindex \unset
1270
1271 Contexts are responsible for holding the values of a number of
1272 context @emph{properties}.  Many of them can be changed to
1273 influence the interpretation of the input and so change the
1274 appearance of the output.  They are changed by the 
1275 @code{\set} command.  This takes the form
1276
1277 @example
1278 \set @emph{ContextName}.@emph{propertyName} = #@emph{value}
1279 @end example
1280
1281 Where the @emph{ContextName} is usually @code{Score},
1282 @code{Staff} or @code{Voice}.  It may be omitted,
1283 in which case @code{Voice} is assumed.
1284
1285 The names of context properties consist of words joined
1286 together with no hyphens or underscores, all except the
1287 first having a capital letter.  Here are a few examples
1288 of some commonly used ones.  There are many more.
1289
1290 @c attempt to force this onto a new page
1291 @need 50
1292 @multitable @columnfractions .25 .2 .4 .15
1293 @headitem propertyName
1294   @tab Value
1295   @tab Function
1296   @tab Example
1297 @item extraNatural
1298   @tab Boolean
1299   @tab If true, set extra natural signs before accidentals
1300   @tab @code{#t}, @code{#f}
1301 @item currentBarNumber
1302   @tab Integer
1303   @tab Set the current bar number
1304   @tab @code{50}
1305 @item doubleSlurs
1306   @tab Boolean
1307   @tab If true, print slurs both above and below notes
1308   @tab @code{#t}, @code{#f}
1309 @item instrumentName
1310   @tab Text
1311   @tab Set the name to be placed at the start of the staff
1312   @tab @code{"Cello I"}
1313 @item fontSize
1314   @tab Real
1315   @tab Increase or decrease the font size
1316   @tab @code{2.4}
1317 @item stanza
1318   @tab Text
1319   @tab Set the text to print before the start of a verse
1320   @tab @code{"2"}
1321 @end multitable
1322 @noindent
1323 where a Boolean is either True (@code{#t}) or False (@code{#f}),
1324 an Integer is a positive whole number, a Real is a positive
1325 or negative decimal number, and text is enclosed in double
1326 apostrophes.  Note the occurrence of hash signs,
1327 (@code{#}), in two different places -- as part of the Boolean 
1328 value before the @code{t} or @code{f}, and before @emph{value}
1329 in the @code{\set} statement.  So when a Boolean is being 
1330 entered you need to code two hash signs, e.g., @code{##t}.  
1331
1332 Before we can set any of these properties we need to know
1333 in which context they operate.  Sometimes this is obvious,
1334 but occasionally it can be tricky.  If the wrong context
1335 is specified, no error message is produced, but the expected
1336 action will not be taken.  For example, the 
1337 @code{instrumentName} clearly lives in the Staff context, since
1338 it is the staff that is to be named.
1339 In this example the first staff is labelled, but not the second,
1340 because we omitted the context name.
1341
1342 @lilypond[quote,verbatim,ragged-right]
1343 <<
1344   \new Staff \relative c'' {
1345     \set Staff.instrumentName = "Soprano"
1346     c4 c
1347  }
1348   \new Staff \relative c' {
1349   \set instrumentName = "Alto"  % Wrong!
1350   d4 d 
1351  }
1352 >>
1353 @end lilypond
1354
1355 Remember the default context name is Voice, so the second
1356 @code{\set} command set the property @code{instrumentName} in the
1357 Voice context to @qq{Alto}, but as LilyPond does not look
1358 for any such property in the @code{Voice} context, no 
1359 further action took place.  This is not an error, and no error 
1360 message is logged in the log file.
1361
1362 Similarly, if the property name is mis-spelt no error message 
1363 is produced, and clearly the expected action cannot be performed.
1364 If fact, you can set any (fictitious) @q{property} using any 
1365 name you like in any context that exists by using the 
1366 @code{\set} command.  But if the name is not
1367 known to LilyPond it will not cause any action to be taken.
1368 This is one of the reasons why it is highly recommended to
1369 use a context-sensitive editor with syntax highlighting for
1370 editing LilyPond files, such as Vim, Jedit, ConTEXT or Emacs,
1371 since unknown property names will be highlighted differently.
1372
1373 The @code{instrumentName} property will take effect only
1374 if it is set in the @code{Staff} context, but
1375 some properties can be set in more than one context.
1376 For example, the property @code{extraNatural} is by
1377 default set to ##t (true) for all staves.  
1378 If it is set to ##f (false) in the @code{Staff} context 
1379 it applies just to the accidentals on that staff.  
1380 If it is set to false in the @code{Score} context
1381 it applies to all staves.
1382
1383 So this turns off extra naturals in one staff:
1384
1385 @lilypond[quote,verbatim,ragged-right]
1386 <<
1387   \new Staff \relative c'' {
1388     ais4 aes
1389  }
1390   \new Staff \relative c'' {
1391     \set Staff.extraNatural = ##f
1392     ais4 aes
1393  }
1394 >>
1395 @end lilypond
1396
1397 @noindent
1398 and this turns them off in all staves:
1399
1400 @lilypond[quote,verbatim,ragged-right]
1401 <<
1402   \new Staff \relative c'' {
1403     ais4 aes
1404  }
1405   \new Staff \relative c'' {
1406     \set Score.extraNatural = ##f
1407     ais4 aes
1408  }
1409 >>
1410 @end lilypond
1411
1412 The value of every property set in this way can be reset
1413 to its original value with the @code{\unset} command.
1414  
1415 The @code{\set} and @code{\unset} commands can appear anywhere
1416 in the input file and will take effect from the time they are
1417 encountered until the end of the score or until the property is 
1418 @code{\set} or @code{\unset} again.  Let's try changing the 
1419 font size, which affects the size of the note heads (among 
1420 other things) several times.
1421
1422 @lilypond[quote,verbatim,ragged-right,relative=1,fragment]
1423 c4 
1424 % make noteheads smaller
1425 \set fontSize = #-4
1426 d e
1427 % make noteheads larger
1428 \set fontSize = #2.5
1429 f g
1430 % return to original size
1431 \unset fontSize
1432 a b
1433 @end lilypond
1434
1435 We have now seen how to set the values of several different
1436 types of property.  Note that integers and numbers are alway 
1437 preceded by a hash sign, @code{#}, while a true or false value 
1438 is specified by ##t and ##f, with two hash signs.  A text 
1439 property should be enclosed in double quotation signs, as above, 
1440 although we shall see later that text can actually be specified
1441 in a much more general way by using the very powerful 
1442 @code{markup} command. 
1443
1444
1445 @funindex \with
1446
1447 Context properties may also be set at the time the context is
1448 created.  Sometimes this is a clearer way of specifying a 
1449 property value if it is to remain fixed for the duration of
1450 the context.  When a context is created with a @code{\new}
1451 command it may be immediately followed by a 
1452 @code{\with @{ .. @}} block in which the property values are
1453 set.  For example, if we wish to suppress the printing of
1454 extra naturals for the duration of a staff we would write:
1455
1456 @lilypond[quote,verbatim,ragged-right]
1457 \new Staff \with {
1458   extraNatural = ##f
1459 }
1460 \relative c' {
1461   gis ges aes ais
1462 }
1463 @end lilypond
1464
1465 In effect this overrides the default value of the property.  It
1466 may still be changed dynamically using @code{\set} and 
1467 @code{\unset}.
1468
1469 @node Adding and removing engravers
1470 @subsection Adding and removing engravers
1471
1472 @cindex Engravers, adding
1473 @cindex Engravers, removing
1474
1475 @funindex \consists
1476 @funindex \remove
1477
1478 We have seen that contexts each contain several engravers, each
1479 of which is responsible for producing a particular part of the
1480 output, like barlines, staves, note heads, stems, etc.  If an
1481 engraver is removed from a context it can no longer produce its
1482 output.  This is a crude way of modifying the output, but it
1483 can sometimes be useful.  
1484
1485 To remove an engraver we can use the @code{\with} command placed
1486 immediately after the context creation command, as in the 
1487 previous section.
1488
1489 As an 
1490 illustration let's repeat an example from the previous 
1491 section with the staff lines removed.  Remember that the 
1492 staff lines are produced by the Staff_symbol_engraver.
1493
1494 @lilypond[quote,verbatim,ragged-right]
1495 \new Staff \with {
1496   \remove Staff_symbol_engraver
1497 }
1498 \relative c' {
1499   c4 
1500   \set fontSize = #-4  % make noteheads smaller
1501   d e
1502   \set fontSize = #2.5  % make noteheads larger
1503   f g
1504   \unset fontSize  % return to original size
1505   a b
1506 }
1507 @end lilypond
1508
1509 @cindex ambitus engraver
1510
1511 Engravers can also be added to contexts.  The command
1512 to do this is 
1513
1514 @code{\consists @emph{Engraver_name}},
1515
1516 placed inside a @code{\with} block.  Some vocal scores
1517 have an @rglos{ambitus} placed at the beginning of a
1518 staff to indicate the range of notes in that staff.
1519 The ambitus is produced by the @code{Ambitus_engraver},
1520 which is not normally included in any context.  If
1521 we add it to the @code{Voice} context it calculates
1522 the range from that voice only:
1523
1524 @lilypond[quote,verbatim,ragged-right]
1525 \new Staff <<
1526   \new Voice \with {
1527     \consists Ambitus_engraver
1528   }
1529   \relative c'' { 
1530     \voiceOne
1531     c a b g 
1532   }
1533   \new Voice
1534   \relative c' {
1535     \voiceTwo
1536     c e d f
1537   }
1538 >>
1539 @end lilypond
1540
1541 @noindent
1542 but if we add the Ambitus engraver to the 
1543 @code{Staff} context it calculates the range from all
1544 the notes in all the voices on that staff:
1545
1546 @lilypond[quote,verbatim,ragged-right]
1547 \new Staff \with {
1548     \consists Ambitus_engraver
1549   }
1550   <<
1551   \new Voice
1552   \relative c'' { 
1553     \voiceOne
1554     c a b g 
1555   }
1556   \new Voice
1557   \relative c' {
1558     \voiceTwo
1559     c e d f
1560   }
1561 >>
1562 @end lilypond
1563
1564
1565 @node Extending the templates
1566 @section Extending the templates
1567
1568 You've read the tutorial, you know how to write music, you 
1569 understand the fundamental concepts.  But how can you
1570 get the staves that you want?  Well, you can find lots of 
1571 templates (see @ref{Templates}) which may give you a start.  
1572 But what
1573 if you want something that isn't covered there?  Read on.
1574
1575 TODO Add links to templates after they have been moved to LSR
1576
1577 @menu
1578 * Soprano and cello::           
1579 * Four-part SATB vocal score::  
1580 * Building a score from scratch::  
1581 @end menu
1582
1583 @node Soprano and cello
1584 @subsection Soprano and cello
1585
1586 Start off with the template that seems closest to what you want to end
1587 up with.  Let's say that you want to write something for soprano and
1588 cello.  In this case, we would start with @q{Notes and lyrics} (for the
1589 soprano part).
1590
1591 @example
1592 \version "2.11.23"
1593 melody = \relative c' @{
1594   \clef treble
1595   \key c \major
1596   \time 4/4
1597
1598   a4 b c d
1599 @}
1600
1601 text = \lyricmode @{
1602   Aaa Bee Cee Dee
1603 @}
1604
1605 \score @{
1606   <<
1607     \new Voice = "one" @{
1608       \autoBeamOff
1609       \melody
1610     @}
1611     \new Lyrics \lyricsto "one" \text
1612   >>
1613   \layout @{ @}
1614   \midi @{ @}
1615 @}
1616 @end example
1617
1618 Now we want to add a cello part.  Let's look at the @q{Notes only} example:
1619
1620 @example
1621 \version "2.11.23"
1622 melody = \relative c' @{
1623   \clef treble
1624   \key c \major
1625   \time 4/4
1626
1627   a4 b c d
1628 @}
1629
1630 \score @{
1631   \new Staff \melody
1632   \layout @{ @}
1633   \midi @{ @}
1634 @}
1635 @end example
1636
1637 We don't need two @code{\version} commands.  We'll need the @code{melody}
1638 section.  We don't want two @code{\score} sections -- if we had two
1639 @code{\score}s, we'd get the two parts separately.  We want them together,
1640 as a duet.  Within the @code{\score} section, we don't need two
1641 @code{\layout} or @code{\midi}.
1642
1643 If we simply cut and paste the @code{melody} section, we would end up with
1644 two @code{melody} sections.  So let's rename them.  We'll call the section
1645 for the soprano @code{sopranoMusic} and the section for the cello
1646 @code{celloMusic}.  While we're doing this, let's rename @code{text}
1647 to be @code{sopranoLyrics}.  Remember to rename both instances of all
1648 these names -- both the initial definition (the
1649 @code{melody = relative c' @{ } part) and the name's use (in the
1650 @code{\score} section).
1651
1652 While we're doing this, let's change the cello part's staff -- celli
1653 normally use bass clef.  We'll also give the cello some different
1654 notes.
1655
1656 @example
1657 \version "2.11.23"
1658 sopranoMusic = \relative c' @{
1659   \clef treble
1660   \key c \major
1661   \time 4/4
1662
1663   a4 b c d
1664 @}
1665
1666 sopranoLyrics = \lyricmode @{
1667   Aaa Bee Cee Dee
1668 @}
1669
1670 celloMusic = \relative c @{
1671   \clef bass
1672   \key c \major
1673   \time 4/4
1674
1675   d4 g fis8 e d4
1676 @}
1677
1678 \score @{
1679   <<
1680     \new Voice = "one" @{
1681       \autoBeamOff
1682       \sopranoMusic
1683     @}
1684     \new Lyrics \lyricsto "one" \sopranoLyrics
1685   >>
1686   \layout @{ @}
1687   \midi @{ @}
1688 @}
1689 @end example
1690
1691 This is looking promising, but the cello part won't appear in the
1692 score -- we haven't used it in the @code{\score} section.  If we
1693 want the cello part to appear under the soprano part, we need to add
1694
1695 @example
1696 \new Staff \celloMusic
1697 @end example
1698
1699 @noindent
1700 underneath the soprano stuff.  We also need to add @code{<<} and
1701 @code{>>} around the music -- that tells LilyPond that there's
1702 more than one thing (in this case, two @code{Staves}) happening 
1703 at once.  The @code{\score} looks like this now
1704
1705 @c Indentation in this example is deliberately poor
1706 @example
1707 \score @{
1708   <<
1709   <<
1710     \new Voice = "one" @{
1711       \autoBeamOff
1712       \sopranoMusic
1713     @}
1714     \new Lyrics \lyricsto "one" \sopranoLyrics
1715   >>
1716   \new Staff \celloMusic
1717   >>
1718   \layout @{ @}
1719   \midi @{ @}
1720 @}
1721 @end example
1722
1723 @noindent
1724 This looks a bit messy; the indentation is messed up now.  That is
1725 easily fixed.  Here's the complete soprano and cello template.
1726
1727 @lilypond[quote,verbatim,ragged-right]
1728 \version "2.11.23"
1729 sopranoMusic = \relative c' {
1730   \clef treble
1731   \key c \major
1732   \time 4/4
1733
1734   a4 b c d
1735 }
1736
1737 sopranoLyrics = \lyricmode {
1738   Aaa Bee Cee Dee
1739 }
1740
1741 celloMusic = \relative c {
1742   \clef bass
1743   \key c \major
1744   \time 4/4
1745
1746   d4 g fis8 e d4
1747 }
1748
1749 \score {
1750   <<
1751     <<
1752       \new Voice = "one" {
1753         \autoBeamOff
1754         \sopranoMusic
1755       }
1756       \new Lyrics \lyricsto "one" \sopranoLyrics
1757     >>
1758     \new Staff \celloMusic
1759   >>
1760   \layout { }
1761   \midi { }
1762 }
1763 @end lilypond
1764
1765
1766 @node Four-part SATB vocal score
1767 @subsection Four-part SATB vocal score
1768
1769 Most vocal scores of music written for four-part mixed choir 
1770 with orchestral accompaniment such as Mendelssohn's Elijah or
1771 Handel's Messiah have the choral music and words on four
1772 staves, one for each of SATB, with a piano reduction of the
1773 orchestral accompaniment underneath.  Here's an example
1774 from Handel's Messiah:
1775
1776 @c The following should appear as music without code
1777 @lilypond[quote,ragged-right]
1778 \version "2.11.23"
1779 global = { \key d \major \time 4/4 }
1780 sopMusic = \relative c'' {
1781   \clef "treble"
1782   r4 d2 a4 | d4. d8 a2 | cis4 d cis2 |
1783 }
1784 sopWords = \lyricmode {
1785   Wor -- thy is the lamb that was slain
1786 }
1787 altoMusic = \relative a' {
1788   \clef "treble"
1789   r4 a2 a4 | fis4. fis8 a2 | g4 fis fis2 |
1790 }
1791 altoWords = \sopWords
1792 tenorMusic = \relative c' {
1793   \clef "G_8"
1794   r4 fis2 e4 | d4. d8 d2 | e4 a, cis2 |
1795 }
1796 tenorWords = \sopWords
1797 bassMusic = \relative c' {
1798   \clef "bass"
1799   r4 d2 cis4 | b4. b8 fis2 | e4 d a'2 |
1800 }
1801 bassWords = \sopWords
1802 upper = \relative a' {
1803   \clef "treble"
1804   \global
1805   r4 <a d fis>2 <a e' a>4 |
1806   <d fis d'>4. <d fis d'>8 <a d a'>2 |
1807   <g cis g'>4 <a d fis> <a cis e>2 |
1808 }
1809 lower = \relative c, {
1810   \clef "bass"
1811   \global
1812   <d d'>4 <d d'>2 <cis cis'>4 |
1813   <b b'>4. <b' b'>8 <fis fis'>2 |
1814   <e e'>4 <d d'> <a' a'>2 |
1815 }
1816
1817 \score {
1818   <<  % combine ChoirStaff and PianoStaff in parallel
1819     \new ChoirStaff <<
1820       \new Staff = "sopranos" <<
1821         \set Staff.instrumentName = "Soprano"
1822         \new Voice = "sopranos" { \global \sopMusic }
1823       >>
1824       \new Lyrics \lyricsto "sopranos" { \sopWords }
1825       \new Staff = "altos" <<
1826         \set Staff.instrumentName = "Alto"
1827         \new Voice = "altos" { \global \altoMusic }
1828       >>
1829       \new Lyrics \lyricsto "altos" { \altoWords }
1830       \new Staff = "tenors" <<
1831         \set Staff.instrumentName = "Tenor"
1832         \new Voice = "tenors" { \global \tenorMusic }
1833       >>
1834       \new Lyrics \lyricsto "tenors" { \tenorWords }
1835       \new Staff = "basses" <<
1836         \set Staff.instrumentName = "Bass"
1837         \new Voice = "basses" { \global \bassMusic }
1838       >>
1839       \new Lyrics \lyricsto "basses" { \bassWords }
1840     >>  % end ChoirStaff
1841
1842     \new PianoStaff <<
1843       \set PianoStaff.instrumentName = "Piano  "
1844       \new Staff = "upper" \upper
1845       \new Staff = "lower" \lower
1846     >>
1847   >>
1848 }
1849 @end lilypond
1850
1851 None of the templates provides this layout exactly.  The
1852 nearest is @q{SATB vocal score and automatic piano reduction},
1853 but we shall need to change the layout and add a piano
1854 accompaniment which is not derived automatically from the
1855 vocal parts.  The variables holding the music and words for
1856 the vocal parts are fine, but we shall need to add variables for
1857 the piano reduction.
1858
1859 The order in which the contexts appear in the ChoirStaff of
1860 the template do not correspond with the order in the vocal 
1861 score shown above.  We need to rearrange them so there are
1862 four staves with the words written directly underneath the
1863 notes for each part.
1864 All the voices should be @code{\voiceOne}, which is
1865 the default, so the @code{\voiceXXX} commands can be removed.
1866 We also need to specify the tenor clef for the tenors.
1867 The way in which lyrics are specified has also been simplified
1868 as we have not yet encountered the method used in the template.
1869 We've also added the names of each staff.
1870
1871 Doing this gives for our ChoirStaff:
1872
1873 @example
1874     \new ChoirStaff <<
1875       \new Staff = "sopranos" <<
1876         \set Staff.instrumentName = "Soprano"
1877         \new Voice = "sopranos" @{ \global \sopMusic @}
1878       >>
1879       \new Lyrics \lyricsto "sopranos" @{ \sopWords @}
1880       \new Staff = "altos" <<
1881         \set Staff.instrumentName = "Alto"
1882         \new Voice = "altos" @{ \global \altoMusic @}
1883       >>
1884       \new Lyrics \lyricsto "altos" @{ \altoWords @}
1885       \new Staff = "tenors" <<
1886         \set Staff.instrumentName = "Tenor"
1887         \new Voice = "tenors" @{ \global \tenorMusic @}
1888       >>
1889       \new Lyrics \lyricsto "tenors" @{ \tenorWords @}
1890       \new Staff = "basses" <<
1891         \set Staff.instrumentName = "Bass"
1892         \new Voice = "basses" @{ \global \bassMusic @}
1893       >>
1894       \new Lyrics \lyricsto "basses" @{ \bassWords @}
1895     >>  % end ChoirStaff
1896 @end example
1897
1898 Next we must work out the piano part.  This is
1899 easy - we just pull out the piano part from the
1900 @q{Solo piano} template:
1901
1902 @example
1903 \new PianoStaff <<
1904   \set PianoStaff.instrumentName = "Piano  "
1905   \new Staff = "upper" \upper
1906   \new Staff = "lower" \lower
1907 >>
1908 @end example
1909
1910 and add the variable definitions for @code{upper}
1911 and @code{lower}.
1912
1913 The ChoirStaff and PianoStaff must be combined
1914 using angle brackets as we want them to be
1915 stacked one above the other:
1916
1917 @example
1918 <<  % combine ChoirStaff and PianoStaff one above the other 
1919   \new ChoirStaff <<
1920     \new Staff = "sopranos" <<
1921       \new Voice = "sopranos" @{ \global \sopMusic @}
1922     >>
1923     \new Lyrics \lyricsto "sopranos" @{ \sopWords @}
1924     \new Staff = "altos" <<
1925       \new Voice = "altos" @{ \global \altoMusic @}
1926     >>
1927     \new Lyrics \lyricsto "altos" @{ \altoWords @}
1928     \new Staff = "tenors" <<
1929       \clef "G_8"  % tenor clef
1930       \new Voice = "tenors" @{ \global \tenorMusic @}
1931     >>
1932     \new Lyrics \lyricsto "tenors" @{ \tenorWords @}
1933     \new Staff = "basses" <<
1934       \clef "bass"
1935       \new Voice = "basses" @{ \global \bassMusic @}
1936     >>
1937     \new Lyrics \lyricsto "basses" @{ bassWords @}   
1938   >>  % end ChoirStaff
1939
1940   \new PianoStaff <<
1941     \set PianoStaff.instrumentName = "Piano  "
1942     \new Staff = "upper" \upper
1943     \new Staff = "lower" \lower
1944   >>
1945 >>
1946 @end example
1947
1948 Combining all these together and adding the music
1949 for the three bars of the example above gives:
1950
1951 @lilypond[quote,verbatim,ragged-right]
1952 \version "2.11.23"
1953 global = { \key d \major \time 4/4 }
1954 sopMusic = \relative c'' {
1955   \clef "treble"
1956   r4 d2 a4 | d4. d8 a2 | cis4 d cis2 |
1957 }
1958 sopWords = \lyricmode {
1959   Wor -- thy is the lamb that was slain
1960 }
1961 altoMusic = \relative a' {
1962   \clef "treble"
1963   r4 a2 a4 | fis4. fis8 a2 | g4 fis fis2 |
1964 }
1965 altoWords = \sopWords
1966 tenorMusic = \relative c' {
1967   \clef "G_8"
1968   r4 fis2 e4 | d4. d8 d2 | e4 a, cis2 |
1969 }
1970 tenorWords = \sopWords
1971 bassMusic = \relative c' {
1972   \clef "bass"
1973   r4 d2 cis4 | b4. b8 fis2 | e4 d a'2 |
1974 }
1975 bassWords = \sopWords
1976 upper = \relative a' {
1977   \clef "treble"
1978   \global
1979   r4 <a d fis>2 <a e' a>4 |
1980   <d fis d'>4. <d fis d'>8 <a d a'>2 |
1981   <g cis g'>4 <a d fis> <a cis e>2 |
1982 }
1983 lower = \relative c, {
1984   \clef "bass"
1985   \global
1986   <d d'>4 <d d'>2 <cis cis'>4 |
1987   <b b'>4. <b' b'>8 <fis fis'>2 |
1988   <e e'>4 <d d'> <a' a'>2 |
1989 }
1990
1991 \score {
1992   <<  % combine ChoirStaff and PianoStaff in parallel
1993     \new ChoirStaff <<
1994       \new Staff = "sopranos" <<
1995         \set Staff.instrumentName = "Soprano"
1996         \new Voice = "sopranos" { \global \sopMusic }
1997       >>
1998       \new Lyrics \lyricsto "sopranos" { \sopWords }
1999       \new Staff = "altos" <<
2000         \set Staff.instrumentName = "Alto"
2001         \new Voice = "altos" { \global \altoMusic }
2002       >>
2003       \new Lyrics \lyricsto "altos" { \altoWords }
2004       \new Staff = "tenors" <<
2005         \set Staff.instrumentName = "Tenor"
2006         \new Voice = "tenors" { \global \tenorMusic }
2007       >>
2008       \new Lyrics \lyricsto "tenors" { \tenorWords }
2009       \new Staff = "basses" <<
2010         \set Staff.instrumentName = "Bass"
2011         \new Voice = "basses" { \global \bassMusic }
2012       >>
2013       \new Lyrics \lyricsto "basses" { \bassWords }
2014     >>  % end ChoirStaff
2015
2016     \new PianoStaff <<
2017       \set PianoStaff.instrumentName = "Piano  "
2018       \new Staff = "upper" \upper
2019       \new Staff = "lower" \lower
2020     >>
2021   >>
2022 }
2023 @end lilypond
2024   
2025
2026 @node Building a score from scratch
2027 @subsection Building a score from scratch
2028
2029 After gaining some facility with writing LilyPond code you
2030 may find that it is easier to build a score from scratch
2031 rather than modifying one of the templates.  You can also
2032 develop your own style this way to suit the sort of music you
2033 like.  Let's see how to put together the score for an organ 
2034 prelude as an example.
2035
2036 We begin with a header section.  Here go the title, name
2037 of composer, etc, then come any variable definitions, and
2038 finally the score block.  Let's start with these in outline
2039 and fill in the details later.
2040
2041 We'll use the first two bars of Bach's prelude
2042 based on @emph{Jesu, meine Freude} which is written for two
2043 manuals and pedal organ.  The top manual part has two voices,
2044 the lower and pedal organ one each.  So we need four
2045 music definitions and one to define the time signature
2046 and key:
2047
2048 @example
2049 \version "2.11.23"
2050 \header @{
2051   title = "Jesu, meine Freude"
2052   composer = "J S Bach"
2053 @}
2054 TimeKey = @{ \time 4/4 \key c \minor @}
2055 ManualOneVoiceOneMusic = @{s1@}
2056 ManualOneVoiceTwoMusic = @{s1@}
2057 ManualTwoMusic = @{s1@}
2058 PedalOrganMusic = @{s1@}
2059
2060 \score @{
2061 @}
2062 @end example
2063
2064 For now we've just used a spacer note, @code{s1},
2065 instead of the real music.  We'll add that later.
2066
2067 Next let's see what should go in the score block.
2068 We simply mirror the staff structure we want.
2069 Organ music is usually written on three staves,
2070 one for each manual and one for the pedals.  The
2071 manual staves should be bracketed together so we
2072 need to use a PianoStaff for them.  The first
2073 manual part needs two voices and the second manual
2074 part just one.
2075
2076 @example
2077   \new PianoStaff <<
2078     \new Staff = "ManualOne" <<
2079       \new Voice @{ \ManualOneVoiceOneMusic @}
2080       \new Voice @{ \ManualOneVoiceTwoMusic @}
2081     >>  % end ManualOne Staff context
2082     \new Staff = "ManualTwo" <<
2083       \new Voice @{ \ManualTwoMusic @}
2084     >>  % end ManualTwo Staff context
2085   >>  % end PianoStaff context
2086 @end example
2087
2088 Next we need to add a staff for the pedal organ.
2089 This goes underneath the PianoStaff, but it must
2090 be simultaneous with it, so we need angle brackets
2091 round the two.  Missing these out would generate
2092 an error in the log file.  It's a common mistake 
2093 which you'll make sooner or later!  Try copying
2094 the final example at the end of this section,
2095 remove these angle brackets, and compile it to
2096 see what errors it generates.
2097
2098 @example
2099 <<  % PianoStaff and Pedal Staff must be simultaneous
2100   \new PianoStaff <<
2101     \new Staff = "ManualOne" <<
2102       \new Voice @{ \ManualOneVoiceOneMusic @}
2103       \new Voice @{ \ManualOneVoiceTwoMusic @}
2104     >>  % end ManualOne Staff context
2105     \new Staff = "ManualTwo" <<
2106       \new Voice @{ \ManualTwoMusic @}
2107     >>  % end ManualTwo Staff context
2108   >>  % end PianoStaff context
2109   \new Staff = "PedalOrgan" <<
2110     \new Voice @{ \PedalOrganMusic @}
2111   >>
2112 >>
2113 @end example
2114
2115 It is not strictly necessary to use the simultaneous construct
2116 @code{<<  >>} for the manual two staff and the pedal organ staff,
2117 since they contain only one music expression, but it does no harm
2118 and always using angle brackets after @code{\new Staff} is a good
2119 habit to cultivate in case there are multiple voices.  
2120
2121 Let's add this structure to the score block, and adjust the
2122 indenting.  We also add the appropriate clefs, ensure the
2123 second voice stems point down with @code{\voiceTwo} and
2124 enter the time signature and key to each staff using our
2125 predefined variable, @code{\TimeKey}.
2126
2127 @example
2128 \score @{
2129   <<  % PianoStaff and Pedal Staff must be simultaneous
2130     \new PianoStaff <<
2131       \new Staff = "ManualOne" <<
2132         \TimeKey  % set time signature and key
2133         \clef "treble"
2134         \new Voice @{ \ManualOneVoiceOneMusic @}
2135         \new Voice @{ \voiceTwo \ManualOneVoiceTwoMusic @}
2136       >>  % end ManualOne Staff context
2137       \new Staff = "ManualTwo" <<
2138         \TimeKey
2139         \clef "bass"
2140         \new Voice @{ \ManualTwoMusic @}
2141       >>  % end ManualTwo Staff context
2142     >>  % end PianoStaff context
2143     \new Staff = "PedalOrgan" <<
2144       \TimeKey
2145       \clef "bass"
2146       \new Voice @{ \PedalOrganMusic @}
2147     >>  % end PedalOrgan Staff
2148   >>
2149 @}  % end Score context
2150 @end example
2151
2152 That completes the structure.  Any three-staff organ music
2153 will have a similar structure, although the number of voices
2154 may vary.  All that remains now 
2155 is to add the music, and combine all the parts together.
2156
2157 @lilypond[quote,verbatim,ragged-right]
2158 \version "2.11.23"
2159 \header {
2160   title = "Jesu, meine Freude"
2161   composer = "J S Bach"
2162 }
2163 TimeKey = { \time 4/4 \key c \minor }
2164 ManualOneVoiceOneMusic = \relative g' {
2165   g4 g f ees | d2 c2 |
2166 }
2167 ManualOneVoiceTwoMusic = \relative c' {
2168   ees16 d ees8~ ees16 f ees s c8 d~ d c~ |
2169   c c4 b8 c8. g16 c b c d |
2170 }
2171 ManualTwoMusic = \relative c' {
2172   c16 b c8~ c16 b c g a8 g~ g16 g aes ees |
2173   f ees f d g aes g f ees d e8~ ees16 f ees d |
2174 }
2175 PedalOrganMusic = \relative c {
2176   r8 c16 d ees d ees8~ ees16 a, b g c b c8 |
2177   r16 g ees f g f g8 c,2 |
2178   }
2179
2180 \score {
2181   <<  % PianoStaff and Pedal Staff must be simultaneous
2182     \new PianoStaff <<
2183       \new Staff = "ManualOne" <<
2184         \TimeKey  % set time signature and key
2185         \clef "treble"
2186         \new Voice { \ManualOneVoiceOneMusic }
2187         \new Voice { \voiceTwo \ManualOneVoiceTwoMusic }
2188       >>  % end ManualOne Staff context
2189       \new Staff = "ManualTwo" <<
2190         \TimeKey
2191         \clef "bass"
2192         \new Voice { \ManualTwoMusic }
2193       >>  % end ManualTwo Staff context
2194     >>  % end PianoStaff context
2195     \new Staff = "PedalOrgan" <<
2196       \TimeKey
2197       \clef "bass"
2198       \new Voice { \PedalOrganMusic }
2199     >>  % end PedalOrgan Staff
2200   >>
2201 }  % end Score context
2202 @end lilypond
2203
2204
2205