]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/user/changing-defaults.itely
Use two spaces after a period; thanks Michael Rasmussen!
[lilypond.git] / Documentation / user / changing-defaults.itely
1 @c -*- coding: utf-8; mode: texinfo; -*-
2 @c This file is part of lilypond.tely
3 @ignore
4     Translation of GIT committish: FILL-IN-HEAD-COMMITTISH
5
6     When revising a translation, copy the HEAD committish of the
7     version that you are working on.  See TRANSLATION for details.
8 @end ignore
9
10 @node Changing defaults
11 @chapter Changing defaults
12
13
14 The purpose of LilyPond's design is to provide the finest output
15 quality as a default.  Nevertheless, it may happen that you need to
16 change this default layout.  The layout is controlled through a large
17 number of proverbial @q{knobs and switches.}  This chapter does not
18 list each and every knob.  Rather, it outlines what groups of controls
19 are available and explains how to lookup which knob to use for a
20 particular effect.
21
22
23 @cindex Program reference
24
25 The controls available for tuning are described in a separate
26 document, the
27 @iftex
28 Program reference manual.
29 @end iftex
30 @ifnottex
31 @ref{Top,Program reference,,lilypond-internals}.
32 @end ifnottex
33 That manual
34 lists all different variables, functions and options available in
35 LilyPond.  It is written as a HTML document, which is available
36 @c leave the @uref as one long line.
37 @uref{http://@/lilypond@/.org/@/doc/@/stable/@/Documentation/@/user/@/lilypond@/-internals/,on@/-line},
38 but is also included with the LilyPond documentation package.
39
40 There are four areas where the default settings may be changed:
41
42 @itemize @bullet
43 @item
44 Automatic notation: changing the automatic creation of notation
45 elements.  For example, changing the beaming rules.
46
47 @item
48 Output: changing the appearance of individual
49 objects.  For example, changing stem directions or the location of
50 subscripts.
51
52 @item
53 Context: changing aspects of the translation from music events to
54 notation.  For example, giving each staff a separate time signature.
55
56 @item
57 Page layout: changing the appearance of the spacing, line
58 breaks, and page dimensions.  These modifications are discussed
59 in @ref{Non-musical notation}, and @ref{Spacing issues}.
60 @end itemize
61
62 Internally, LilyPond uses Scheme (a LISP dialect) to provide
63 infrastructure.  Overriding layout decisions in effect accesses the
64 program internals, which requires Scheme input.  Scheme elements are
65 introduced in a @code{.ly} file with the hash mark
66 @code{#}.@footnote{@rlearning{Scheme tutorial}, contains a short tutorial
67 on entering numbers, lists, strings, and symbols in Scheme.}
68
69
70 @menu
71 * Automatic notation::          
72 * Interpretation contexts::     
73 * The \override command::       
74 @end menu
75
76
77 @node Automatic notation
78 @section Automatic notation
79
80 This section describes how to change the way that accidentals and
81 beams are automatically displayed.
82
83 @menu
84 * Automatic accidentals::       
85 * Setting automatic beam behavior::  
86 @end menu
87
88 @node Automatic accidentals
89 @subsection Automatic accidentals
90 @cindex Automatic accidentals
91
92 Common rules for typesetting accidentals have been placed in a
93 function.  This function is called as follows
94
95 @funindex set-accidental-style
96 @example
97 #(set-accidental-style 'STYLE)
98 @end example
99
100 @c TODO: check the context stuff below
101 @c -does it *really* work?
102 @c -the default contexts as specified in
103 @c  scm/music-function.scm seem to be different -vv
104
105 Optionally, the function can take two arguments: the name of the
106 accidental style, and an optional argument that denotes the context that
107 should be changed:
108
109 @example
110 #(set-accidental-style 'STYLE #('CONTEXT#))
111 @end example
112
113 If no context name is supplied, @code{Staff} is the default,
114 but you may wish to apply the accidental style to a single @code{Voice}
115 instead.
116
117 The following accidental styles are supported:
118
119 @table @code
120 @item default
121 This is the default typesetting behavior.  It corresponds
122 to 18th century common practice: Accidentals are
123 remembered to the end of the measure in which they occur and
124 only on their own octave.
125
126 @lilypond[quote,ragged-right]
127 musicA = {  << \relative {  cis'8 fis, d'4 <a cis>8 f bis4 | cis2. <c, g'>4 | } \\
128        \relative { ais'2 cis, | fis8 b a4 cis2 | } >> }
129
130 musicB = { \clef bass  \new Voice { \voiceTwo \relative { < fis, a cis>4 
131         \change Staff = up cis' \change Staff = down <fis, a>  
132         \change Staff = up dis' | \change Staff = down <fis, a cis>4 gis 
133         <f a d>2 |  } }}
134
135 \score { 
136         \new PianoStaff {
137        << \context Staff = "up" {
138                %#(set-accidental-style 'default) 
139        \musicA }
140        \context Staff = "down"{
141                %#(set-accidental-style 'default)
142        \musicB } >> }
143        \header { piece = \markup {\fill-line { \fontsize #3  "'default" }}}
144 }
145 @end lilypond
146
147 @item voice
148 The normal behavior is to remember the accidentals on
149 Staff-level.  This variable, however, typesets accidentals
150 individually for each voice.  Apart from that, the rule is similar to
151 @code{default}.
152
153 @example
154  \new Staff <<
155         #(set-accidental-style 'voice)
156         @{ @dots{} @}
157         >>
158 @end example
159
160 As a result, accidentals from one voice do not get canceled in other
161 voices, which is often an unwanted result: in the following example, it
162 is hard to determine whether the second @samp{a} should be played
163 natural or sharp.
164
165 @lilypond[quote,ragged-right]
166 musicA = {  << \relative {  cis'8 fis, d'4 <a cis>8 f bis4 | cis2. <c, g'>4 | } \\
167        \relative { ais'2 cis, | fis8 b a4 cis2 | } >> }
168
169 musicB = { \clef bass  \new Voice { \voiceTwo \relative { < fis, a cis>4 
170         \change Staff = up cis' \change Staff = down <fis, a>  
171         \change Staff = up dis' | \change Staff = down <fis, a cis>4 gis 
172         <f a d>2 |  } }}
173
174 \score { 
175         \new PianoStaff {
176        << \context Staff = "up" {
177                #(set-accidental-style 'voice) 
178        \musicA }
179        \context Staff = "down"{
180                #(set-accidental-style 'voice)
181        \musicB } >> }
182        \header { piece = \markup {\fill-line { \fontsize #3  "'voice" }}}
183 }
184 @end lilypond
185
186 The @code{voice} option should be used if the voices
187 are to be read solely by individual musicians.  If the staff is to be
188 used by one musician (e.g., a conductor) then
189 @code{modern} or @code{modern-cautionary}
190 should be used instead.
191
192 @item modern
193 @funindex modern style accidentals
194 This rule corresponds to the common practice in the 20th century.  This rule
195 prints the same accidentals as @code{default}, but temporary
196 accidentals also are canceled in other octaves.  Furthermore,
197 in the same octave, they also get canceled in the following
198 measure: in the following example, notice the two natural signs which appear
199 in the second bar of the upper staff.
200
201 @lilypond[quote,ragged-right]
202 musicA = {  << \relative {  cis'8 fis, d'4 <a cis>8 f bis4 | cis2. <c, g'>4 | } \\
203        \relative { ais'2 cis, | fis8 b a4 cis2 | } >> }
204
205 musicB = { \clef bass  \new Voice { \voiceTwo \relative { < fis, a cis>4 
206         \change Staff = up cis' \change Staff = down <fis, a>  
207         \change Staff = up dis' | \change Staff = down <fis, a cis>4 gis 
208         <f a d>2 |  } }}
209
210 \score { 
211         \new PianoStaff {
212        << \context Staff = "up" {
213                #(set-accidental-style 'modern) 
214        \musicA }
215        \context Staff = "down"{
216                #(set-accidental-style 'modern)
217        \musicB } >> }
218        \header { piece = \markup {\fill-line { \fontsize #3  "'modern" }}}
219 }
220 @end lilypond
221
222 @item @code{modern-cautionary}
223 @funindex modern-cautionary
224 This rule is similar to @code{modern}, but the @q{extra} accidentals
225 (the ones not typeset by @code{default}) are typeset as cautionary
226 accidentals.  They are printed in reduced size or (by default)
227 with parentheses -- this can be set by definig the @code{cautionary-style}
228 property of the @internalsref{AccidentalSuggestion} object.
229
230 @lilypond[quote,ragged-right]
231 musicA = {  << \relative {  cis'8 fis, d'4 <a cis>8 f bis4 | cis2. <c, g'>4 | } \\
232        \relative { ais'2 cis, | fis8 b a4 cis2 | } >> }
233
234 musicB = { \clef bass  \new Voice { \voiceTwo \relative { < fis, a cis>4 
235         \change Staff = up cis' \change Staff = down <fis, a>  
236         \change Staff = up dis' | \change Staff = down <fis, a cis>4 gis 
237         <f a d>2 |  } }}
238
239 \score { 
240         \new PianoStaff {
241        << \context Staff = "up" {
242                #(set-accidental-style 'modern-cautionary) 
243        \musicA }
244        \context Staff = "down"{
245                #(set-accidental-style 'modern-cautionary)
246        \musicB } >> }
247        \header { piece = \markup {\fill-line { \fontsize #3  "'modern-cautionary" }}}
248 }
249 @end lilypond
250
251 @funindex modern-voice
252 @item modern-voice
253 This rule is used for multivoice accidentals to be read both by musicians
254 playing one voice and musicians playing all voices.  Accidentals are
255 typeset for each voice, but they @emph{are} canceled across voices in
256 the same @internalsref{Staff}.
257
258 @lilypond[quote,ragged-right]
259 musicA = {  << \relative {  cis'8 fis, d'4 <a cis>8 f bis4 | cis2. <c, g'>4 | } \\
260        \relative { ais'2 cis, | fis8 b a4 cis2 | } >> }
261
262 musicB = { \clef bass  \new Voice { \voiceTwo \relative { < fis, a cis>4 
263         \change Staff = up cis' \change Staff = down <fis, a>  
264         \change Staff = up dis' | \change Staff = down <fis, a cis>4 gis 
265         <f a d>2 |  } }}
266
267 \score { 
268         \new PianoStaff {
269        << \context Staff = "up" {
270                #(set-accidental-style 'modern-voice) 
271        \musicA }
272        \context Staff = "down"{
273                #(set-accidental-style 'modern-voice)
274        \musicB } >> }
275        \header { piece = \markup {\fill-line { \fontsize #3  "'modern-voice" }}}
276 }
277 @end lilypond
278
279 @funindex modern-voice-cautionary
280 @item modern-voice-cautionary
281 This rule is the same as @code{modern-voice}, but with the extra
282 accidentals (the ones not typeset by @code{voice}) typeset
283 as cautionaries.  Even though all accidentals typeset by
284 @code{default} @emph{are} typeset by this variable,
285 some of them are typeset as cautionaries.
286
287 @lilypond[quote,ragged-right]
288 musicA = {  << \relative {  cis'8 fis, d'4 <a cis>8 f bis4 | cis2. <c, g'>4 | } \\
289        \relative { ais'2 cis, | fis8 b a4 cis2 | } >> }
290
291 musicB = { \clef bass  \new Voice { \voiceTwo \relative { < fis, a cis>4 
292         \change Staff = up cis' \change Staff = down <fis, a>  
293         \change Staff = up dis' | \change Staff = down <fis, a cis>4 gis 
294         <f a d>2 |  } }}
295
296 \score { 
297         \new PianoStaff {
298        << \context Staff = "up" {
299                #(set-accidental-style 'modern-voice-cautionary) 
300        \musicA }
301        \context Staff = "down"{
302                #(set-accidental-style 'modern-voice-cautionary)
303        \musicB } >> }
304        \header { piece = \markup {\fill-line { \fontsize #3  "'modern-voice-cautionary" }}}
305 }
306 @end lilypond
307
308 @item piano
309 @funindex piano accidentals
310 This accidental style takes place in a GrandStaff context.  However, you have to 
311 explicitly set it for @emph{each} individual Staff of the GrandStaff:
312
313 @example
314 \new GrandStaff @{ <<
315   \new Staff = "up" @{ <<
316     #(set-accidental-style 'piano)
317     @{ @dots{} @}
318   >> @}
319   \new Staff = "down"@{ <<
320     #(set-accidental-style 'piano)
321   @{ @dots{} @}
322   >> @}
323 >> @}
324 @end example
325
326 This rule reflects 20th century practice for piano notation.  Its behavior is very
327 similar to @code{modern} style, but here accidentals also get canceled
328 across the staves in the same @internalsref{GrandStaff} or
329 @internalsref{PianoStaff}.
330
331 @lilypond[quote,ragged-right]
332 musicA = {  << \relative {  cis'8 fis, d'4 <a cis>8 f bis4 | cis2. <c, g'>4 | } \\
333        \relative { ais'2 cis, | fis8 b a4 cis2 | } >> }
334
335 musicB = { \clef bass  \new Voice { \voiceTwo \relative { < fis, a cis>4 
336         \change Staff = up cis' \change Staff = down <fis, a>  
337         \change Staff = up dis' | \change Staff = down <fis, a cis>4 gis 
338         <f a d>2 |  } }}
339
340 \score { 
341         \new PianoStaff {
342        << \context Staff = "up" {
343                #(set-accidental-style 'piano) 
344        \musicA }
345        \context Staff = "down"{
346                #(set-accidental-style 'piano)
347        \musicB } >> }
348        \header { piece = \markup {\fill-line { \fontsize #3  "'piano" }}}
349 }
350 @end lilypond
351
352 @item piano-cautionary
353 @funindex #(set-accidental-style 'piano-cautionary)
354 Same as @code{#(set-accidental-style 'piano)} but with the extra
355 accidentals typeset as cautionaries.
356
357 @lilypond[quote,ragged-right]
358 musicA = {  << \relative {  cis'8 fis, d'4 <a cis>8 f bis4 | cis2. <c, g'>4 | } \\
359        \relative { ais'2 cis, | fis8 b a4 cis2 | } >> }
360
361 musicB = { \clef bass  \new Voice { \voiceTwo \relative { < fis, a cis>4 
362         \change Staff = up cis' \change Staff = down <fis, a>  
363         \change Staff = up dis' | \change Staff = down <fis, a cis>4 gis 
364         <f a d>2 |  } }}
365
366 \score { 
367         \new PianoStaff {
368        << \context Staff = "up" {
369                #(set-accidental-style 'piano-cautionary) 
370        \musicA }
371        \context Staff = "down"{
372                #(set-accidental-style 'piano-cautionary)
373        \musicB } >> }
374        \header { piece = \markup {\fill-line { \fontsize #3  "'piano-cautionary" }}}
375 }
376 @end lilypond
377
378 @item no-reset
379 @funindex no-reset accidental style
380 This is the same as @code{default} but with accidentals lasting
381 @q{forever} and not only until the next measure:
382 @lilypond[quote,ragged-right]
383 musicA = {  << \relative {  cis'8 fis, d'4 <a cis>8 f bis4 | cis2. <c, g'>4 | } \\
384        \relative { ais'2 cis, | fis8 b a4 cis2 | } >> }
385
386 musicB = { \clef bass  \new Voice { \voiceTwo \relative { < fis, a cis>4 
387         \change Staff = up cis' \change Staff = down <fis, a>  
388         \change Staff = up dis' | \change Staff = down <fis, a cis>4 gis 
389         <f a d>2 |  } }}
390
391 \score { 
392         \new PianoStaff {
393        << \context Staff = "up" {
394                #(set-accidental-style 'no-reset) 
395        \musicA }
396        \context Staff = "down"{
397                #(set-accidental-style 'no-reset)
398        \musicB } >> }
399        \header { piece = \markup {\fill-line { \fontsize #3  "'no-reset" }}}
400 }
401 @end lilypond
402
403 @item forget
404 This is sort of the opposite of @code{no-reset}: Accidentals
405 are not remembered at all -- and hence all accidentals are
406 typeset relative to the key signature, regardless of what was
407 before in the music
408
409 @lilypond[quote,ragged-right]
410 musicA = {  << \relative {  cis'8 fis, d'4 <a cis>8 f bis4 | cis2. <c, g'>4 | } \\
411        \relative { ais'2 cis, | fis8 b a4 cis2 | } >> }
412
413 musicB = { \clef bass  \new Voice { \voiceTwo \relative { < fis, a cis>4 
414         \change Staff = up cis' \change Staff = down <fis, a>  
415         \change Staff = up dis' | \change Staff = down <fis, a cis>4 gis 
416         <f a d>2 |  } }}
417
418 \score { 
419         \new PianoStaff {
420        << \context Staff = "up" {
421                #(set-accidental-style 'forget) 
422        \musicA }
423        \context Staff = "down"{
424                #(set-accidental-style 'forget)
425        \musicB } >> }
426        \header { piece = \markup {\fill-line { \fontsize #3  "'forget" }}}
427 }
428 @end lilypond
429 @end table
430
431
432 @seealso
433
434 Program reference: @internalsref{Accidental_engraver},
435 @internalsref{Accidental}, @internalsref{AccidentalSuggestion} and @internalsref{AccidentalPlacement}.
436
437
438 @refbugs
439
440 Simultaneous notes are considered to be entered in sequential
441 mode.  This means that in a chord the accidentals are typeset as if the
442 notes in the chord happen one at a time, in the order in which
443 they appear in the input file.  This is a problem when accidentals
444 in a chord depend on each other,
445 which does not happen for the default accidental style.  The problem
446 can be solved by manually inserting @code{!} and @code{?} for the
447 problematic notes.
448
449
450 @node Setting automatic beam behavior
451 @subsection Setting automatic beam behavior
452
453 @funindex autoBeamSettings
454 @funindex (end * * * *)
455 @funindex (begin * * * *)
456 @cindex automatic beams, tuning
457 @cindex tuning automatic beaming
458
459 @c [TODO: use \applyContext]
460
461 In normal time signatures, automatic beams can start on any note but can
462 only end in a few positions within the measure: beams can end on a beat,
463 or at durations specified by the properties in
464 @code{autoBeamSettings}.  The properties in @code{autoBeamSettings}
465 consist of a list of rules for where beams can begin and end.  The
466 default @code{autoBeamSettings} rules are defined in
467 @file{scm/@/auto@/-beam@/.scm}.
468
469 In order to add a rule to the list, use
470 @example
471 #(override-auto-beam-setting '(be p q n m) a b [context])
472 @end example
473
474 @itemize @bullet
475
476 @item @code{be} is either "begin" or "end".
477
478 @item @code{p/q} is the duration of the note for which you want
479 to add a rule.  A beam is considered to have the duration of its
480 shortest note.  Set @code{p} and @code{q} to @code{'*'} to
481 have this apply to any beam.
482
483 @item @code{n/m} is the time signature to which
484 this rule should apply.  Set @code{n} and @code{m} to @code{'*'}
485 to have this apply in any time signature.
486
487 @item @code{a/b} is the position in the bar at which the beam should
488 begin/end.
489
490 @item @code{context} is optional, and it specifies the context at which
491 the change should be made.  The default is @code{'Voice}.
492 @code{#(score-override-auto-beam-setting '(A B C D) E F)} is equivalent to
493 @code{#(override-auto-beam-setting '(A B C D) E F 'Score)}.
494
495 @end itemize
496
497 For example, if automatic beams should always end on the first quarter
498 note, use
499
500 @example
501 #(override-auto-beam-setting '(end * * * *) 1 4)
502 @end example
503
504 You can force the beam settings to only take effect on beams whose shortest
505 note is a certain duration
506
507 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
508 \time 2/4
509 #(override-auto-beam-setting '(end 1 16 * *) 1 16)
510 a16 a a a a a a a |
511 a32 a a a a16 a a a a a |
512 #(override-auto-beam-setting '(end 1 32 * *) 1 16)
513 a32 a a a a16 a a a a a |
514 @end lilypond
515
516 You can force the beam settings to only take effect in certain time
517 signatures
518
519 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
520 \time 5/8
521 #(override-auto-beam-setting '(end * * 5 8) 2 8)
522 c8 c d d d
523 \time 4/4
524 e8 e f f e e d d
525 \time 5/8
526 c8 c d d d
527 @end lilypond
528
529 You can also remove a previously set beam-ending rule by using
530
531 @example
532 #(revert-auto-beam-setting '(be p q n m) a b [context])
533 @end example
534
535 @noindent
536 be, p, q, n, m, a, b and context are the same as above.  Note that the
537 default rules are specified in @file{scm/@/auto@/-beam@/.scm},
538 so you can revert rules that you did not explicitly create.
539
540 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
541 \time 4/4
542 a16 a a a a a a a a a a a a a a a
543 #(revert-auto-beam-setting '(end 1 16 4 4) 1 4)
544 a16 a a a a a a a a a a a a a a a
545 @end lilypond
546
547 The rule in a revert-auto-beam-setting statement must exactly match the
548 original rule.  That is, no wildcard expansion is taken into account.
549
550 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
551 \time 1/4
552 #(override-auto-beam-setting '(end 1 16 1 4) 1 8)
553 a16 a a a
554 #(revert-auto-beam-setting '(end 1 16 * *) 1 8) % this won't revert it!
555 a a a a
556 #(revert-auto-beam-setting '(end 1 16 1 4) 1 8) % this will
557 a a a a
558 @end lilypond
559
560
561
562 @c TODO:  old material -- not covered by above stuff, I think.
563 If automatic beams should end on every quarter in 5/4 time, specify
564 all endings
565 @example
566 #(override-auto-beam-setting '(end * * * *) 1 4 'Staff)
567 #(override-auto-beam-setting '(end * * * *) 1 2 'Staff)
568 #(override-auto-beam-setting '(end * * * *) 3 4 'Staff)
569 #(override-auto-beam-setting '(end * * * *) 5 4 'Staff)
570 @dots{}
571 @end example
572
573 The same syntax can be used to specify beam starting points.  In this
574 example, automatic beams can only end on a dotted quarter note
575 @example
576 #(override-auto-beam-setting '(end * * * *) 3 8)
577 #(override-auto-beam-setting '(end * * * *) 1 2)
578 #(override-auto-beam-setting '(end * * * *) 7 8)
579 @end example
580 In 4/4 time signature, this means that automatic beams could end only on
581 3/8 and on the fourth beat of the measure (after 3/4, that is 2 times
582 3/8, has passed within the measure).
583
584 If any unexpected beam behaviour occurs, check the default automatic beam
585 settings in @file{scm/@/auto@/-beam@/.scm}
586 for possible interference, because the beam
587 endings defined there will still apply on top of your own overrides.  Any
588 unwanted endings in the default vales must be reverted for your time
589 signature(s).
590
591 For example, to typeset @code{(3 4 3 2)}-beam endings in 12/8, begin
592 with
593
594 @example
595 %%% revert default values in scm/auto-beam.scm regarding 12/8 time
596 #(revert-auto-beam-setting '(end * * 12 8) 3 8)
597 #(revert-auto-beam-setting '(end * * 12 8) 3 4)
598 #(revert-auto-beam-setting '(end * * 12 8) 9 8)
599
600 %%% your new values
601 #(override-auto-beam-setting '(end 1 8 12 8) 3 8)
602 #(override-auto-beam-setting '(end 1 8 12 8) 7 8)
603 #(override-auto-beam-setting '(end 1 8 12 8) 10 8)
604 @end example
605
606 @cindex automatic beam generation
607 @cindex autobeam
608 @funindex autoBeaming
609 @cindex lyrics
610
611 If beams are used to indicate melismata in songs, then automatic
612 beaming should be switched off with @code{\autoBeamOff}.
613
614
615 @refcommands
616
617 @funindex \autoBeamOff
618 @code{\autoBeamOff},
619 @funindex \autoBeamOn
620 @code{\autoBeamOn}.
621
622 @commonprop
623
624 Beaming patterns may be altered with the @code{beatGrouping} property,
625
626 @lilypond[quote,verbatim,relative=2,fragment,ragged-right]
627 \time 5/16
628 \set beatGrouping = #'(2 3)
629 c8[^"(2+3)" c16 c8]
630 \set beatGrouping = #'(3 2)
631 c8[^"(3+2)" c16 c8]
632 @end lilypond
633
634
635 @refbugs
636
637 If a score ends while an automatic beam has not been ended and is
638 still accepting notes, this last beam will not be typeset at all.  The
639 same holds polyphonic voices, entered with @code{<< @dots{} \\ @dots{}
640 >>}.  If a polyphonic voice ends while an automatic beam is still
641 accepting notes, it is not typeset.
642
643
644 @node Interpretation contexts
645 @section Interpretation contexts
646
647 This section describes what contexts are, and how to modify them.
648
649 @menu
650 * Contexts explained::          
651 * Creating contexts::           
652 * Changing context properties on the fly::  
653 * Modifying context plug-ins::  
654 * Layout tunings within contexts::  
655 * Changing context default settings::  
656 * Defining new contexts::       
657 * Aligning contexts::           
658 * Vertical grouping of grobs::  
659 @end menu
660
661
662 @node Contexts explained
663 @subsection Contexts explained
664
665 When music is printed, a lot of notational elements must be added to the
666 output.  For example, compare the input and output of the following example:
667
668 @lilypond[quote,verbatim,relative=2,fragment]
669 cis4 cis2. g4
670 @end lilypond
671
672 The input is rather sparse, but in the output, bar lines, accidentals,
673 clef, and time signature are added.  LilyPond @emph{interprets} the
674 input.  During this step, the musical information is inspected in time
675 order, similar to reading a score from left to right.  While reading
676 the input, the program remembers where measure boundaries are, and which
677 pitches require explicit accidentals.  This information can be presented on
678 several levels.  For example, the effect of an accidental is limited
679 to a single staff, while a bar line must be synchronized across the
680 entire score.
681
682 Within LilyPond, these rules and bits of information are grouped in
683 @emph{Contexts}.  Some examples of contexts are @context{Voice},
684 @context{Staff}, and @context{Score}.  They are hierarchical, for
685 example: a @context{Staff} can contain many @context{Voice}s, and a
686 @context{Score} can contain many @context{Staff} contexts.
687
688 @quotation
689 @image{context-example,5cm,,}
690 @end quotation
691
692 Each context has the responsibility for enforcing some notation rules,
693 creating some notation objects and maintaining the associated
694 properties.  For example, the @context{Voice} context may introduce an
695 accidental and then the @context{Staff} context maintains the rule to
696 show or suppress the accidental for the remainder of the measure.  The
697 synchronization of bar lines is handled at @context{Score} context.
698
699 However, in some music we may not want the bar lines to be
700 synchronized -- consider a polymetric score in 4/4 and 3/4 time.  In
701 such cases, we must modify the default settings of the @context{Score}
702 and @context{Staff} contexts.
703
704 For very simple scores, contexts are created implicitly, and you need
705 not be aware of them.  For larger pieces, such as anything with more
706 than one staff, they must be
707 created explicitly to make sure that you get as many staves as you
708 need, and that they are in the correct order.  For typesetting pieces
709 with specialized notation, it can be useful to modify existing or
710 to define new contexts.
711
712
713 A complete description of all available contexts is in the program
714 reference, see
715 @ifhtml
716 @internalsref{Contexts}.
717 @end ifhtml
718 @ifnothtml
719 Translation @expansion{} Context.
720 @end ifnothtml
721
722 @c [TODO: describe propagation]
723
724
725 @node Creating contexts
726 @subsection Creating contexts
727
728 For scores with only one voice and one staff, contexts are
729 created automatically.  For more complex scores, it is necessary to
730 create them by hand.  There are three commands that do this.
731
732 @itemize @bullet
733
734 @item
735 The easiest command is @code{\new}, and it also the quickest to type.
736 It is prepended to a music expression, for example
737
738 @funindex \new
739 @cindex new contexts
740 @cindex Context, creating
741
742 @example
743 \new @var{type} @var{music expression}
744 @end example
745
746 @noindent
747 where @var{type} is a context name (like @code{Staff} or
748 @code{Voice}).  This command creates a new context, and starts
749 interpreting the @var{music expression} with that.
750
751 A practical application of @code{\new} is a score with many
752 staves.  Each part that should be on its own staff, is preceded with
753 @code{\new Staff}.
754
755 @lilypond[quote,verbatim,relative=2,ragged-right,fragment]
756 <<
757   \new Staff { c4 c }
758   \new Staff { d4 d }
759 >>
760 @end lilypond
761
762 The @code{\new} command may also give a name to the context,
763
764 @example
765 \new @var{type} = @var{id} @var{music}
766 @end example
767 However, this user specified name is only used if there is no other
768 context already earlier with the same name.
769
770
771 @funindex \context
772
773 @item
774 Like @code{\new}, the @code{\context} command also directs a music
775 expression to a context object, but gives the context an explicit name.  The
776 syntax is
777
778 @example
779 \context @var{type} = @var{id} @var{music}
780 @end example
781
782 This form will search for an existing context of type @var{type}
783 called @var{id}.  If that context does not exist yet, a new
784 context with the specified name is created.  This is useful if
785 the context is referred to later on.  For example, when
786 setting lyrics the melody is in a named context
787
788 @example
789 \context Voice = "@b{tenor}" @var{music}
790 @end example
791
792 @noindent
793 so the texts can be properly aligned to its notes,
794
795 @example
796 \new Lyrics \lyricsto "@b{tenor}" @var{lyrics}
797 @end example
798
799 @noindent
800
801 Another possible use of named contexts is funneling two different
802 music expressions into one context.  In the following example,
803 articulations and notes are entered separately,
804
805 @example
806 music = @{ c4 c4 @}
807 arts = @{ s4-. s4-> @}
808 @end example
809
810 They are combined by sending both to the same @context{Voice} context,
811
812 @example
813 <<
814   \new Staff \context Voice = "A" \music
815   \context Voice = "A" \arts
816 >>
817 @end example
818 @lilypond[quote,ragged-right]
819 music = { c4 c4 }
820 arts = { s4-. s4-> }
821 \relative c'' <<
822   \new Staff \context Voice = "A" \music
823   \context Voice = "A" \arts
824 >>
825 @end lilypond
826
827 With this mechanism, it is possible to define an Urtext (original
828 edition), with the option to put several distinct articulations on the
829 same notes.
830
831 @cindex creating contexts
832
833 @item
834 The third command for creating contexts is
835 @example
836 \context @var{type} @var{music}
837 @end example
838
839
840 @noindent
841 This is similar to @code{\context} with @code{= @var{id}}, but matches
842 any context of type @var{type}, regardless of its given name.
843
844 This variant is used with music expressions that can be interpreted at
845 several levels.  For example, the @code{\applyOutput} command (see
846 @ref{Running a function on all layout objects}).  Without an explicit
847 @code{\context}, it is usually applied to @context{Voice}
848
849 @example
850 \applyOutput #'@var{context} #@var{function}   % apply to Voice
851 @end example
852
853 To have it interpreted at the @context{Score} or @context{Staff} level use
854 these forms
855
856 @example
857 \applyOutput #'Score #@var{function}
858 \applyOutput #'Staff #@var{function}
859 @end example
860
861 @end itemize
862
863
864 @node Changing context properties on the fly
865 @subsection Changing context properties on the fly
866
867 @cindex properties
868 @funindex \set
869 @cindex changing properties
870
871 Each context can have different @emph{properties}, variables contained
872 in that context.  They can be changed during the interpretation step.
873 This is achieved by inserting the @code{\set} command in the music,
874
875 @example
876 \set @var{context}.@var{prop} = #@var{value}
877 @end example
878
879 For example,
880 @lilypond[quote,verbatim,relative=2,fragment]
881 R1*2
882 \set Score.skipBars = ##t
883 R1*2
884 @end lilypond
885
886 This command skips measures that have no notes.  The result is that
887 multi-rests are condensed.  The value assigned is a Scheme object.  In
888 this case, it is @code{#t}, the boolean True value.
889
890 If the @var{context} argument is left out, then the current bottom-most
891 context (typically @context{ChordNames}, @context{Voice}, or
892 @context{Lyrics}) is used.  In this example,
893
894 @lilypond[quote,verbatim,relative=2,fragment]
895 c8 c c c
896 \set autoBeaming = ##f
897 c8 c c c
898 @end lilypond
899
900 @noindent
901 the @var{context} argument to @code{\set} is left out, so automatic
902 beaming is switched off in the current @internalsref{Voice}.  Note that
903 the bottom-most context does not always contain the property that you
904 wish to change -- for example, attempting to set the @code{skipBars}
905 property (of the bottom-most context, in this case @code{Voice}) will
906 have no effect.
907
908 @lilypond[quote,verbatim,relative=2,fragment]
909 R1*2
910 \set skipBars = ##t
911 R1*2
912 @end lilypond
913
914 Contexts are hierarchical, so if a bigger context was specified, for
915 example @context{Staff}, then the change would also apply to all
916 @context{Voice}s in the current stave.  The change is applied
917 @q{on-the-fly}, during the music, so that the setting only affects the
918 second group of eighth notes.
919
920 @funindex \unset
921
922 There is also an @code{\unset} command,
923 @example
924 \unset @var{context}.@var{prop}
925 @end example
926
927 @noindent
928 which removes the definition of @var{prop}.  This command removes
929 the definition only if it is set in @var{context}, so
930
931 @example
932 \set Staff.autoBeaming = ##f
933 @end example
934
935 @noindent
936 introduces a property setting at @code{Staff} level.  The setting also
937 applies to the current @code{Voice}.  However,
938
939 @example
940 \unset Voice.autoBeaming
941 @end example
942
943 @noindent
944 does not have any effect.  To cancel this setting, the @code{\unset}
945 must be specified on the same level as the original @code{\set}.  In
946 other words, undoing the effect of @code{Staff.autoBeaming = ##f}
947 requires
948 @example
949 \unset Staff.autoBeaming
950 @end example
951
952 Like @code{\set}, the @var{context} argument does not have to be
953 specified for a bottom context, so the two statements
954
955 @example
956 \set Voice.autoBeaming = ##t
957 \set autoBeaming = ##t
958 @end example
959
960 @noindent
961 are equivalent.
962
963
964 @cindex \once
965 Settings that should only apply to a single time-step can be entered
966 with @code{\once}, for example in
967
968 @lilypond[quote,verbatim,relative=2,fragment]
969 c4
970 \once \set fontSize = #4.7
971 c4
972 c4
973 @end lilypond
974
975 the property @code{fontSize} is unset automatically after the second
976 note.
977
978 A full description of all available context properties is in the
979 program reference, see
980 @ifhtml
981 @internalsref{Tunable context properties}.
982 @end ifhtml
983 @ifnothtml
984 Translation @expansion{} Tunable context properties.
985 @end ifnothtml
986
987
988 @node Modifying context plug-ins
989 @subsection Modifying context plug-ins
990
991 Notation contexts (like @code{Score} and @code{Staff}) not only
992 store properties,
993 they also contain plug-ins called @q{engravers} that create notation
994 elements.  For example, the @code{Voice} context contains a
995 @code{Note_head_engraver} and the @code{Staff} context contains a
996 @code{Key_signature_engraver}.
997
998 For a full a description of each plug-in, see
999 @ifhtml
1000 @internalsref{Engravers}.
1001 @end ifhtml
1002 @ifnothtml
1003 Program reference @expansion{} Translation @expansion{} Engravers.
1004 @end ifnothtml
1005 Every context described in
1006 @ifhtml
1007 @internalsref{Contexts}
1008 @end ifhtml
1009 @ifnothtml
1010 Program reference @expansion{} Translation @expansion{} Context.
1011 @end ifnothtml
1012 lists the engravers used for that context.
1013
1014
1015 It can be useful to shuffle around these plug-ins.  This is done by
1016 starting a new context with @code{\new} or @code{\context}, and
1017 modifying it,
1018
1019 @funindex \with
1020
1021 @example
1022 \new @var{context} \with @{
1023   \consists @dots{}
1024   \consists @dots{}
1025   \remove @dots{}
1026   \remove @dots{}
1027   @emph{etc.}
1028 @}
1029 @{
1030   @emph{..music..}
1031 @}
1032 @end example
1033
1034 @noindent
1035 where the @dots{} should be the name of an engraver.  Here is a simple
1036 example which removes @code{Time_signature_engraver} and
1037 @code{Clef_engraver} from a @code{Staff} context,
1038
1039 @lilypond[quote,relative=1,verbatim,fragment]
1040 <<
1041   \new Staff {
1042     f2 g
1043   }
1044   \new Staff \with {
1045      \remove "Time_signature_engraver"
1046      \remove "Clef_engraver"
1047   } {
1048     f2 g2
1049   }
1050 >>
1051 @end lilypond
1052
1053 In the second staff there are no time signature or clef symbols.  This
1054 is a rather crude method of making objects disappear since it will affect
1055 the entire staff.  This method also influences the spacing, which may or
1056 may not be desirable.  A more
1057 sophisticated method of blanking objects is shown in @rlearning{Common tweaks}.
1058
1059 The next example shows a practical application.  Bar lines and time
1060 signatures are normally synchronized across the score.  This is done
1061 by the @code{Timing_translator} and @code{Default_bar_line_engraver}.
1062 This plug-in keeps an administration of time signature, location
1063 within the measure, etc.  By moving thes engraver from @code{Score} to
1064 @code{Staff} context, we can have a score where each staff has its own
1065 time signature.
1066
1067 @cindex polymetric scores
1068 @cindex Time signatures, multiple
1069
1070 @lilypond[quote,relative=1,ragged-right,verbatim,fragment]
1071 \new Score \with {
1072   \remove "Timing_translator"
1073   \remove "Default_bar_line_engraver"
1074 } <<
1075   \new Staff \with {
1076     \consists "Timing_translator"
1077     \consists "Default_bar_line_engraver"
1078   } {
1079       \time 3/4
1080       c4 c c c c c
1081   }
1082   \new Staff \with {
1083     \consists "Timing_translator"
1084     \consists "Default_bar_line_engraver"
1085   } {
1086        \time 2/4
1087        c4 c c c c c
1088   }
1089 >>
1090 @end lilypond
1091
1092
1093 @node Layout tunings within contexts
1094 @subsection Layout tunings within contexts
1095
1096 Each context is responsible for creating certain types of graphical
1097 objects.  The settings used for printing these objects are also stored by
1098 context.  By changing these settings, the appearance of objects can be
1099 altered.
1100
1101 The syntax for this is
1102
1103 @example
1104 \override @var{context}.@var{name} #'@var{property} = #@var{value}
1105 @end example
1106
1107 Here @var{name} is the name of a graphical object, like @code{Stem} or
1108 @code{NoteHead}, and @var{property} is an internal variable of the
1109 formatting system (@q{grob property} or @q{layout property}).  The latter is a
1110 symbol, so it must be quoted.  The subsection @ref{Constructing a
1111 tweak}, explains what to fill in for @var{name}, @var{property}, and
1112 @var{value}.  Here we only discuss the functionality of this command.
1113
1114 The command
1115
1116 @verbatim
1117 \override Staff.Stem #'thickness = #4.0
1118 @end verbatim
1119
1120 @noindent
1121 makes stems thicker (the default is 1.3, with staff line thickness as a
1122 unit).  Since the command specifies @context{Staff} as context, it only
1123 applies to the current staff.  Other staves will keep their normal
1124 appearance.  Here we see the command in action:
1125
1126 @lilypond[quote,verbatim,relative=2,fragment]
1127 c4
1128 \override Staff.Stem #'thickness = #4.0
1129 c4
1130 c4
1131 c4
1132 @end lilypond
1133
1134 The @code{\override} command changes the definition of the @code{Stem}
1135 within the current @context{Staff}.  After the command is interpreted
1136 all stems are thickened.
1137
1138 Analogous to @code{\set}, the @var{context} argument may be left out,
1139 causing the default context @context{Voice} to be used.  Adding
1140 @code{\once} applies the change during one timestep only.
1141
1142 @lilypond[quote,fragment,verbatim,relative=2]
1143 c4
1144 \once \override Stem #'thickness = #4.0
1145 c4
1146 c4
1147 @end lilypond
1148
1149 The @code{\override} must be done before the object is
1150 started.  Therefore, when altering @emph{Spanner} objects such as slurs
1151 or beams, the @code{\override} command must be executed at the moment
1152 when the object is created.  In this example,
1153
1154 @lilypond[quote,fragment,verbatim,relative=2]
1155 \override Slur #'thickness = #3.0
1156 c8[( c
1157 \override Beam #'thickness = #0.6
1158 c8 c])
1159 @end lilypond
1160
1161 @noindent
1162 the slur is fatter but the beam is not.  This is because the command for
1163 @code{Beam} comes after the Beam is started, so it has no effect.
1164
1165 Analogous to @code{\unset}, the @code{\revert} command for a context
1166 undoes an @code{\override} command; like with @code{\unset}, it only
1167 affects settings that were made in the same context.  In other words, the
1168 @code{\revert} in the next example does not do anything.
1169
1170 @example
1171 \override Voice.Stem #'thickness = #4.0
1172 \revert Staff.Stem #'thickness
1173 @end example
1174
1175 Some tweakable options are called @q{subproperties} and reside inside
1176 properties.  To tweak those, use commands of the form
1177
1178 @c leave this as a long long
1179 @example
1180 \override @var{context}.@var{name} #'@var{property} #'@var{subproperty} = #@var{value}
1181 @end example
1182
1183 @noindent
1184 such as
1185
1186 @example
1187 \override Stem #'details #'beamed-lengths = #'(4 4 3)
1188 @end example
1189
1190
1191 @seealso
1192
1193 Internals: @internalsref{OverrideProperty}, @internalsref{RevertProperty},
1194 @internalsref{PropertySet}, @internalsref{Backend}, and
1195 @internalsref{All layout objects}.
1196
1197
1198 @refbugs
1199
1200 The back-end is not very strict in type-checking object properties.
1201 Cyclic references in Scheme values for properties can cause hangs
1202 or crashes, or both.
1203
1204
1205 @node Changing context default settings
1206 @subsection Changing context default settings
1207
1208 The adjustments of the previous subsections (@ref{Changing context
1209 properties on the fly}, @ref{Modifying context plug-ins}, and
1210 @ref{Layout tunings within contexts}) can also be entered separately
1211 from the music in the @code{\layout} block,
1212
1213 @example
1214 \layout @{
1215   @dots{}
1216   \context @{
1217     \Staff
1218
1219     \set fontSize = #-2
1220     \override Stem #'thickness = #4.0
1221     \remove "Time_signature_engraver"
1222   @}
1223 @}
1224 @end example
1225
1226 The @code{\Staff} command brings in the existing definition of the
1227 staff context so that it can be modified.
1228
1229 The statements
1230 @example
1231 \set fontSize = #-2
1232 \override Stem #'thickness = #4.0
1233 \remove "Time_signature_engraver"
1234 @end example
1235
1236 @noindent
1237 affect all staves in the score.  Other contexts can be modified
1238 analogously.
1239
1240 The @code{\set} keyword is optional within the @code{\layout} block, so
1241
1242 @example
1243 \context @{
1244   @dots{}
1245   fontSize = #-2
1246 @}
1247 @end example
1248
1249 @noindent
1250 will also work.
1251
1252
1253
1254 @refbugs
1255
1256 It is not possible to collect context changes in a variable and apply
1257 them to a @code{\context} definition by referring to that variable.
1258
1259 The @code{\RemoveEmptyStaffContext} will overwrite your current
1260 @code{\Staff} settings.  If you wish to change the defaults for a
1261 staff which uses @code{\RemoveEmptyStaffContext}, you must do so
1262 after calling @code{\RemoveEmptyStaffContext}, ie
1263
1264 @example
1265 \layout @{
1266   \context @{
1267     \RemoveEmptyStaffContext
1268
1269     \override Stem #'thickness = #4.0
1270   @}
1271 @}
1272 @end example
1273
1274
1275 @node Defining new contexts
1276 @subsection Defining new contexts
1277
1278 Specific contexts, like @context{Staff} and @code{Voice}, are made of
1279 simple building blocks.  It is possible to create new types of
1280 contexts with different combinations of engraver plug-ins.
1281
1282 The next example shows how to build a different type of
1283 @context{Voice} context from scratch.  It will be similar to
1284 @code{Voice}, but only prints centered slash noteheads.  It can be used
1285 to indicate improvisation in jazz pieces,
1286
1287 @lilypond[quote,ragged-right]
1288 \layout { \context {
1289   \name ImproVoice
1290   \type "Engraver_group"
1291   \consists "Note_heads_engraver"
1292   \consists "Text_engraver"
1293   \consists Pitch_squash_engraver
1294   squashedPosition = #0
1295   \override NoteHead #'style = #'slash
1296   \override Stem #'transparent = ##t
1297   \alias Voice
1298 }
1299 \context { \Staff
1300   \accepts "ImproVoice"
1301 }}
1302
1303 \relative c'' {
1304   a4 d8 bes8 \new ImproVoice { c4^"ad lib" c
1305    c4 c^"undress" c_"while playing :)" c }
1306   a1
1307 }
1308 @end lilypond
1309
1310
1311 These settings are defined within a @code{\context} block inside a
1312 @code{\layout} block,
1313
1314 @example
1315 \layout @{
1316   \context @{
1317     @dots{}
1318   @}
1319 @}
1320 @end example
1321
1322 In the following discussion, the example input shown should go in place
1323 of the @dots{} in the previous fragment.
1324
1325 First it is necessary to define a name for the new context:
1326
1327 @example
1328 \name ImproVoice
1329 @end example
1330
1331 Since it is similar to the @context{Voice}, we want commands that work
1332 on (existing) @context{Voice}s to remain working.  This is achieved by
1333 giving the new context an alias @context{Voice},
1334
1335 @example
1336 \alias Voice
1337 @end example
1338
1339 The context will print notes and instructive texts, so we need to add
1340 the engravers which provide this functionality,
1341
1342 @example
1343 \consists Note_heads_engraver
1344 \consists Text_engraver
1345 @end example
1346
1347 but we only need this on the center line,
1348
1349 @example
1350 \consists Pitch_squash_engraver
1351 squashedPosition = #0
1352 @end example
1353
1354 The @internalsref{Pitch_squash_engraver} modifies note heads (created
1355 by @internalsref{Note_heads_engraver}) and sets their vertical
1356 position to the value of @code{squashedPosition}, in this case@tie{}@code{0},
1357 the center line.
1358
1359 The notes look like a slash, and have no stem,
1360
1361 @example
1362 \override NoteHead #'style = #'slash
1363 \override Stem #'transparent = ##t
1364 @end example
1365
1366 All these plug-ins have to cooperate, and this is achieved with a
1367 special plug-in, which must be marked with the keyword @code{\type}.
1368 This should always be @internalsref{Engraver_group},
1369
1370 @example
1371 \type "Engraver_group"
1372 @end example
1373
1374 Put together, we get
1375
1376 @example
1377 \context @{
1378   \name ImproVoice
1379   \type "Engraver_group"
1380   \consists "Note_heads_engraver"
1381   \consists "Text_engraver"
1382   \consists Pitch_squash_engraver
1383   squashedPosition = #0
1384   \override NoteHead #'style = #'slash
1385   \override Stem #'transparent = ##t
1386   \alias Voice
1387 @}
1388 @end example
1389
1390 @funindex \accepts
1391 Contexts form hierarchies.  We want to hang the @context{ImproVoice}
1392 under @context{Staff}, just like normal @code{Voice}s.  Therefore, we
1393 modify the @code{Staff} definition with the @code{\accepts}
1394 command,
1395
1396 @example
1397 \context @{
1398   \Staff
1399   \accepts ImproVoice
1400 @}
1401 @end example
1402
1403 @funindex \denies
1404 The opposite of @code{\accepts} is @code{\denies},
1405 which is sometimes needed when reusing existing context definitions.
1406
1407 Putting both into a @code{\layout} block, like
1408
1409 @example
1410 \layout @{
1411   \context @{
1412     \name ImproVoice
1413     @dots{}
1414   @}
1415   \context @{
1416     \Staff
1417     \accepts "ImproVoice"
1418   @}
1419 @}
1420 @end example
1421
1422 Then the output at the start of this subsection can be entered as
1423
1424 @example
1425 \relative c'' @{
1426   a4 d8 bes8
1427   \new ImproVoice @{
1428     c4^"ad lib" c
1429     c4 c^"undress"
1430     c c_"while playing :)"
1431   @}
1432   a1
1433 @}
1434 @end example
1435
1436
1437 @node Aligning contexts
1438 @subsection Aligning contexts
1439
1440 New contexts may be aligned above or below exisiting contexts.  This
1441 could be useful in setting up a vocal staff (@rlearning{Vocal ensembles}) and
1442 in ossia,
1443
1444 @cindex ossia
1445 @findex alignAboveContext
1446 @findex alignBelowContext
1447
1448 @lilypond[quote,ragged-right]
1449 ossia = { f4 f f f }
1450 \score{
1451   \relative c' \new Staff = "main" {
1452     c4 c c c
1453     <<
1454       \new Staff \with {alignAboveContext=main} \ossia
1455       { d8 f d f d f d f }
1456     >>
1457   }
1458 }
1459 @end lilypond
1460
1461
1462 @node Vertical grouping of grobs
1463 @subsection Vertical grouping of grobs
1464
1465 The VerticalAlignment and VerticalAxisGroup grobs work together.
1466 VerticalAxisGroup groups together different grobs like Staff, Lyrics,
1467 etc.  VerticalAlignment then vertically aligns the different grobs
1468 grouped together by VerticalAxisGroup.  There is usually only one
1469 VerticalAlignment per score but every Staff, Lyrics, etc. has its own
1470 VerticalAxisGroup. 
1471
1472
1473 @node The \override command
1474 @section The @code{\override} command
1475
1476 In the previous section, we have already touched on a command that
1477 changes layout details: the @code{\override} command.  In this section,
1478 we will look in more detail at how to use the command in practice.  The
1479 general syntax of this command is:
1480
1481 @example
1482 \override @var{context}.@var{layout_object} #'@var{layout_property} = #@var{value}
1483 @end example
1484
1485 This will set the @var{layout_property} of the specified @var{layout_object},
1486 which is a member of the @var{context}, to the @var{value}.
1487
1488 @menu
1489 * Constructing a tweak::        
1490 * Navigating the program reference::  
1491 * Layout interfaces::           
1492 * Determining the grob property::  
1493 * Objects connected to the input::  
1494 * Using Scheme code instead of \tweak::  
1495 * \set versus \override::       
1496 * Difficult tweaks::            
1497 @end menu
1498
1499
1500
1501 @node Constructing a tweak
1502 @subsection Constructing a tweak
1503
1504 Commands which change output generally look like
1505
1506 @example
1507 \override Voice.Stem #'thickness = #3.0
1508 @end example
1509
1510 @noindent
1511 To construct this tweak we must determine these bits of information:
1512
1513 @itemize
1514 @item the context: here @context{Voice}.
1515 @item the layout object: here @code{Stem}.
1516 @item the layout property: here @code{thickness}.
1517 @item a sensible value: here @code{3.0}.
1518 @end itemize
1519
1520 Some tweakable options are called @q{subproperties} and reside inside
1521 properties.  To tweak those, use commands in the form
1522
1523 @example
1524 \override Stem #'details #'beamed-lengths = #'(4 4 3)
1525 @end example
1526
1527 @cindex internal documentation
1528 @cindex finding graphical objects
1529 @cindex graphical object descriptions
1530 @cindex tweaking
1531 @funindex \override
1532 @cindex internal documentation
1533
1534 For many properties, regardless of the data type of the property, setting the
1535 property to false ( @code{##f} ) will result in turning it off, causing
1536 Lilypond to ignore that property entirely.  This is particularly useful for
1537 turning off grob properties which may otherwise be causing problems.
1538
1539 We demonstrate how to glean this information from the notation manual
1540 and the program reference.
1541
1542
1543
1544
1545 @node Navigating the program reference
1546 @subsection Navigating the program reference
1547
1548 Suppose we want to move the fingering indication in the fragment
1549 below:
1550
1551 @lilypond[quote,fragment,relative=2,verbatim]
1552 c-2
1553 \stemUp
1554 f
1555 @end lilypond
1556
1557 If you visit the documentation on fingering instructions (in
1558 @ref{Fingering instructions}), you will notice:
1559
1560 @quotation
1561 @seealso
1562
1563 Program reference: @internalsref{Fingering}.
1564
1565 @end quotation
1566
1567
1568 @c  outdated info; probably will delete.
1569 @ignore
1570 This fragment points to two parts of the program reference: a page
1571 on @code{FingerEvent} and one on @code{Fingering}.
1572
1573 The page on @code{FingerEvent} describes the properties of the music
1574 expression for the input @code{-2}.  The page contains many links
1575 forward.  For example, it says
1576
1577 @quotation
1578 Accepted by: @internalsref{Fingering_engraver},
1579 @end quotation
1580
1581 @noindent
1582 That link brings us to the documentation for the Engraver, the
1583 plug-in, which says
1584
1585 @quotation
1586 This engraver creates the following layout objects: @internalsref{Fingering}.
1587 @end quotation
1588
1589 In other words, once the @code{FingerEvent}s are interpreted, the
1590 @code{Fingering_engraver} plug-in will process them.
1591 @end ignore
1592
1593 @ignore
1594 @c  I can't figure out what this is supposed to mean.  -gp
1595
1596 The @code{Fingering_engraver} is also listed to create
1597 @internalsref{Fingering} objects,
1598
1599 @c  old info?  it doesn't make any sense to me with our current docs.
1600 This is also the
1601 second bit of information listed under @b{See also} in the Notation
1602 manual.
1603 @end ignore
1604
1605 @ifnothtml
1606 The programmer's reference is available as an HTML document.  It is
1607 highly recommended that you read it in HTML form, either online or
1608 by downloading the HTML documentation.  This section will be much more
1609 difficult to understand if you are using the 
1610 PDF manual.
1611 @end ifnothtml
1612
1613 Follow the link to @internalsref{Fingering}.  At the top of the
1614 page, you will see
1615
1616 @quotation
1617 Fingering objects are created by: @internalsref{Fingering_engraver} and
1618 @internalsref{New_fingering_engraver}.
1619 @end quotation
1620
1621 By following related links inside the program reference, we can follow the
1622 flow of information within the program:
1623
1624 @itemize @bullet
1625
1626 @item @internalsref{Fingering}:
1627 @internalsref{Fingering} objects are created by:
1628 @internalsref{Fingering_engraver}
1629
1630 @item @internalsref{Fingering_engraver}:
1631 Music types accepted: @internalsref{fingering-event}
1632
1633 @item @internalsref{fingering-event}:
1634 Music event type @code{fingering-event} is in Music expressions named
1635 @internalsref{FingerEvent}
1636 @end itemize
1637
1638 This path goes against the flow of information in the program: it
1639 starts from the output, and ends at the input event.  You could
1640 also start at an input event, and read with the flow of
1641 information, eventually ending up at the output object(s).
1642
1643 The program reference can also be browsed like a normal document.  It
1644 contains chapters on
1645 @ifhtml
1646 @internalsref{Music definitions},
1647 @end ifhtml
1648 @ifnothtml
1649 @code{Music definitions}
1650 @end ifnothtml
1651 on @internalsref{Translation}, and the @internalsref{Backend}.  Every
1652 chapter lists all the definitions used and all properties that may be
1653 tuned.
1654
1655
1656 @node Layout interfaces
1657 @subsection Layout interfaces
1658
1659 @cindex interface, layout
1660 @cindex layout interface
1661 @cindex grob
1662
1663 The HTML page that we found in the previous section describes the
1664 layout object called @internalsref{Fingering}.  Such an object is a
1665 symbol within the score.  It has properties that store numbers (like
1666 thicknesses and directions), but also pointers to related objects.  A
1667 layout object is also called a @emph{Grob}, which is short for Graphical
1668 Object.  For more details about Grobs, see @internalsref{grob-interface}.
1669
1670 The page for @code{Fingering} lists the definitions for the
1671 @code{Fingering} object.  For example, the page says
1672
1673 @quotation
1674 @code{padding} (dimension, in staff space):
1675
1676 @code{0.5}
1677 @end quotation
1678
1679 @noindent
1680 which means that the number will be kept at a distance of at least 0.5
1681 of the note head.
1682
1683
1684 Each layout object may have several functions as a notational or
1685 typographical element.  For example, the Fingering object
1686 has the following aspects
1687
1688 @itemize @bullet
1689 @item
1690 Its size is independent of the horizontal spacing, unlike slurs or beams.
1691
1692 @item
1693 It is a piece of text.  Granted, it is usually a very short text.
1694
1695 @item
1696 That piece of text is typeset with a font, unlike slurs or beams.
1697
1698 @item
1699 Horizontally, the center of the symbol should be aligned to the
1700 center of the notehead.
1701
1702 @item
1703 Vertically, the symbol is placed next to the note and the staff.
1704
1705 @item
1706 The vertical position is also coordinated with other superscript
1707 and subscript symbols.
1708 @end itemize
1709
1710 Each of these aspects is captured in so-called @emph{interface}s,
1711 which are listed on the @internalsref{Fingering} page at the bottom
1712
1713 @quotation
1714 This object supports the following interfaces:
1715 @internalsref{item-interface},
1716 @internalsref{self-alignment-interface},
1717 @internalsref{side-position-interface}, @internalsref{text-interface},
1718 @internalsref{text-script-interface}, @internalsref{font-interface},
1719 @internalsref{finger-interface}, and @internalsref{grob-interface}.
1720 @end quotation
1721
1722 Clicking any of the links will take you to the page of the respective
1723 object interface.  Each interface has a number of properties.  Some of
1724 them are not user-serviceable (@q{Internal properties}), but others
1725 can be modified.
1726
1727 We have been talking of @emph{the} @code{Fingering} object, but actually it
1728 does not amount to much.  The initialization file (see
1729 @rlearning{Default files})
1730 @file{scm/@/define@/-grobs@/.scm} shows the soul of the @q{object},
1731
1732 @example
1733 (Fingering
1734   . ((padding . 0.5)
1735      (avoid-slur . around)
1736      (slur-padding . 0.2)
1737      (staff-padding . 0.5)
1738      (self-alignment-X . 0)
1739      (self-alignment-Y . 0)
1740      (script-priority . 100)
1741      (stencil . ,ly:text-interface::print)
1742      (direction . ,ly:script-interface::calc-direction)
1743      (font-encoding . fetaNumber)
1744      (font-size . -5)           ; don't overlap when next to heads.
1745      (meta . ((class . Item)
1746      (interfaces . (finger-interface
1747                     font-interface
1748                     text-script-interface
1749                     text-interface
1750                     side-position-interface
1751                     self-alignment-interface
1752                     item-interface))))))
1753 @end example
1754
1755 @noindent
1756 As you can see, the @code{Fingering} object is nothing more than a
1757 bunch of variable settings, and the webpage in the Program Reference
1758 is directly generated from this definition.
1759
1760
1761 @node Determining the grob property
1762 @subsection Determining the grob property
1763
1764 Recall that we wanted to change the position of the @b{2} in
1765
1766 @lilypond[quote,fragment,relative=2,verbatim]
1767 c-2
1768 \stemUp
1769 f
1770 @end lilypond
1771
1772 Since the @b{2} is vertically positioned next to its note, we have to
1773 meddle with the interface associated with this positioning.  This is
1774 done using @code{side-position-interface}.  The page for this interface
1775 says
1776
1777 @quotation
1778 @code{side-position-interface}
1779
1780 Position a victim object (this one) next to other objects (the
1781 support).  The property @code{direction} signifies where to put the
1782 victim object relative to the support (left or right, up or down?)
1783 @end quotation
1784
1785 @cindex padding
1786 @noindent
1787 Below this description, the variable @code{padding} is described as
1788
1789 @quotation
1790 @table @code
1791 @item padding
1792 (dimension, in staff space)
1793
1794 Add this much extra space between objects that are next to each other.
1795 @end table
1796 @end quotation
1797
1798 By increasing the value of @code{padding}, we can move the
1799 fingering away from the notehead.  The following command inserts
1800 3 staff spaces of white
1801 between the note and the fingering:
1802 @example
1803 \once \override Voice.Fingering #'padding = #3
1804 @end example
1805
1806 Inserting this command before the Fingering object is created,
1807 i.e., before @code{c2}, yields the following result:
1808
1809 @lilypond[quote,relative=2,fragment,verbatim]
1810 \once \override Voice.Fingering #'padding = #3
1811 c-2
1812 \stemUp
1813 f
1814 @end lilypond
1815
1816
1817 In this case, the context for this tweak is @context{Voice}.  This
1818 fact can also be deduced from the program reference, for the page for
1819 the @internalsref{Fingering_engraver} plug-in says
1820
1821 @quotation
1822 Fingering_engraver is part of contexts: @dots{} @internalsref{Voice}
1823 @end quotation
1824
1825
1826 @node Objects connected to the input
1827 @subsection Objects connected to the input
1828
1829 @funindex \tweak
1830
1831 In some cases, it is possible to take a short-cut for tuning graphical
1832 objects.  For objects that result directly from a piece of the input,
1833 you can use the @code{\tweak} function, for example
1834
1835 @lilypond[relative=2,fragment,verbatim,ragged-right]
1836 <
1837   c
1838   \tweak #'color #red d
1839   g
1840   \tweak #'duration-log #1  a
1841 >4-\tweak #'padding #10 -.
1842 @end lilypond
1843
1844 As you can see, properties are set in the objects directly,
1845 without mentioning the grob name or context where this should be
1846 applied.
1847
1848 This technique only works for objects that are directly connected to
1849 an @internalsref{Event} from the input, for example
1850
1851 @itemize @bullet
1852 @item note heads, caused by chord-pitch (i.e., notes inside a chord)
1853 @item articulation signs, caused by articulation instructions
1854 @end itemize
1855
1856 It notably does not work for stems and accidentals (these are caused
1857 by note heads, not by music events) or clefs (these are not caused by
1858 music inputs, but rather by the change of a property value).
1859
1860 There are very few objects which are @emph{directly} connected to
1861 output.  A normal note (like @code{c4}) is not directly connected
1862 to output, so
1863
1864 @example
1865 \tweak #'color #red c4
1866 @end example
1867
1868 @noindent
1869 does not change color.  See @ref{Displaying music expressions}, for
1870 details.
1871
1872
1873 @node Using Scheme code instead of \tweak
1874 @subsection Using Scheme code instead of @code{\tweak}
1875
1876 The main disadvantage of @code{\tweak} is its syntactical
1877 inflexibility.  For example, the following produces a syntax error.
1878
1879 @example
1880 F = \tweak #'font-size #-3 -\flageolet
1881
1882 \relative c'' @{
1883   c4^\F c4_\F
1884 @}
1885 @end example
1886
1887 @noindent
1888 With other words, @code{\tweak} doesn't behave like an articulation
1889 regarding the syntax; in particular, it can't be attached with
1890 @samp{^} and @samp{_}.
1891
1892 Using Scheme, this problem can be circumvented.  The route to the
1893 result is given in @ref{Adding articulation to notes (example)},
1894 especially how to use @code{\displayMusic} as a helping guide.
1895
1896 @example
1897 F = #(let ((m (make-music 'ArticulationEvent
1898                           'articulation-type "flageolet")))
1899        (set! (ly:music-property m 'tweaks)
1900              (acons 'font-size -3
1901                     (ly:music-property m 'tweaks)))
1902        m)
1903  
1904 \relative c'' @{
1905   c4^\F c4_\F
1906 @}
1907 @end example
1908
1909 @noindent
1910 Here, the @code{tweaks} properties of the flageolet object
1911 @samp{m} (created with @code{make-music}) are extracted with
1912 @code{ly:music-property}, a new key-value pair to change the
1913 font size is prepended to the property list with the
1914 @code{acons} Scheme function, and the result is finally
1915 written back with @code{set!}.  The last element of the
1916 @code{let} block is the return value, @samp{m} itself.
1917
1918
1919 @node \set versus \override
1920 @subsection @code{\set} vs. @code{\override}
1921
1922 We have seen two methods of changing properties: @code{\set} and
1923 @code{\override}.  There are actually two different kinds of
1924 properties.
1925
1926 Contexts can have properties, which are usually named in
1927 @code{studlyCaps}.  They mostly control the translation from
1928 music to notatino, eg. @code{localKeySignature} (for determining
1929 whether to print accidentals), @code{measurePosition} (for
1930 determining when to print a barline).  Context properties can
1931 change value over time while interpreting a piece of music;
1932 @code{measurePosition} is an obvious example of
1933 this.  Context properties are modified with @code{\set}.
1934
1935 There is a special type of context property: the element
1936 description.  These properties are named in @code{StudlyCaps}
1937 (starting with capital letters).  They contain the
1938 @q{default settings} for said graphical object as an
1939 association list.  See @file{scm/@/define@/-grobs@/.scm}
1940 to see what kind of settings there are.  Element descriptions
1941 may be modified with @code{\override}.
1942
1943 @code{\override} is actually a shorthand;
1944
1945 @example
1946 \override @var{context}.@var{name} #'@var{property} = #@var{value}
1947 @end example
1948
1949 @noindent
1950 is more or less equivalent to
1951
1952 @c  leave this long line -gp
1953 @example
1954 \set @var{context}.@var{name} #'@var{property} = #(cons (cons '@var{property} @var{value}) <previous value of @var{context})
1955 @end example
1956
1957 The value of @code{context} (the alist) is used to initalize
1958 the properties of individual grobs.  Grobs also have
1959 properties, named in Scheme style, with
1960 @code{dashed-words}.  The values of grob properties change
1961 during the formatting process: formatting basically amounts
1962 to computing properties using callback functions.
1963
1964 @code{fontSize} is a special property: it is equivalent to
1965 entering @code{\override ... #'font-size} for all pertinent
1966 objects.  Since this is a common change, the special
1967 property (modified with @code{\set}) was created.
1968
1969
1970 @node Difficult tweaks
1971 @subsection Difficult tweaks
1972
1973 There are a few classes of difficult adjustments.
1974
1975 @itemize @bullet
1976
1977
1978 @item
1979 One type of difficult adjustment is the appearance of spanner objects,
1980 such as slur and tie.  Initially, only one of these objects is created,
1981 and they can be adjusted with the normal mechanism.  However, in some
1982 cases the spanners cross line breaks.  If this happens, these objects
1983 are cloned.  A separate object is created for every system that it is
1984 in.  These are clones of the original object and inherit all
1985 properties, including @code{\override}s.
1986
1987
1988 In other words, an @code{\override} always affects all pieces of a
1989 broken spanner.  To change only one part of a spanner at a line break,
1990 it is necessary to hook into the formatting process.  The
1991 @code{after-line-breaking} callback contains the Scheme procedure that
1992 is called after the line breaks have been determined, and layout
1993 objects have been split over different systems.
1994
1995 In the following example, we define a procedure
1996 @code{my-callback}.  This procedure
1997
1998 @itemize @bullet
1999 @item
2000 determines if we have been split across line breaks
2001 @item
2002 if yes, retrieves all the split objects
2003 @item
2004 checks if we are the last of the split objects
2005 @item
2006 if yes, it sets @code{extra-offset}.
2007 @end itemize
2008
2009 This procedure is installed into @internalsref{Tie}, so the last part
2010 of the broken tie is translated up.
2011
2012 @lilypond[quote,verbatim,ragged-right]
2013 #(define (my-callback grob)
2014   (let* (
2015          ; have we been split?
2016          (orig (ly:grob-original grob))
2017
2018          ; if yes, get the split pieces (our siblings)
2019          (siblings (if (ly:grob? orig)
2020                      (ly:spanner-broken-into orig) '() )))
2021
2022    (if (and (>= (length siblings) 2)
2023              (eq? (car (last-pair siblings)) grob))
2024      (ly:grob-set-property! grob 'extra-offset '(-2 . 5)))))
2025
2026 \relative c'' {
2027   \override Tie #'after-line-breaking =
2028   #my-callback
2029   c1 ~ \break c2 ~ c
2030 }
2031 @end lilypond
2032
2033 @noindent
2034 When applying this trick, the new @code{after-line-breaking} callback
2035 should also call the old one @code{after-line-breaking}, if there is
2036 one.  For example, if using this with @code{Hairpin},
2037 @code{ly:hairpin::after-line-breaking} should also be called.
2038
2039
2040 @item Some objects cannot be changed with @code{\override} for
2041 technical reasons.  Examples of those are @code{NonMusicalPaperColumn}
2042 and @code{PaperColumn}.  They can be changed with the
2043 @code{\overrideProperty} function, which works similar to @code{\once
2044 \override}, but uses a different syntax.
2045
2046 @example
2047 \overrideProperty
2048 #"Score.NonMusicalPaperColumn"  % Grob name
2049 #'line-break-system-details     % Property name
2050 #'((next-padding . 20))         % Value
2051 @end example
2052
2053 Note, however, that @code{\override}, applied to
2054 @code{NoteMusicalPaperColumn} and @code{PaperColumn}, still works as
2055 expected within @code{\context} blocks.
2056
2057 @end itemize