]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/user/fundamental.itely
Merge branch 'master' of git://git.sv.gnu.org/lilypond
[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 output 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 @ref{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   % Main voice
627   c16 d e f
628   << % Bar 1
629     { 
630       \voiceOneStyle
631       g4 f e
632     }
633   \\
634     { 
635       \voiceTwoStyle
636       r8 e4 d c8 ~
637     }
638   >>
639   << % Bar 2
640     { d2 e2 }  % Voice 1 continues
641   \\
642     { c8 b16 a b8 g ~ g2 }  % Voice 2 continues
643   \\
644     {
645       \voiceThreeStyle
646       s4 b4 c2
647     }
648   >>
649 }
650 @end lilypond
651
652 The commands @code{\voiceXXXStyle} are mainly intended for use in
653 educational documents such as this one.  They modify the color
654 of the notehead, the stem and the beams, and the style of the
655 notehead, so that the voices may be easily distinguished.
656 Voice one is set to red diamonds, voice two to blue triangles,
657 voice three to green crossed circles, and voice four (not used
658 here) to magenta crosses.  We shall see later how commands like 
659 these may be created by the user.  
660 See @ref{Visibility and color of objects}
661 TODO Add link to using variables for tweaks
662
663 Polyphony does not change the relationship of notes within a
664 @code{\relative @{ @}} block.  Each note is still calculated 
665 relative to the note immediately preceding it, or to the first 
666 note of the preceding chord.  So in
667
668 @example
669 \relative c' @{ noteA << < noteB noteC > \\ noteD >> noteE @}
670 @end example
671
672 @noindent
673 @code{noteB} is relative to @code{noteA}                      @*
674 @code{noteC} is relative to @code{noteB}, not @code{noteA};   @*
675 @code{noteD} is relative to @code{noteB}, not @code{noteA} or
676 @code{noteC}.                                                 @*
677 @code{noteE} is relative to @code{noteD}, not @code{noteA}
678
679 We are now in a position to return to the first example from
680 the Chopin Nocturne to see how this might be encoded.  As we
681 shall see, this encounters some difficulties.  We begin as
682 we have learnt, using the @code{<< \\  >>} construct to 
683 enter the music of the first bar in three voices:
684
685 @lilypond[quote,verbatim,fragment,ragged-right]
686 \new Staff \relative c'' {
687   \key aes \major
688   << 
689     { c2 aes4. bes8 } \\ { aes2 f4 fes } \\ { <ees c>2 des2 }
690   >>
691   <c ees aes c>1
692 }
693 @end lilypond
694
695 @cindex stem down
696 @cindex stem up
697 @funindex \voiceOne
698 @funindex \voiceTwo
699 @funindex \voiceThree
700 @funindex \voiceFour
701
702 The stem directions are automatically assigned with the
703 odd-numbered voices taking upward stems and the even-numbered
704 voices downward ones.  The stems for voices 1 and 2 are right,
705 but the stems in voice 3 should go down in this particular piece
706 of music.  We can correct this
707 by telling LilyPond that this third voice is really a fourth
708 voice, with stems going down, using the @code{\voiceFour} 
709 command.  There are also corresponding @code{\voiceOne},
710 @code{\voiceTwo}, and @code{\voiceThree} 
711 commands.  This results in the following:
712
713 @lilypond[quote,verbatim,fragment,ragged-right]
714 \new Staff \relative c'' {
715   \key aes \major
716   << 
717     { c2 aes4. bes8 } \\ 
718     { aes2 f4 fes   } \\ 
719     { \voiceFour <ees c>2 des2 }
720   >> |
721   <c ees aes c>1 |
722 }
723 @end lilypond
724
725 @noindent
726 and exposes a problem commonly encountered with multiple
727 voices: the stems of notes can collide with noteheads
728 in other voices.  In laying out the notes, LilyPond allows the
729 notes or chords from two voices to occupy the same vertical
730 note column provided the stems are in opposite directions, but
731 the notes from a third voice are displaced to avoid the stems
732 colliding.  This often works well, but in this example the
733 notes of the third voice are clearly not well placed by default.
734 LilyPond provides several ways to adjust the horizontal placing
735 of notes.  We are not quite ready yet to see how to correct this,
736 so we shall leave this problem until a later section 
737 (see @ref{Fixing overlapping notation} )
738
739 @node Explicitly instantiating voices
740 @subsection Explicitly instantiating voices
741
742 Voice contexts can also be created manually
743 inside a @code{<< >>} block to create polyphonic music, using
744 @code{\voiceOne} ... @code{\voiceFour} to indicate the required
745 directions of stems, slurs, etc.  In longer scores this method
746 is clearer, as it permits the voices to be separated and to be
747 given more descriptive names.
748
749 Specifically, the construct @code{<< \\ >>} which we used in
750 the previous section:
751
752 @example
753 \new Staff @{
754   \relative c' @{
755     << @{ e4 f g a @} \\ @{ c,4 d e f @} >>
756   @}
757 @}
758 @end example
759
760 @noindent
761 is equivalent to 
762
763 @example
764 \new Staff <<
765   \new Voice = "1" @{ \voiceOne \relative c' @{ e4 f g a @} @}
766   \new Voice = "2" @{ \voiceTwo \relative c' @{ c4 d e f @} @}
767 >>
768 @end example
769
770 Both of the above would produce
771
772 @c The following example should not display the code
773 FIXME: weird compiling bug!  Leave for gp.
774 @c @lilypond{ragged-right,quote]
775 @c \relative c' {
776 @c %  << { e4 f g a } \\ { c,4 d e f } >>
777 @c }
778 @c @end lilypond
779
780 The @code{\voiceXXX} commands set the direction of stems, slurs,
781 ties, articulations, text annotations, augmentation dots of dotted
782 notes, and fingerings.  @code{\voiceOne} and @code{\voiceThree}
783 make these objects point upwards, while @code{\voiceTwo} and
784 @code{\voiceFour} make them point downwards.  These commands also
785 generate a horizontal shift for each voice when this is required 
786 to avoid clashes of note heads.  The command @code{\oneVoice} 
787 reverts the settings back to the normal values for a single voice.
788
789 Let us see in some simple examples exactly what effect 
790 @code{\oneVoice}, @code{\voiceOne} and @code{voiceTwo} have on
791 markup, ties, slurs, and dynamics:
792
793 @lilypond[quote,ragged-right,verbatim]
794 \relative c'{
795   % Default behaviour or behaviour after \oneVoice
796   c d8 ~ d e4 ( f g a ) b-> c
797 }
798 @end lilypond
799
800 @lilypond[quote,ragged-right,verbatim]
801 \relative c'{
802   \voiceOne
803   c d8 ~ d e4 ( f g a ) b-> c
804   \oneVoice
805   c, d8 ~ d e4 ( f g a ) b-> c
806 }
807 @end lilypond
808
809 @lilypond[quote,ragged-right,verbatim]
810 \relative c'{
811   \voiceTwo
812   c d8 ~ d e4 ( f g a ) b-> c
813   \oneVoice
814   c, d8 ~ d e4 ( f g a ) b-> c
815 }
816 @end lilypond
817
818 An expression that appears directly inside a @code{<< >>} belongs
819 to the main voice (but, note, @strong{not} in a @code{<< \\ >>}
820 construct).  This is useful when extra voices appear while the
821 main voice is playing.  Here is a more correct rendition of the
822 example from the previous section.  The red diamond-shaped notes
823 demonstrate that the main melody is now in a single voice context,
824 permitting a phrasing slur to be drawn over them.
825
826 @lilypond[quote,ragged-right,verbatim]
827 \new Staff \relative c' {
828   \voiceOneStyle
829   % The following notes are monophonic
830   c16^( d e f
831   % Start simultaneous section of three voices
832   <<
833     % Continue the main voice in parallel
834     { g4 f e | d2 e2) }
835     % Initiate second voice
836     \new Voice {
837       % Set stems, etc, down
838       \voiceTwo
839       r8 e4 d c8 ~ | c8 b16 a b8 g ~ g2
840     }
841     % Initiate third voice
842     \new Voice {
843       % Set stems, etc, up
844       \voiceThree
845       s2. | s4 b4 c2
846     }
847   >>
848 }
849 @end lilypond
850
851 @cindex nesting music expressions
852 @cindex nesting simultaneous constructs
853
854 More deeply nested polyphony constructs are possible, and if a
855 voice appears only briefly this might be a more natural way to
856 typeset the music.
857
858 @lilypond[quote,ragged-right,verbatim]
859 \new Staff \relative c' {
860   c16^( d e f
861   <<
862     { g4 f e | d2 e2) }
863     \new Voice {
864       \voiceTwo
865       r8 e4 d c8 ~ |
866       <<
867         {c8 b16 a b8 g ~ g2}
868         \new Voice {
869           \voiceThree
870           s4 b4 c2
871         }
872       >>
873     }
874   >>
875 }
876 @end lilypond
877
878
879 This method of nesting new voices briefly is useful
880 when only small sections of the music
881 are polyphonic, but when the whole staff is largely polyphonic
882 it can be clearer to use multiple voices throughout, using
883 spacing notes to step over sections where the voice is silent,
884 as here:
885
886 @lilypond[quote,ragged-right,verbatim]
887 \new Staff \relative c' <<
888   % Initiate first voice
889   \new Voice {
890     \voiceOne
891     c16^( d e f g4 f e | d2 e2) |
892   }
893   % Initiate second voice
894   \new Voice {
895     % set stems, etc down
896     \voiceTwo
897     s4 r8 e4 d c8 ~ | c8 b16 a b8 g ~ g2 |
898   }
899   % Initiate third voice
900   \new Voice {
901     % set stems, etc up
902     \voiceThree
903     s1 | s4 b4 c2 |
904   }
905 >>
906 @end lilypond
907
908 @cindex note column
909 @cindex shift commands
910 @funindex \shiftOff
911 @funindex \shiftOn
912 @funindex \shiftOnn
913 @funindex \shiftOnnn
914
915 Closely spaced notes in a chord, or notes occuring at the same
916 time in different voices, are arranged in two, occasionally more,
917 columns to prevent the noteheads overlapping.  These are called
918 note columns.  There are separate columns for each voice, and 
919 the currently specified voice-dependent shift is applied to the 
920 note column if there would otherwise be a collision.  This can
921 be seen in the example above.  In bar 2 the C in voice two is
922 shifted to the right relative to the D in voice one, and in the
923 final chord the C in voice three is also shifted to the right
924 relative to the other notes.
925
926 The @code{\shiftOn}, @code{\shiftOnn}, @code{\shiftOnnn}, and
927 @code{\shiftOff} commands specify the degree to which notes and
928 chords of the voice should be shifted if a collision
929 would otherwise occur. By default, the outer voices (normally 
930 voices one and two) have @code{\shiftOff} specified, while the 
931 inner voices (three and four) have @code{\shiftOn} specified.  
932 When a shift is applied, Voices one and three are shifted to 
933 the right and voices two and four to the left.
934
935 @code{\shiftOnn} and @code{\shiftOnnn} define further shift
936 levels which may be specified temporarily to resolve collisions
937 in complex situations -- see @ref{Real music example}.
938
939 A note column can contain just one note (or chord) from a voice
940 with stems up and one note (or chord) from a voice with stems
941 down.  If notes from two voices which have their stems in the
942 same direction are placed at the same position and both voices 
943 have no shift or the same shift specified, the error message
944 @qq{Too many clashing note columns} will be produced.
945
946 @node Voices and vocals
947 @subsection Voices and vocals
948
949 Vocal music presents a special difficulty: we need to combine two
950 expressions -- notes and lyrics.
951
952 You have already seen the @code{\addlyrics@{@}} command, which
953 handles simple scores well.  However, this technique is
954 quite limited.  For more complex music, you must introduce the
955 lyrics in a @code{Lyrics} context using @code{\new Lyrics} and
956 explicitly link
957 the lyrics to the notes with @code{\lyricsto@{@}}, using the
958 name assigned to the Voice.
959
960 @lilypond[quote,verbatim,fragment]
961 <<
962   \new Voice = "one" \relative c'' {
963     \autoBeamOff
964     \time 2/4
965     c4 b8. a16 g4. f8 e4 d c2
966   }
967   \new Lyrics \lyricsto "one" {
968     No more let sins and sor -- rows grow.
969   }
970 >>
971 @end lilypond
972
973 The automatic beaming which LilyPond uses by default works well
974 for instrumental music, but not so well for music with lyrics,
975 where beaming is either not required at all or is used to indicate
976 melismata in the lyrics.  In the example above we use the command
977 @code{\autoBeamOff} to turn off the automatic beaming.
978
979 Let us reuse the earlier example from Judas Maccabæus to 
980 illustrate this more flexible technique.  We first recast
981 it to use variables so the music and lyrics can be separated
982 from the staff structure.  We also introduce a ChoirStaff 
983 bracket.  The lyrics themselves must be introduced with 
984 @code{\lyricmode} to ensure they are interpreted as lyrics
985 rather than music.
986
987 @lilypond[quote,verbatim]
988 global = { \time 6/8 \partial 8 \key f \major}
989 SopOneMusic = \relative c'' {
990   c8 | c([ bes)] a a([ g)] f | f'4. b, | c4.~ c4 }
991 SopTwoMusic = \relative c' {
992   r8 | r4. r4 c8 | a'([ g)] f f([ e)] d | e([ d)] c bes' }
993 SopOneLyrics = \lyricmode {
994   Let | flee -- cy flocks the | hills a -- dorn, __ }
995 SopTwoLyrics = \lyricmode {
996   Let | flee -- cy flocks the | hills a -- dorn, }
997
998 \score {
999   \new ChoirStaff <<
1000     \new Staff <<
1001       \new Voice = "SopOne" {
1002         \global
1003         \SopOneMusic
1004       }
1005       \new Lyrics \lyricsto "SopOne" {
1006         \SopOneLyrics
1007       }
1008     >>
1009     \new Staff <<
1010       \new Voice = "SopTwo" {
1011         \SopTwoMusic
1012       }
1013       \new Lyrics \lyricsto "SopTwo" {
1014         \SopTwoLyrics
1015       }
1016     >>
1017   >>
1018 }
1019 @end lilypond
1020
1021 This is the basic structure of all vocal scores.  More staves may
1022 be added as required, more voices may be added to the staves,
1023 more verses may be added to the lyrics,
1024 and the variables containing the music can easily be placed
1025 in separate files should they become too long.
1026
1027 Here is a final example of the first line of a hymn with four
1028 verses, set for SATB.  In this case the words for all four
1029 parts are the same.
1030
1031 @lilypond[quote,verbatim]
1032 TimeKey = { \time 4/4 \partial 4 \key c \major}
1033 SopMusic   = \relative c' { c4 | e4. e8 g4  g  | a a g }
1034 AltoMusic  = \relative c' { c4 | c4. c8 e4  e  | f f e }
1035 TenorMusic = \relative c  { e4 | g4. g8 c4. b8 | a8 b c d e4 }
1036 BassMusic  = \relative c  { c4 | c4. c8 c4  c  | f8 g a b c4 }
1037 VerseOne   = \lyricmode { 
1038   E -- | ter -- nal fa -- ther, | strong to save, }
1039 VerseTwo   = \lyricmode { 
1040   O | Christ, whose voice the | wa -- ters heard, }
1041 VerseThree = \lyricmode { 
1042   O | Ho -- ly Spi -- rit, | who didst brood }
1043 VerseFour  = \lyricmode { 
1044   O | Tri -- ni -- ty of | love and pow'r }
1045
1046 \score {
1047   \new ChoirStaff <<
1048     \new Staff <<
1049       \clef "treble"
1050       \new Voice = "Sop"  { \voiceOne \TimeKey \SopMusic }
1051       \new Voice = "Alto" { \voiceTwo \AltoMusic }
1052       \new Lyrics \lyricsto "Sop" { \VerseOne   }
1053       \new Lyrics \lyricsto "Sop" { \VerseTwo   }
1054       \new Lyrics \lyricsto "Sop" { \VerseThree }
1055       \new Lyrics \lyricsto "Sop" { \VerseFour  }
1056     >>
1057     \new Staff <<
1058       \clef "bass"
1059       \new Voice = "Tenor" { \voiceOne \TenorMusic }
1060       \new Voice = "Bass"  { \voiceTwo \BassMusic  }
1061     >>
1062   >>
1063 }
1064 @end lilypond
1065
1066 @node Contexts and engravers
1067 @section Contexts and engravers
1068
1069 Contexts and engravers have been mentioned informally
1070 in earlier sections; we now must look at 
1071 these concepts in more detail, as they are important
1072 in the fine-tuning of LilyPond output.
1073
1074
1075 @menu
1076 * Contexts explained::          
1077 * Creating contexts::           
1078 * Engravers explained::         
1079 * Modifying context properties::  
1080 * Adding and removing engravers::  
1081 @end menu
1082
1083 @node Contexts explained
1084 @subsection Contexts explained
1085
1086 When music is printed, many notational elements which do not 
1087 appear explicitly in the input file must be added to the
1088 output.  For example, compare the input and output of the 
1089 following example:
1090
1091 @lilypond[quote,verbatim,relative=2,fragment]
1092 cis4 cis2. g4
1093 @end lilypond
1094
1095 The input is rather sparse, but in the output, bar lines, 
1096 accidentals, clef, and time signature have been added.  When 
1097 LilyPond @emph{interprets} the input the musical information 
1098 is inspected in time order, similar to reading a score from left 
1099 to right.  While reading the input, the program remembers where 
1100 measure boundaries are, and which pitches require explicit 
1101 accidentals.  This information must be held on several levels.  
1102 For example, the effect of an accidental is limited
1103 to a single staff, while a bar line must be synchronized across 
1104 the entire score.
1105
1106 Within LilyPond, these rules and bits of information are grouped
1107 in @emph{Contexts}.  We have already met the 
1108 @code{Voice} context. 
1109 Others are the @code{Staff} and @code{Score} contexts.  
1110 Contexts are hierarchical to reflect the heirarchical nature of 
1111 a musical score.  
1112 For example: a @code{Staff} context can contain many 
1113 @code{Voice} contexts, and a @code{Score} context can 
1114 contain many @code{Staff} contexts.
1115
1116 @quotation
1117 @image{context-example,5cm,,}
1118 @end quotation
1119
1120 Each context has the responsibility for enforcing some notation rules,
1121 creating some notation objects and maintaining the associated
1122 properties.  For example, the @code{Voice} context may introduce an
1123 accidental and then the @code{Staff} context maintains the rule to
1124 show or suppress the accidental for the remainder of the measure.
1125
1126 As another example, the synchronization of bar lines is, by default, 
1127 handled in the @code{Score} context.
1128 However, in some music we may not want the bar lines to be
1129 synchronized -- consider a polymetric score in 4/4 and 3/4 time.
1130 In such cases, we must modify the default settings of the 
1131 @code{Score} and @code{Staff} contexts.
1132
1133 For very simple scores, contexts are created implicitly, and you need
1134 not be aware of them.  For larger pieces, such as anything with more
1135 than one staff, they must be
1136 created explicitly to make sure that you get as many staves as you
1137 need, and that they are in the correct order.  For typesetting pieces
1138 with specialized notation, it is usual to modify existing, or
1139 even to define totally new, contexts.
1140
1141 In addition to the @code{Score,} @code{Staff} and 
1142 @code{Voice} contexts there are contexts which fit between
1143 the score and staff levels to control staff groups, such as the
1144 @code{PianoStaff} and @code{ChoirStaff} contexts.  There
1145 are also alternative staff and voice contexts, and contexts for
1146 lyrics, percussion, fret boards, figured bass, etc.  A complete
1147 list is shown in the Notation Reference.
1148 TODO: Add link
1149
1150 The names of all context types are formed from one or more 
1151 words, each word being capitalised and joined immediately to the 
1152 preceding word with no hyphen or underscore, e.g., 
1153 @code{GregorianTranscriptionStaff}.
1154
1155 @node Creating contexts
1156 @subsection Creating contexts
1157
1158 There can be only one top level context: the 
1159 @code{Score} 
1160 context.  This is created with the @code{\score} command, 
1161 or, in simple scores, it is created automatically.
1162
1163 For scores with only one voice and one staff, the 
1164 @code{Voice} and @code{Staff} contexts may be left to be 
1165 created automatically, but for more complex scores it is 
1166 necessary to create them by hand.  
1167 The simplest command that does this is @code{\new}.  
1168 It is prepended to a music expression, for example
1169
1170 @funindex \new
1171 @cindex new contexts
1172 @cindex Context, creating
1173
1174 @example
1175 \new @var{type} @var{music-expression}
1176 @end example
1177
1178 @noindent
1179 where @var{type} is a context name (like @code{Staff} or
1180 @code{Voice}).  This command creates a new context, and starts
1181 interpreting the @var{music-expression} within that context.
1182
1183 Note that there is no @code{\new Score} command;
1184 the single top-level @code{Score} context is introduced 
1185 with @code{\score}.  
1186
1187 The @code{\new} command may also give a identifying name to the 
1188 context to distinguish it from other contexts of the same type,
1189
1190 @example
1191 \new @var{type} = @var{id} @var{music-expression}
1192 @end example
1193
1194 Note the distinction between the name of the context type,
1195 @code{Staff}, @code{Voice}, etc, and
1196 the identifying name of a particular instance of that type,
1197 which can be any sequence of letters invented by the user.
1198 The identifying name is used to refer back to that particular
1199 instance of a context.  We saw this in use in the section on 
1200 lyrics in @ref{Voices and vocals}.
1201
1202
1203 @node Engravers explained
1204 @subsection Engravers explained
1205
1206 @cindex engravers
1207
1208 Every mark on the printed output of a score produced by LilyPond
1209 is produced by an @code{Engraver}.  Thus there is an engraver
1210 to print staves, one to print noteheads, one for stems, one for
1211 beams, etc, etc.  In total there are over 120 such engravers!
1212 Fortunately, for most scores it is not necessary to know about 
1213 more than a few, and for simple scores you do not need to know 
1214 about any.  
1215
1216 Engravers live and operate in Contexts.
1217 Engravers such as the @code{Metronome_mark_engraver}, whose
1218 action and output applies to the score as a whole, operate in
1219 the highest level context -- the @code{Score} context.
1220
1221 The @code{Clef_engraver} and @code{Key_engraver} are to be
1222 found in every Staff Context, as different staves may require 
1223 different clefs and keys.
1224
1225 The @code{Note_heads_engraver} and @code{Stem_engraver} live
1226 in every @code{Voice} context, the lowest level context of all.
1227
1228 Each engraver processes the particular objects associated
1229 with its function, and maintains the properties that relate
1230 to that function.  These properties, like the properties
1231 associated with contexts, may be modified to change the
1232 operation of the engraver or the appearance of those elements
1233 in the printed score.
1234    
1235 Engravers all have compound names formed from words which
1236 describe their function.  Just the first word is capitalised, 
1237 and the remainder are joined to it with underscores.  Thus
1238 the @code{Staff_symbol_engraver} is responsible for creating the
1239 lines of the staff, the @code{Clef_engraver} determines and sets
1240 the pitch reference point on the staff by drawing a clef symbol.
1241
1242 Here are some of the most common engravers together with their
1243 function.  You will see it is easy to guess the function from 
1244 the name, or vice versa.
1245
1246 @multitable @columnfractions .3 .7 
1247 @headitem Engraver
1248   @tab Function
1249 @item Accidental_engraver
1250   @tab Makes accidentals, cautionary and suggested accidentals
1251 @item Beam_engraver
1252   @tab Engraves beams
1253 @item Clef_engraver
1254   @tab Engraves clefs
1255 @item Dynamic_engraver
1256   @tab Creates hairpins and dynamic texts
1257 @item Key_engraver
1258   @tab Creates the key signature
1259 @item Metronome_mark_engraver
1260   @tab Engraves metronome marking
1261 @item Note_heads_engraver
1262   @tab Engraves noteheads
1263 @item Rest_engraver
1264   @tab Engraves rests
1265 @item Staff_symbol_engraver
1266   @tab Engraves the five (by default) lines of the staff
1267 @item Stem_engraver
1268   @tab Creates stems and single-stem tremulos
1269 @item Time_signature_engraver
1270   @tab Creates time signatures
1271 @end multitable
1272
1273 @smallspace
1274
1275 We shall see later how the output of LilyPond can be changed
1276 by modifying the action of Engravers.
1277   
1278
1279 @node Modifying context properties
1280 @subsection Modifying context properties
1281
1282 @cindex context properties
1283 @funindex \set
1284 @funindex \unset
1285
1286 Contexts are responsible for holding the values of a number of
1287 context @emph{properties}.  Many of them can be changed to
1288 influence the interpretation of the input and so change the
1289 appearance of the output.  They are changed by the 
1290 @code{\set} command.  This takes the form
1291
1292 @example
1293 \set @emph{ContextName}.@emph{propertyName} = #@emph{value}
1294 @end example
1295
1296 Where the @emph{ContextName} is usually @code{Score},
1297 @code{Staff} or @code{Voice}.  It may be omitted,
1298 in which case @code{Voice} is assumed.
1299
1300 The names of context properties consist of words joined
1301 together with no hyphens or underscores, all except the
1302 first having a capital letter.  Here are a few examples
1303 of some commonly used ones.  There are many more.
1304
1305 @c attempt to force this onto a new page
1306 @need 50
1307 @multitable @columnfractions .25 .15 .45 .15
1308 @headitem propertyName
1309   @tab Type
1310   @tab Function
1311   @tab Example Value
1312 @item extraNatural
1313   @tab Boolean
1314   @tab If true, set extra natural signs before accidentals
1315   @tab @code{#t}, @code{#f}
1316 @item currentBarNumber
1317   @tab Integer
1318   @tab Set the current bar number
1319   @tab @code{50}
1320 @item doubleSlurs
1321   @tab Boolean
1322   @tab If true, print slurs both above and below notes
1323   @tab @code{#t}, @code{#f}
1324 @item instrumentName
1325   @tab Text
1326   @tab Set the name to be placed at the start of the staff
1327   @tab @code{"Cello I"}
1328 @item fontSize
1329   @tab Real
1330   @tab Increase or decrease the font size
1331   @tab @code{2.4}
1332 @item stanza
1333   @tab Text
1334   @tab Set the text to print before the start of a verse
1335   @tab @code{"2"}
1336 @end multitable
1337
1338 @noindent
1339 where a Boolean is either True (@code{#t}) or False (@code{#f}),
1340 an Integer is a positive whole number, a Real is a positive
1341 or negative decimal number, and text is enclosed in double
1342 apostrophes.  Note the occurrence of hash signs,
1343 (@code{#}), in two different places -- as part of the Boolean 
1344 value before the @code{t} or @code{f}, and before @emph{value}
1345 in the @code{\set} statement.  So when a Boolean is being 
1346 entered you need to code two hash signs, e.g., @code{##t}.  
1347
1348 Before we can set any of these properties we need to know
1349 in which context they operate.  Sometimes this is obvious,
1350 but occasionally it can be tricky.  If the wrong context
1351 is specified, no error message is produced, but the expected
1352 action will not take place.  For example, the
1353 @code{instrumentName} clearly lives in the Staff context, since
1354 it is the staff that is to be named.
1355 In this example the first staff is labelled, but not the second,
1356 because we omitted the context name.
1357
1358 @lilypond[quote,verbatim,ragged-right]
1359 <<
1360   \new Staff \relative c'' {
1361     \set Staff.instrumentName = "Soprano"
1362     c4 c
1363  }
1364   \new Staff \relative c' {
1365   \set instrumentName = "Alto"  % Wrong!
1366   d4 d 
1367  }
1368 >>
1369 @end lilypond
1370
1371 Remember the default context name is Voice, so the second
1372 @code{\set} command set the property @code{instrumentName} in the
1373 Voice context to @qq{Alto}, but as LilyPond does not look
1374 for any such property in the @code{Voice} context, no 
1375 further action took place.  This is not an error, and no error 
1376 message is logged in the log file.
1377
1378 Similarly, if the property name is mis-spelt no error message 
1379 is produced, and clearly the expected action cannot be performed.
1380 If fact, you can set any (fictitious) @q{property} using any 
1381 name you like in any context that exists by using the 
1382 @code{\set} command.  But if the name is not
1383 known to LilyPond it will not cause any action to be taken.
1384 This is one of the reasons why it is highly recommended to
1385 use a context-sensitive editor with syntax highlighting for
1386 editing LilyPond files, such as Vim, Jedit, ConTEXT or Emacs,
1387 since unknown property names will be highlighted differently.
1388
1389 The @code{instrumentName} property will take effect only
1390 if it is set in the @code{Staff} context, but
1391 some properties can be set in more than one context.
1392 For example, the property @code{extraNatural} is by
1393 default set to ##t (true) for all staves.  
1394 If it is set to ##f (false) in the @code{Staff} context 
1395 it applies just to the accidentals on that staff.  
1396 If it is set to false in the @code{Score} context
1397 it applies to all staves.
1398
1399 So this turns off extra naturals in one staff:
1400
1401 @lilypond[quote,verbatim,ragged-right]
1402 <<
1403   \new Staff \relative c'' {
1404     ais4 aes
1405  }
1406   \new Staff \relative c'' {
1407     \set Staff.extraNatural = ##f
1408     ais4 aes
1409  }
1410 >>
1411 @end lilypond
1412
1413 @noindent
1414 and this turns them off in all staves:
1415
1416 @lilypond[quote,verbatim,ragged-right]
1417 <<
1418   \new Staff \relative c'' {
1419     ais4 aes
1420  }
1421   \new Staff \relative c'' {
1422     \set Score.extraNatural = ##f
1423     ais4 aes
1424  }
1425 >>
1426 @end lilypond
1427
1428 The value of every property set in this way can be reset
1429 to its original value with the @code{\unset} command.
1430  
1431 The @code{\set} and @code{\unset} commands can appear anywhere
1432 in the input file and will take effect from the time they are
1433 encountered until the end of the score or until the property is 
1434 @code{\set} or @code{\unset} again.  Let's try changing the 
1435 font size, which affects the size of the note heads (among 
1436 other things) several times.
1437
1438 @lilypond[quote,verbatim,ragged-right,relative=1,fragment]
1439 c4 
1440 % make noteheads smaller
1441 \set fontSize = #-4
1442 d e
1443 % make noteheads larger
1444 \set fontSize = #2.5
1445 f g
1446 % return to original size
1447 \unset fontSize
1448 a b
1449 @end lilypond
1450
1451 We have now seen how to set the values of several different
1452 types of property.  Note that integers and numbers are alway 
1453 preceded by a hash sign, @code{#}, while a true or false value 
1454 is specified by ##t and ##f, with two hash signs.  A text 
1455 property should be enclosed in double quotation signs, as above, 
1456 although we shall see later that text can actually be specified
1457 in a much more general way by using the very powerful 
1458 @code{markup} command. 
1459
1460
1461 @funindex \with
1462
1463 Context properties may also be set at the time the context is
1464 created.  Sometimes this is a clearer way of specifying a 
1465 property value if it is to remain fixed for the duration of
1466 the context.  When a context is created with a @code{\new}
1467 command it may be immediately followed by a 
1468 @code{\with @{ .. @}} block in which the property values are
1469 set.  For example, if we wish to suppress the printing of
1470 extra naturals for the duration of a staff we would write:
1471
1472 @lilypond[quote,verbatim,ragged-right]
1473 \new Staff \with {
1474   extraNatural = ##f
1475 }
1476 \relative c' {
1477   gis ges aes ais
1478 }
1479 @end lilypond
1480
1481 In effect this overrides the default value of the property.  It
1482 may still be changed dynamically using @code{\set} and returned
1483 to its (new) default value with @code{\unset}.
1484
1485 @node Adding and removing engravers
1486 @subsection Adding and removing engravers
1487
1488 @cindex Engravers, adding
1489 @cindex Engravers, removing
1490
1491 @funindex \consists
1492 @funindex \remove
1493
1494 We have seen that contexts each contain several engravers, each
1495 of which is responsible for producing a particular part of the
1496 output, like barlines, staves, note heads, stems, etc.  If an
1497 engraver is removed from a context it can no longer produce its
1498 output.  This is a crude way of modifying the output, but it
1499 can sometimes be useful.  
1500
1501 To remove an engraver we can use the @code{\with} command placed
1502 immediately after the context creation command, as in the 
1503 previous section.
1504
1505 As an 
1506 illustration let's repeat an example from the previous 
1507 section with the staff lines removed.  Remember that the 
1508 staff lines are produced by the Staff_symbol_engraver.
1509
1510 @lilypond[quote,verbatim,ragged-right]
1511 \new Staff \with {
1512   \remove Staff_symbol_engraver
1513 }
1514 \relative c' {
1515   c4 
1516   \set fontSize = #-4  % make noteheads smaller
1517   d e
1518   \set fontSize = #2.5  % make noteheads larger
1519   f g
1520   \unset fontSize  % return to original size
1521   a b
1522 }
1523 @end lilypond
1524
1525 @cindex ambitus engraver
1526
1527 Engravers can also be added to contexts.  The command
1528 to do this is 
1529
1530 @code{\consists @emph{Engraver_name}},
1531
1532 placed inside a @code{\with} block.  Some vocal scores
1533 have an @rglos{ambitus} placed at the beginning of a
1534 staff to indicate the range of notes in that staff.
1535 The ambitus is produced by the @code{Ambitus_engraver},
1536 which is not normally included in any context.  If
1537 we add it to the @code{Voice} context it calculates
1538 the range from that voice only:
1539
1540 @lilypond[quote,verbatim,ragged-right]
1541 \new Staff <<
1542   \new Voice \with {
1543     \consists Ambitus_engraver
1544   }
1545   \relative c'' { 
1546     \voiceOne
1547     c a b g 
1548   }
1549   \new Voice
1550   \relative c' {
1551     \voiceTwo
1552     c e d f
1553   }
1554 >>
1555 @end lilypond
1556
1557 @noindent
1558 but if we add the Ambitus engraver to the 
1559 @code{Staff} context it calculates the range from all
1560 the notes in all the voices on that staff:
1561
1562 @lilypond[quote,verbatim,ragged-right]
1563 \new Staff \with {
1564     \consists Ambitus_engraver
1565   }
1566   <<
1567   \new Voice
1568   \relative c'' { 
1569     \voiceOne
1570     c a b g 
1571   }
1572   \new Voice
1573   \relative c' {
1574     \voiceTwo
1575     c e d f
1576   }
1577 >>
1578 @end lilypond
1579
1580
1581 @node Extending the templates
1582 @section Extending the templates
1583
1584 You've read the tutorial, you know how to write music, you 
1585 understand the fundamental concepts.  But how can you
1586 get the staves that you want?  Well, you can find lots of 
1587 templates (see @ref{Templates}) which may give you a start.  
1588 But what
1589 if you want something that isn't covered there?  Read on.
1590
1591 TODO Add links to templates after they have been moved to LSR
1592
1593 @menu
1594 * Soprano and cello::           
1595 * Four-part SATB vocal score::  
1596 * Building a score from scratch::  
1597 @end menu
1598
1599 @node Soprano and cello
1600 @subsection Soprano and cello
1601
1602 Start off with the template that seems closest to what you want to end
1603 up with.  Let's say that you want to write something for soprano and
1604 cello.  In this case, we would start with @q{Notes and lyrics} (for the
1605 soprano part).
1606
1607 @example
1608 \version "2.11.23"
1609 melody = \relative c' @{
1610   \clef treble
1611   \key c \major
1612   \time 4/4
1613
1614   a4 b c d
1615 @}
1616
1617 text = \lyricmode @{
1618   Aaa Bee Cee Dee
1619 @}
1620
1621 \score @{
1622   <<
1623     \new Voice = "one" @{
1624       \autoBeamOff
1625       \melody
1626     @}
1627     \new Lyrics \lyricsto "one" \text
1628   >>
1629   \layout @{ @}
1630   \midi @{ @}
1631 @}
1632 @end example
1633
1634 Now we want to add a cello part.  Let's look at the @q{Notes only} example:
1635
1636 @example
1637 \version "2.11.23"
1638 melody = \relative c' @{
1639   \clef treble
1640   \key c \major
1641   \time 4/4
1642
1643   a4 b c d
1644 @}
1645
1646 \score @{
1647   \new Staff \melody
1648   \layout @{ @}
1649   \midi @{ @}
1650 @}
1651 @end example
1652
1653 We don't need two @code{\version} commands.  We'll need the @code{melody}
1654 section.  We don't want two @code{\score} sections -- if we had two
1655 @code{\score}s, we'd get the two parts separately.  We want them together,
1656 as a duet.  Within the @code{\score} section, we don't need two
1657 @code{\layout} or @code{\midi}.
1658
1659 If we simply cut and paste the @code{melody} section, we would end up with
1660 two @code{melody} sections.  So let's rename them.  We'll call the section
1661 for the soprano @code{sopranoMusic} and the section for the cello
1662 @code{celloMusic}.  While we're doing this, let's rename @code{text}
1663 to be @code{sopranoLyrics}.  Remember to rename both instances of all
1664 these names -- both the initial definition (the
1665 @code{melody = relative c' @{ } part) and the name's use (in the
1666 @code{\score} section).
1667
1668 While we're doing this, let's change the cello part's staff -- celli
1669 normally use bass clef.  We'll also give the cello some different
1670 notes.
1671
1672 @example
1673 \version "2.11.23"
1674 sopranoMusic = \relative c' @{
1675   \clef treble
1676   \key c \major
1677   \time 4/4
1678
1679   a4 b c d
1680 @}
1681
1682 sopranoLyrics = \lyricmode @{
1683   Aaa Bee Cee Dee
1684 @}
1685
1686 celloMusic = \relative c @{
1687   \clef bass
1688   \key c \major
1689   \time 4/4
1690
1691   d4 g fis8 e d4
1692 @}
1693
1694 \score @{
1695   <<
1696     \new Voice = "one" @{
1697       \autoBeamOff
1698       \sopranoMusic
1699     @}
1700     \new Lyrics \lyricsto "one" \sopranoLyrics
1701   >>
1702   \layout @{ @}
1703   \midi @{ @}
1704 @}
1705 @end example
1706
1707 This is looking promising, but the cello part won't appear in the
1708 score -- we haven't used it in the @code{\score} section.  If we
1709 want the cello part to appear under the soprano part, we need to add
1710
1711 @example
1712 \new Staff \celloMusic
1713 @end example
1714
1715 @noindent
1716 underneath the soprano stuff.  We also need to add @code{<<} and
1717 @code{>>} around the music -- that tells LilyPond that there's
1718 more than one thing (in this case, two @code{Staves}) happening 
1719 at once.  The @code{\score} looks like this now
1720
1721 @c Indentation in this example is deliberately poor
1722 @example
1723 \score @{
1724   <<
1725   <<
1726     \new Voice = "one" @{
1727       \autoBeamOff
1728       \sopranoMusic
1729     @}
1730     \new Lyrics \lyricsto "one" \sopranoLyrics
1731   >>
1732   \new Staff \celloMusic
1733   >>
1734   \layout @{ @}
1735   \midi @{ @}
1736 @}
1737 @end example
1738
1739 @noindent
1740 This looks a bit messy; the indentation is messed up now.  That is
1741 easily fixed.  Here's the complete soprano and cello template.
1742
1743 @lilypond[quote,verbatim,ragged-right]
1744 \version "2.11.23"
1745 sopranoMusic = \relative c' {
1746   \clef treble
1747   \key c \major
1748   \time 4/4
1749
1750   a4 b c d
1751 }
1752
1753 sopranoLyrics = \lyricmode {
1754   Aaa Bee Cee Dee
1755 }
1756
1757 celloMusic = \relative c {
1758   \clef bass
1759   \key c \major
1760   \time 4/4
1761
1762   d4 g fis8 e d4
1763 }
1764
1765 \score {
1766   <<
1767     <<
1768       \new Voice = "one" {
1769         \autoBeamOff
1770         \sopranoMusic
1771       }
1772       \new Lyrics \lyricsto "one" \sopranoLyrics
1773     >>
1774     \new Staff \celloMusic
1775   >>
1776   \layout { }
1777   \midi { }
1778 }
1779 @end lilypond
1780
1781
1782 @node Four-part SATB vocal score
1783 @subsection Four-part SATB vocal score
1784
1785 Most vocal scores of music written for four-part mixed choir 
1786 with orchestral accompaniment such as Mendelssohn's Elijah or
1787 Handel's Messiah have the choral music and words on four
1788 staves, one for each of SATB, with a piano reduction of the
1789 orchestral accompaniment underneath.  Here's an example
1790 from Handel's Messiah:
1791
1792 @c The following should appear as music without code
1793 @lilypond[quote,ragged-right]
1794 \version "2.11.23"
1795 global = { \key d \major \time 4/4 }
1796 sopMusic = \relative c'' {
1797   \clef "treble"
1798   r4 d2 a4 | d4. d8 a2 | cis4 d cis2 |
1799 }
1800 sopWords = \lyricmode {
1801   Wor -- thy is the lamb that was slain
1802 }
1803 altoMusic = \relative a' {
1804   \clef "treble"
1805   r4 a2 a4 | fis4. fis8 a2 | g4 fis fis2 |
1806 }
1807 altoWords = \sopWords
1808 tenorMusic = \relative c' {
1809   \clef "G_8"
1810   r4 fis2 e4 | d4. d8 d2 | e4 a, cis2 |
1811 }
1812 tenorWords = \sopWords
1813 bassMusic = \relative c' {
1814   \clef "bass"
1815   r4 d2 cis4 | b4. b8 fis2 | e4 d a'2 |
1816 }
1817 bassWords = \sopWords
1818 upper = \relative a' {
1819   \clef "treble"
1820   \global
1821   r4 <a d fis>2 <a e' a>4 |
1822   <d fis d'>4. <d fis d'>8 <a d a'>2 |
1823   <g cis g'>4 <a d fis> <a cis e>2 |
1824 }
1825 lower = \relative c, {
1826   \clef "bass"
1827   \global
1828   <d d'>4 <d d'>2 <cis cis'>4 |
1829   <b b'>4. <b' b'>8 <fis fis'>2 |
1830   <e e'>4 <d d'> <a' a'>2 |
1831 }
1832
1833 \score {
1834   <<  % combine ChoirStaff and PianoStaff in parallel
1835     \new ChoirStaff <<
1836       \new Staff = "sopranos" <<
1837         \set Staff.instrumentName = "Soprano"
1838         \new Voice = "sopranos" { \global \sopMusic }
1839       >>
1840       \new Lyrics \lyricsto "sopranos" { \sopWords }
1841       \new Staff = "altos" <<
1842         \set Staff.instrumentName = "Alto"
1843         \new Voice = "altos" { \global \altoMusic }
1844       >>
1845       \new Lyrics \lyricsto "altos" { \altoWords }
1846       \new Staff = "tenors" <<
1847         \set Staff.instrumentName = "Tenor"
1848         \new Voice = "tenors" { \global \tenorMusic }
1849       >>
1850       \new Lyrics \lyricsto "tenors" { \tenorWords }
1851       \new Staff = "basses" <<
1852         \set Staff.instrumentName = "Bass"
1853         \new Voice = "basses" { \global \bassMusic }
1854       >>
1855       \new Lyrics \lyricsto "basses" { \bassWords }
1856     >>  % end ChoirStaff
1857
1858     \new PianoStaff <<
1859       \set PianoStaff.instrumentName = "Piano  "
1860       \new Staff = "upper" \upper
1861       \new Staff = "lower" \lower
1862     >>
1863   >>
1864 }
1865 @end lilypond
1866
1867 None of the templates provides this layout exactly.  The
1868 nearest is @q{SATB vocal score and automatic piano reduction},
1869 but we need to change the layout and add a piano
1870 accompaniment which is not derived automatically from the
1871 vocal parts.  The variables holding the music and words for
1872 the vocal parts are fine, but we shall need to add variables for
1873 the piano reduction.
1874
1875 The order in which the contexts appear in the ChoirStaff of
1876 the template do not correspond with the order in the vocal 
1877 score shown above.  We need to rearrange them so there are
1878 four staves with the words written directly underneath the
1879 notes for each part.
1880 All the voices should be @code{\voiceOne}, which is
1881 the default, so the @code{\voiceXXX} commands should be removed.
1882 We also need to specify the tenor clef for the tenors.
1883 The way in which lyrics are specified in the template has not yet
1884 been encountered so we need to use the method with which we are 
1885 familiar.  We should also add the names of each staff.
1886
1887 Doing this gives for our ChoirStaff:
1888
1889 @example
1890     \new ChoirStaff <<
1891       \new Staff = "sopranos" <<
1892         \set Staff.instrumentName = "Soprano"
1893         \new Voice = "sopranos" @{ \global \sopMusic @}
1894       >>
1895       \new Lyrics \lyricsto "sopranos" @{ \sopWords @}
1896       \new Staff = "altos" <<
1897         \set Staff.instrumentName = "Alto"
1898         \new Voice = "altos" @{ \global \altoMusic @}
1899       >>
1900       \new Lyrics \lyricsto "altos" @{ \altoWords @}
1901       \new Staff = "tenors" <<
1902         \set Staff.instrumentName = "Tenor"
1903         \new Voice = "tenors" @{ \global \tenorMusic @}
1904       >>
1905       \new Lyrics \lyricsto "tenors" @{ \tenorWords @}
1906       \new Staff = "basses" <<
1907         \set Staff.instrumentName = "Bass"
1908         \new Voice = "basses" @{ \global \bassMusic @}
1909       >>
1910       \new Lyrics \lyricsto "basses" @{ \bassWords @}
1911     >>  % end ChoirStaff
1912 @end example
1913
1914 Next we must work out the piano part.  This is
1915 easy - we just pull out the piano part from the
1916 @q{Solo piano} template:
1917
1918 @example
1919 \new PianoStaff <<
1920   \set PianoStaff.instrumentName = "Piano  "
1921   \new Staff = "upper" \upper
1922   \new Staff = "lower" \lower
1923 >>
1924 @end example
1925
1926 and add the variable definitions for @code{upper}
1927 and @code{lower}.
1928
1929 The ChoirStaff and PianoStaff must be combined
1930 using angle brackets as we want them to be
1931 stacked one above the other:
1932
1933 @example
1934 <<  % combine ChoirStaff and PianoStaff one above the other 
1935   \new ChoirStaff <<
1936     \new Staff = "sopranos" <<
1937       \new Voice = "sopranos" @{ \global \sopMusic @}
1938     >>
1939     \new Lyrics \lyricsto "sopranos" @{ \sopWords @}
1940     \new Staff = "altos" <<
1941       \new Voice = "altos" @{ \global \altoMusic @}
1942     >>
1943     \new Lyrics \lyricsto "altos" @{ \altoWords @}
1944     \new Staff = "tenors" <<
1945       \clef "G_8"  % tenor clef
1946       \new Voice = "tenors" @{ \global \tenorMusic @}
1947     >>
1948     \new Lyrics \lyricsto "tenors" @{ \tenorWords @}
1949     \new Staff = "basses" <<
1950       \clef "bass"
1951       \new Voice = "basses" @{ \global \bassMusic @}
1952     >>
1953     \new Lyrics \lyricsto "basses" @{ bassWords @}   
1954   >>  % end ChoirStaff
1955
1956   \new PianoStaff <<
1957     \set PianoStaff.instrumentName = "Piano  "
1958     \new Staff = "upper" \upper
1959     \new Staff = "lower" \lower
1960   >>
1961 >>
1962 @end example
1963
1964 Combining all these together and adding the music
1965 for the three bars of the example above gives:
1966
1967 @lilypond[quote,verbatim,ragged-right]
1968 \version "2.11.23"
1969 global = { \key d \major \time 4/4 }
1970 sopMusic = \relative c'' {
1971   \clef "treble"
1972   r4 d2 a4 | d4. d8 a2 | cis4 d cis2 |
1973 }
1974 sopWords = \lyricmode {
1975   Wor -- thy is the lamb that was slain
1976 }
1977 altoMusic = \relative a' {
1978   \clef "treble"
1979   r4 a2 a4 | fis4. fis8 a2 | g4 fis fis2 |
1980 }
1981 altoWords = \sopWords
1982 tenorMusic = \relative c' {
1983   \clef "G_8"
1984   r4 fis2 e4 | d4. d8 d2 | e4 a, cis2 |
1985 }
1986 tenorWords = \sopWords
1987 bassMusic = \relative c' {
1988   \clef "bass"
1989   r4 d2 cis4 | b4. b8 fis2 | e4 d a'2 |
1990 }
1991 bassWords = \sopWords
1992 upper = \relative a' {
1993   \clef "treble"
1994   \global
1995   r4 <a d fis>2 <a e' a>4 |
1996   <d fis d'>4. <d fis d'>8 <a d a'>2 |
1997   <g cis g'>4 <a d fis> <a cis e>2 |
1998 }
1999 lower = \relative c, {
2000   \clef "bass"
2001   \global
2002   <d d'>4 <d d'>2 <cis cis'>4 |
2003   <b b'>4. <b' b'>8 <fis fis'>2 |
2004   <e e'>4 <d d'> <a' a'>2 |
2005 }
2006
2007 \score {
2008   <<  % combine ChoirStaff and PianoStaff in parallel
2009     \new ChoirStaff <<
2010       \new Staff = "sopranos" <<
2011         \set Staff.instrumentName = "Soprano"
2012         \new Voice = "sopranos" { \global \sopMusic }
2013       >>
2014       \new Lyrics \lyricsto "sopranos" { \sopWords }
2015       \new Staff = "altos" <<
2016         \set Staff.instrumentName = "Alto"
2017         \new Voice = "altos" { \global \altoMusic }
2018       >>
2019       \new Lyrics \lyricsto "altos" { \altoWords }
2020       \new Staff = "tenors" <<
2021         \set Staff.instrumentName = "Tenor"
2022         \new Voice = "tenors" { \global \tenorMusic }
2023       >>
2024       \new Lyrics \lyricsto "tenors" { \tenorWords }
2025       \new Staff = "basses" <<
2026         \set Staff.instrumentName = "Bass"
2027         \new Voice = "basses" { \global \bassMusic }
2028       >>
2029       \new Lyrics \lyricsto "basses" { \bassWords }
2030     >>  % end ChoirStaff
2031
2032     \new PianoStaff <<
2033       \set PianoStaff.instrumentName = "Piano  "
2034       \new Staff = "upper" \upper
2035       \new Staff = "lower" \lower
2036     >>
2037   >>
2038 }
2039 @end lilypond
2040   
2041
2042 @node Building a score from scratch
2043 @subsection Building a score from scratch
2044
2045 After gaining some facility with writing LilyPond code you
2046 may find that it is easier to build a score from scratch
2047 rather than modifying one of the templates.  You can also
2048 develop your own style this way to suit the sort of music you
2049 like.  Let's see how to put together the score for an organ 
2050 prelude as an example.
2051
2052 We begin with a header section.  Here go the title, name
2053 of composer, etc, then come any variable definitions, and
2054 finally the score block.  Let's start with these in outline
2055 and fill in the details later.
2056
2057 We'll use the first two bars of Bach's prelude
2058 based on @emph{Jesu, meine Freude} which is written for two
2059 manuals and pedal organ.  You can see these two bars of music
2060 at the bottom of this section.  The top manual part has two voices,
2061 the lower and pedal organ one each.  So we need four
2062 music definitions and one to define the time signature
2063 and key:
2064
2065 @example
2066 \version "2.11.23"
2067 \header @{
2068   title = "Jesu, meine Freude"
2069   composer = "J S Bach"
2070 @}
2071 TimeKey = @{ \time 4/4 \key c \minor @}
2072 ManualOneVoiceOneMusic = @{s1@}
2073 ManualOneVoiceTwoMusic = @{s1@}
2074 ManualTwoMusic = @{s1@}
2075 PedalOrganMusic = @{s1@}
2076
2077 \score @{
2078 @}
2079 @end example
2080
2081 For now we've just used a spacer note, @code{s1},
2082 instead of the real music.  We'll add that later.
2083
2084 Next let's see what should go in the score block.
2085 We simply mirror the staff structure we want.
2086 Organ music is usually written on three staves,
2087 one for each manual and one for the pedals.  The
2088 manual staves should be bracketed together so we
2089 need to use a PianoStaff for them.  The first
2090 manual part needs two voices and the second manual
2091 part just one.
2092
2093 @example
2094   \new PianoStaff <<
2095     \new Staff = "ManualOne" <<
2096       \new Voice @{ \ManualOneVoiceOneMusic @}
2097       \new Voice @{ \ManualOneVoiceTwoMusic @}
2098     >>  % end ManualOne Staff context
2099     \new Staff = "ManualTwo" <<
2100       \new Voice @{ \ManualTwoMusic @}
2101     >>  % end ManualTwo Staff context
2102   >>  % end PianoStaff context
2103 @end example
2104
2105 Next we need to add a staff for the pedal organ.
2106 This goes underneath the PianoStaff, but it must
2107 be simultaneous with it, so we need angle brackets
2108 round the two.  Missing these out would generate
2109 an error in the log file.  It's a common mistake 
2110 which you'll make sooner or later!  Try copying
2111 the final example at the end of this section,
2112 remove these angle brackets, and compile it to
2113 see what errors it generates.
2114
2115 @example
2116 <<  % PianoStaff and Pedal Staff must be simultaneous
2117   \new PianoStaff <<
2118     \new Staff = "ManualOne" <<
2119       \new Voice @{ \ManualOneVoiceOneMusic @}
2120       \new Voice @{ \ManualOneVoiceTwoMusic @}
2121     >>  % end ManualOne Staff context
2122     \new Staff = "ManualTwo" <<
2123       \new Voice @{ \ManualTwoMusic @}
2124     >>  % end ManualTwo Staff context
2125   >>  % end PianoStaff context
2126   \new Staff = "PedalOrgan" <<
2127     \new Voice @{ \PedalOrganMusic @}
2128   >>
2129 >>
2130 @end example
2131
2132 It is not strictly necessary to use the simultaneous construct
2133 @code{<<  >>} for the manual two staff and the pedal organ staff,
2134 since they contain only one music expression, but it does no harm
2135 and always using angle brackets after @code{\new Staff} is a good
2136 habit to cultivate in case there are multiple voices.  
2137
2138 Let's add this structure to the score block, and adjust the
2139 indenting.  We also add the appropriate clefs, ensure the
2140 second voice stems point down with @code{\voiceTwo} and
2141 enter the time signature and key to each staff using our
2142 predefined variable, @code{\TimeKey}.
2143
2144 @example
2145 \score @{
2146   <<  % PianoStaff and Pedal Staff must be simultaneous
2147     \new PianoStaff <<
2148       \new Staff = "ManualOne" <<
2149         \TimeKey  % set time signature and key
2150         \clef "treble"
2151         \new Voice @{ \ManualOneVoiceOneMusic @}
2152         \new Voice @{ \voiceTwo \ManualOneVoiceTwoMusic @}
2153       >>  % end ManualOne Staff context
2154       \new Staff = "ManualTwo" <<
2155         \TimeKey
2156         \clef "bass"
2157         \new Voice @{ \ManualTwoMusic @}
2158       >>  % end ManualTwo Staff context
2159     >>  % end PianoStaff context
2160     \new Staff = "PedalOrgan" <<
2161       \TimeKey
2162       \clef "bass"
2163       \new Voice @{ \PedalOrganMusic @}
2164     >>  % end PedalOrgan Staff
2165   >>
2166 @}  % end Score context
2167 @end example
2168
2169 That completes the structure.  Any three-staff organ music
2170 will have a similar structure, although the number of voices
2171 may vary.  All that remains now 
2172 is to add the music, and combine all the parts together.
2173
2174 @lilypond[quote,verbatim,ragged-right]
2175 \version "2.11.23"
2176 \header {
2177   title = "Jesu, meine Freude"
2178   composer = "J S Bach"
2179 }
2180 TimeKey = { \time 4/4 \key c \minor }
2181 ManualOneVoiceOneMusic = \relative g' {
2182   g4 g f ees | d2 c2 |
2183 }
2184 ManualOneVoiceTwoMusic = \relative c' {
2185   ees16 d ees8~ ees16 f ees s c8 d~ d c~ |
2186   c c4 b8 c8. g16 c b c d |
2187 }
2188 ManualTwoMusic = \relative c' {
2189   c16 b c8~ c16 b c g a8 g~ g16 g aes ees |
2190   f ees f d g aes g f ees d e8~ ees16 f ees d |
2191 }
2192 PedalOrganMusic = \relative c {
2193   r8 c16 d ees d ees8~ ees16 a, b g c b c8 |
2194   r16 g ees f g f g8 c,2 |
2195   }
2196
2197 \score {
2198   <<  % PianoStaff and Pedal Staff must be simultaneous
2199     \new PianoStaff <<
2200       \new Staff = "ManualOne" <<
2201         \TimeKey  % set time signature and key
2202         \clef "treble"
2203         \new Voice { \ManualOneVoiceOneMusic }
2204         \new Voice { \voiceTwo \ManualOneVoiceTwoMusic }
2205       >>  % end ManualOne Staff context
2206       \new Staff = "ManualTwo" <<
2207         \TimeKey
2208         \clef "bass"
2209         \new Voice { \ManualTwoMusic }
2210       >>  % end ManualTwo Staff context
2211     >>  % end PianoStaff context
2212     \new Staff = "PedalOrgan" <<
2213       \TimeKey
2214       \clef "bass"
2215       \new Voice { \PedalOrganMusic }
2216     >>  % end PedalOrgan Staff
2217   >>
2218 }  % end Score context
2219 @end lilypond
2220
2221
2222