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