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