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