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