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