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