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