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