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