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