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