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