]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/user/changing-defaults.itely
Further clarifications on \new.
[lilypond.git] / Documentation / user / changing-defaults.itely
1 @c -*- coding: utf-8; mode: texinfo; -*-
2 @node Changing defaults
3 @chapter Changing defaults
4
5
6 The purpose of LilyPond's design is to provide the finest output
7 quality as a default.  Nevertheless, it may happen that you need to
8 change this default layout.  The layout is controlled through a large
9 number of proverbial ``knobs and switches.''  This chapter does not
10 list each and every knob.  Rather, it outlines what groups of controls
11 are available and explains how to lookup which knob to use for a
12 particular effect.
13
14
15 @cindex Program reference
16
17 The controls available for tuning are described in a separate
18 document, the
19 @iftex
20 Program reference
21 @end iftex
22 @ifnottex
23 @ref{Top,Program reference,,lilypond-internals}.
24 @end ifnottex
25 manual.  That manual
26 lists all different variables, functions and options available in
27 LilyPond.  It is written as a HTML document, which is available
28 @uref{http://@/lilypond@/.org/@/doc/@/v2.7/@/Documentation/@/user/@/lilypond@/-internals/,on@/-line},
29 but is also included with the LilyPond documentation package.
30
31 There are three areas where the default settings may be changed:
32
33 @itemize @bullet
34 @item
35 Output: changing the appearance of individual
36 objects.  For example, changing stem directions or the location of
37 subscripts.
38   
39 @item
40 Context: changing aspects of the translation from music events to
41 notation.  For example, giving each staff a separate time signature. 
42   
43 @item
44 Global layout: changing the appearance of the spacing, line
45 breaks, and page dimensions.
46 @end itemize
47
48 Then there are separate systems for typesetting text (like
49 @emph{ritardando}) and selecting different fonts.  This chapter also
50 discusses these.
51
52 Internally, LilyPond uses Scheme (a LISP dialect) to provide
53 infrastructure.  Overriding layout decisions in effect accesses the
54 program internals, which requires Scheme input.  Scheme elements are
55 introduced in a @code{.ly} file with the hash mark
56 @code{#}.@footnote{@ref{Scheme tutorial} contains a short tutorial
57 on entering numbers, lists, strings, and symbols in Scheme.}
58
59
60 @menu
61 * Interpretation contexts::     
62 * The \override command::       
63 @end menu
64
65  
66 @node Interpretation contexts
67 @section Interpretation contexts
68
69 When music is printed, a lot of notational elements must be added to the
70 input, which is often bare bones.  For example, compare the input and
71 output of the following example:
72
73 @lilypond[quote,verbatim,relative=2,fragment]
74 cis4 cis2. g4
75 @end lilypond
76
77 The input is rather sparse, but in the output, bar lines, accidentals,
78 clef, and time signature are added.  LilyPond @emph{interprets} the
79 input.  During this step, the musical information is inspected in time
80 order, similar to reading a score from left to right.  While reading,
81 the input, the program remembers where measure boundaries are, and what
82 pitches need explicit accidentals.  This information can be presented on
83 several levels.  For example, the effect of an accidental is limited
84 to a single staff, while a bar line must be synchronized across the
85 entire score.
86
87 Within LilyPond, these rules and bits of information are grouped in
88 so-called Contexts.  Examples of context are @context{Voice},
89 @context{Staff}, and @context{Score}.  They are hierarchical, for
90 example, a @context{Staff} can contain many @context{Voice}s, and a
91 @context{Score} can contain many @context{Staff} contexts.
92
93 Each context has the responsibility for enforcing some notation rules,
94 creating some notation objects and maintaining the associated
95 properties.  So, the synchronization of bar lines is handled at
96 @context{Score} context.  The @context{Voice} may introduce an
97 accidental and then the @context{Staff} context maintains the rule to
98 show or suppress the accidental for the remainder of the measure.
99
100 For simple scores, contexts are created implicitly, and you need not
101 be aware of them.  For larger pieces, such as piano music, they must be
102 created explicitly to make sure that you get as many staves as you
103 need, and that they are in the correct order.  For typesetting pieces
104 with specialized notation, it can be useful to modify existing or
105 to define new contexts.
106
107
108 A complete description of all available contexts is in the program
109 reference, see
110 @ifhtml
111 @internalsref{Contexts}.
112 @end ifhtml
113 @ifnothtml 
114 Translation @arrow{} Context.
115 @end ifnothtml
116
117 @c [TODO: describe propagation]
118
119
120 @menu
121 * Creating contexts::           
122 * Changing context properties on the fly::  
123 * Modifying context plug-ins::  
124 * Layout tunings within contexts::  
125 * Changing context default settings::  
126 * Defining new contexts::       
127 @end menu
128
129 @node Creating contexts
130 @subsection Creating contexts
131
132 For scores with only one voice and one staff, correct contexts are
133 created automatically.  For more complex scores, it is necessary to
134 create them by hand.  There are three commands that do this.
135
136 The easiest command is @code{\new}, and it also the quickest to type.
137 It is prepended to a music expression, for example
138
139 @cindex @code{\new}
140 @cindex new contexts
141 @cindex Context, creating
142
143 @example
144 \new @var{type} @var{music expression}
145 @end example
146
147 @noindent
148 where @var{type} is a context name (like @code{Staff} or
149 @code{Voice}).  This command creates a new context, and starts
150 interpreting the @var{music expression} with that.
151
152 A practical application of @code{\new} is a score with many
153 staves.  Each part that should be on its own staff, is preceded with 
154 @code{\new Staff}.
155
156 @lilypond[quote,verbatim,relative=2,ragged-right,fragment]
157 << \new Staff { c4 c }
158    \new Staff { d4 d }
159 >>
160 @end lilypond
161
162 The @code{\new} command may also give a name to the context,
163
164 @example
165 \new @var{type} = @var{id} @var{music}
166 @end example
167 However, this user specified name is only used if there is no other
168 context already earlier with the same name.
169
170 @cindex @code{\context}
171
172 Like @code{\new}, the @code{\context} command also directs a music
173 expression to a context object, but gives the context an explicit name.  The
174 syntax is
175
176 @example
177 \context @var{type} = @var{id} @var{music}
178 @end example
179
180 This form will search for an existing context of type @var{type}
181 called @var{id}.  If that context does not exist yet, it is created.
182 This is useful if the context is referred to later on.  For example, when
183 setting lyrics the melody is in a named context
184
185 @example
186 \context Voice = "@b{tenor}" @var{music}
187 @end example
188
189 @noindent
190 so the texts can be properly aligned to its notes,
191
192 @example
193 \new Lyrics \lyricsto "@b{tenor}" @var{lyrics} 
194 @end example
195
196 @noindent
197
198 Another possibility is funneling two different music expressions into
199 one context.  In the following example, articulations and notes are
200 entered separately,
201
202 @example
203 music = @{ c4 c4 @}
204 arts = @{ s4-. s4-> @}
205 @end example
206
207 They are combined by sending both to the same @context{Voice} context,
208
209 @example
210 << \new Staff \context Voice = "A" \music
211    \context Voice = "A" \arts
212 >>
213 @end example
214 @lilypond[quote,ragged-right]
215 music = { c4 c4 }
216 arts = { s4-. s4-> }
217 \relative c'' <<
218   \new Staff \context Voice = "A" \music
219   \context Voice = "A" \arts
220 >>
221 @end lilypond
222
223 With this mechanism, it is possible to define an Urtext (original
224 edition), with the option to put several distinct articulations on the
225 same notes.
226
227 @cindex creating contexts
228
229 The third command for creating contexts is
230 @example
231 \context @var{type} @var{music}
232 @end example
233
234
235 @noindent
236 This is similar to @code{\context} with @code{= @var{id}}, but matches
237 any context of type @var{type}, regardless of its given name.
238
239 This variant is used with music expressions that can be interpreted at
240 several levels.  For example, the @code{\applyOutput} command (see
241 @ref{Running a function on all layout objects}).  Without an explicit
242 @code{\context}, it is usually applied to @context{Voice}
243
244 @example
245 \applyOutput #@var{function}   % apply to Voice
246 @end example
247
248 To have it interpreted at the @context{Score} or @context{Staff} level use
249 these forms
250
251 @example
252 \context Score \applyOutput #@var{function}
253 \context Staff \applyOutput #@var{function}
254 @end example
255
256
257 @node Changing context properties on the fly
258 @subsection Changing context properties on the fly
259
260 @cindex properties
261 @cindex @code{\set}
262 @cindex changing properties
263
264 Each context can have different @emph{properties}, variables contained
265 in that context.  They can be changed during the interpretation step.
266 This is achieved by inserting the @code{\set} command in the music,
267
268 @example
269 \set @var{context}.@var{prop} = #@var{value}
270 @end example
271
272 For example,
273 @lilypond[quote,verbatim,relative=2,fragment]
274 R1*2 
275 \set Score.skipBars = ##t
276 R1*2
277 @end lilypond
278
279 This command skips measures that have no notes.  The result is that
280 multi-rests are condensed.  The value assigned is a Scheme object.  In
281 this case, it is @code{#t}, the boolean True value.
282
283 If the @var{context} argument is left out, then the current bottom-most
284 context (typically @context{ChordNames}, @context{Voice}, or
285 @context{Lyrics}) is used.  In this example,
286
287 @lilypond[quote,verbatim,relative=2,fragment]
288 c8 c c c
289 \set autoBeaming = ##f
290 c8 c c c
291 @end lilypond
292
293 @noindent
294 the @var{context} argument to @code{\set} is left out, so automatic
295 beaming is switched off in the current @internalsref{Voice}.  Note that
296 the bottom-most context does not always contain the property that you
297 wish to change -- for example, attempting to set the @code{skipBars}
298 property (of the bottom-most context, in this case @code{Voice}) will
299 have no effect.
300
301 @lilypond[quote,verbatim,relative=2,fragment]
302 R1*2 
303 \set skipBars = ##t
304 R1*2
305 @end lilypond
306
307 Contexts are hierarchical, so if a bigger context was specified, for
308 example @context{Staff}, then the change would also apply to all
309 @context{Voice}s in the current stave.  The change is applied
310 `on-the-fly', during the music, so that the setting only affects the
311 second group of eighth notes.
312
313 @cindex @code{\unset} 
314
315 There is also an @code{\unset} command,
316 @example
317 \unset @var{context}.@var{prop}
318 @end example
319
320 @noindent
321 which removes the definition of @var{prop}.  This command removes
322 the definition only if it is set in @var{context}, so
323
324 @example
325 \set Staff.autoBeaming = ##f
326 @end example
327
328 @noindent
329 introduces a property setting at @code{Staff} level.  The setting also
330 applies to the current @code{Voice}.  However,
331
332 @example
333 \unset Voice.autoBeaming
334 @end example
335
336 @noindent
337 does not have any effect.  To cancel this setting, the @code{\unset}
338 must be specified on the same level as the original @code{\set}.  In
339 other words, undoing the effect of @code{Staff.autoBeaming = ##f}
340 requires
341 @example
342 \unset Staff.autoBeaming
343 @end example
344
345 Like @code{\set}, the @var{context} argument does not have to be
346 specified for a bottom context, so the two statements
347
348 @example
349 \set Voice.autoBeaming = ##t 
350 \set autoBeaming = ##t 
351 @end example 
352
353 @noindent
354 are equivalent.
355
356
357 @cindex \once
358 Settings that should only apply to a single time-step can be entered
359 with @code{\once}, for example in
360
361 @lilypond[quote,verbatim,relative=2,fragment]
362 c4
363 \once \set fontSize = #4.7
364 c4
365 c4
366 @end lilypond
367
368 the property @code{fontSize} is unset automatically after the second
369 note.
370
371 A full description of all available context properties is in the
372 program reference, see
373 @ifhtml
374 @internalsref{Tunable context properties}.
375 @end ifhtml
376 @ifnothtml
377 Translation @arrow{} Tunable context properties.
378 @end ifnothtml
379
380
381 @node Modifying context plug-ins
382 @subsection Modifying context plug-ins
383
384 Notation contexts (like Score and Staff) not only store properties,
385 they also contain plug-ins, called ``engravers'' that create notation
386 elements.  For example, the Voice context contains a
387 @code{Note_head_engraver} and the Staff context contains a
388 @code{Key_signature_engraver}.
389
390 For a full a description of each plug-in, see 
391 @ifhtml
392 @internalsref{Engravers}.
393 @end ifhtml
394 @ifnothtml
395 Program reference @arrow Translation @arrow{} Engravers.
396 @end ifnothtml
397 Every context described in
398 @ifhtml
399 @internalsref{Contexts}
400 @end ifhtml
401 @ifnothtml 
402 Program reference @arrow Translation @arrow{} Context.
403 @end ifnothtml
404 lists the engravers used for that context.
405
406
407 It can be useful to shuffle around these plug-ins.  This is done by
408 starting a new context, with @code{\new} or @code{\context}, and
409 modifying it like this, 
410
411 @cindex @code{\with}
412
413 @example
414 \new @var{context} \with @{
415   \consists @dots{}
416   \consists @dots{}
417   \remove @dots{}
418   \remove @dots{}
419   @emph{etc.}
420 @}
421 @emph{..music..}
422 @end example
423
424 @noindent
425 where the @dots{} should be the name of an engraver.  Here is a simple
426 example which removes @code{Time_signature_engraver} and
427 @code{Clef_engraver} from a @code{Staff} context,
428
429 @lilypond[quote,relative=1,verbatim,fragment]
430 << \new Staff {
431     f2 g
432   }
433   \new Staff \with {
434      \remove "Time_signature_engraver"
435      \remove "Clef_engraver"
436   } {
437     f2 g2
438   }
439 >>
440 @end lilypond
441
442 In the second staff there are no time signature or clef symbols.  This
443 is a rather crude method of making objects disappear since it will affect
444 the entire staff.  The spacing is adversely influenced too.  A more
445 sophisticated method of blanking objects is shown in @ref{Common tweaks}.
446
447 The next example shows a practical application.  Bar lines and time
448 signatures are normally synchronized across the score.  This is done
449 by the @code{Timing_translator} and @code{Default_bar_line_engraver}.
450 This plug-in keeps an administration of time signature, location
451 within the measure, etc.  By moving thes engraver from @code{Score} to
452 @code{Staff} context, we can have a score where each staff has its own
453 time signature.
454
455 @cindex polymetric scores
456 @cindex Time signatures, multiple
457
458 @lilypond[quote,relative=1,ragged-right,verbatim,fragment]
459 \new Score \with {
460   \remove "Timing_translator"
461   \remove "Default_bar_line_engraver"
462 } <<
463   \new Staff \with {
464     \consists "Timing_translator"
465     \consists "Default_bar_line_engraver"
466   } {
467       \time 3/4
468       c4 c c c c c
469   }
470   \new Staff \with {
471     \consists "Timing_translator"
472     \consists "Default_bar_line_engraver"
473   } {
474        \time 2/4
475        c4 c c c c c
476   }
477 >>
478 @end lilypond
479
480
481 @node Layout tunings within contexts
482 @subsection Layout tunings within contexts
483
484 Each context is responsible for creating certain types of graphical
485 objects.  The settings used for printing these objects are also stored by
486 context.  By changing these settings, the appearance of objects can be
487 altered.
488  
489 The syntax for this is
490
491 @example
492 \override @var{context}.@var{name} #'@var{property} = #@var{value}
493 @end example
494
495 Here @var{name} is the name of a graphical object, like @code{Stem} or
496 @code{NoteHead}, and @var{property} is an internal variable of the
497 formatting system (`grob property' or `layout property').  The latter is a
498 symbol, so it must be quoted.  The subsection @ref{Constructing a
499 tweak} explains what to fill in for @var{name}, @var{property}, and
500 @var{value}.  Here we only discuss the functionality of this command.
501
502 The command
503
504 @verbatim
505 \override Staff.Stem #'thickness = #4.0 
506 @end verbatim
507
508 @noindent
509 makes stems thicker (the default is 1.3, with staff line thickness as a
510 unit).  Since the command specifies @context{Staff} as context, it only
511 applies to the current staff.  Other staves will keep their normal
512 appearance.  Here we see the command in action:
513
514 @lilypond[quote,verbatim,relative=2,fragment]
515 c4
516 \override Staff.Stem #'thickness = #4.0 
517 c4
518 c4
519 c4
520 @end lilypond
521
522 The @code{\override} command changes the definition of the @code{Stem}
523 within the current @context{Staff}.  After the command is interpreted
524 all stems are thickened.
525
526 Analogous to @code{\set}, the @var{context} argument may be left out,
527 causing it to default to @context{Voice}, and adding @code{\once} applies
528 the change during one timestep only 
529
530 @lilypond[quote,fragment,verbatim,relative=2]
531 c4
532 \once \override Stem #'thickness = #4.0 
533 c4
534 c4 
535 @end lilypond
536
537 The @code{\override} must be done before the object is
538 started.  Therefore, when altering @emph{Spanner} objects, like slurs or
539 beams, the @code{\override} command must be executed at the moment when
540 the object is created.  In this example,
541
542
543 @lilypond[quote,fragment,verbatim,relative=2]
544 \override Slur #'thickness = #3.0
545 c8[( c
546 \override Beam #'thickness = #0.6
547 c8 c]) 
548 @end lilypond
549
550 @noindent
551 the slur is fatter but the beam is not.  This is because the command for
552 @code{Beam} comes after the Beam is started.  Therefore it has no effect.
553
554 Analogous to @code{\unset}, the @code{\revert} command for a context
555 undoes an @code{\override} command; like with @code{\unset}, it only
556 affects settings that were made in the same context.  In other words, the
557 @code{\revert} in the next example does not do anything.
558
559 @example
560 \override Voice.Stem #'thickness = #4.0
561 \revert Staff.Stem #'thickness
562 @end example
563
564 Some tweakable options are called ``subproperties'' and reside inside
565 properties.  To tweak those, use
566
567 @example
568 \override Stem #'details #'beamed-lengths = #'(4 4 3) 
569 @end example
570
571
572 @seealso
573
574 Internals: @internalsref{OverrideProperty}, @internalsref{RevertProperty},
575 @internalsref{PropertySet}, @internalsref{Backend}, and
576 @internalsref{All layout objects}.
577
578
579 @refbugs
580
581 The back-end is not very strict in type-checking object properties.
582 Cyclic references in Scheme values for properties can cause hangs
583 or crashes, or both.
584
585
586 @node Changing context default settings
587 @subsection Changing context default settings
588
589 The adjustments of the previous subsections (@ref{Changing context
590 properties on the fly}, @ref{Modifying context plug-ins}, and
591 @ref{Layout tunings within contexts}) can also be entered separately
592 from the music, in the @code{\layout} block,
593
594 @example
595 \layout @{
596   @dots{}
597   \context @{
598     \Staff
599
600     \set fontSize = #-2
601     \override Stem #'thickness = #4.0
602     \remove "Time_signature_engraver"
603   @}
604 @}
605 @end example
606
607 Here
608 @example
609 \Staff
610 @end example
611
612 @noindent
613 takes the existing definition for context @context{Staff} from the
614 identifier @code{\Staff}. 
615
616 The statements
617 @example
618 \set fontSize = #-2
619 \override Stem #'thickness = #4.0
620 \remove "Time_signature_engraver"
621 @end example
622
623 @noindent
624 affect all staves in the score.
625
626 Other contexts can be modified analogously.
627
628 The @code{\set} keyword is optional within the @code{\layout} block, so
629
630 @example
631 \context @{
632   @dots{}
633   fontSize = #-2
634 @}
635 @end example
636
637 @noindent
638 will also work.
639
640
641
642 @refbugs
643
644 It is not possible to collect context changes in a variable, and apply
645 them to one @code{\context} definition by referring to that variable.
646
647 The @code{\RemoveEmptyStaffContext} will override your current
648 @code{\Staff} variable.  If you wish to change the defaults for a
649 staff that uses @code{\RemoveEmptyStaffContext}, you must do so
650 after calling @code{\RemoveemptyStaffContext}, ie
651
652 @example
653 \layout @{
654   \context @{
655     \RemoveEmptyStaffContext
656
657     \override Stem #'thickness = #4.0
658   @}
659 @}
660 @end example
661
662
663 @node Defining new contexts
664 @subsection Defining new contexts
665
666 Specific contexts, like @context{Staff} and @code{Voice}, are made of
667 simple building blocks, and it is possible to compose engraver
668 plug-ins in different combinations, thereby creating new types of
669 contexts.
670
671 The next example shows how to build a different type of
672 @context{Voice} context from scratch.  It will be similar to
673 @code{Voice}, but prints centered slash noteheads only.  It can be used
674 to indicate improvisation in Jazz pieces,
675
676 @lilypond[quote,ragged-right]
677 \layout { \context {
678   \name ImproVoice
679   \type "Engraver_group"
680   \consists "Note_heads_engraver"
681   \consists "Text_engraver"
682   \consists Pitch_squash_engraver
683   squashedPosition = #0
684   \override NoteHead #'style = #'slash
685   \override Stem #'transparent = ##t
686   \alias Voice
687 }
688 \context { \Staff
689   \accepts "ImproVoice"
690 }}
691
692 \relative c'' {
693   a4 d8 bes8 \new ImproVoice { c4^"ad lib" c 
694    c4 c^"undress" c_"while playing :)" c } 
695   a1 
696 }
697 @end lilypond
698
699
700 These settings are again done within a @code{\context} block inside a
701 @code{\layout} block,
702
703 @example
704 \layout @{
705   \context @{
706     @dots{}
707   @}
708 @}
709 @end example
710
711 In the following discussion, the example input shown should go on the
712 @dots{} in the previous fragment.
713
714 First, the context gets a name.  Instead of @context{Voice} it
715 will be called @context{ImproVoice},
716
717 @example
718 \name ImproVoice
719 @end example
720
721 Since it is similar to the @context{Voice}, we want commands that work
722 on (existing) @context{Voice}s to remain working.  This is achieved by
723 giving the new context an alias @context{Voice},
724
725 @example
726 \alias Voice
727 @end example
728
729 The context will print notes, and instructive texts
730
731 @example
732 \consists Note_heads_engraver
733 \consists Text_engraver
734 @end example
735
736 but only on the center line,
737
738 @example
739 \consists Pitch_squash_engraver
740 squashedPosition = #0
741 @end example
742
743 The @internalsref{Pitch_squash_engraver} modifies note heads (created
744 by @internalsref{Note_heads_engraver}) and sets their vertical
745 position to the value of @code{squashedPosition}, in this case@tie{}@code{0},
746 the center line.
747
748 The notes look like a slash, without a stem,
749
750 @example
751 \override NoteHead #'style = #'slash
752 \override Stem #'transparent = ##t
753 @end example
754
755
756 All these plug-ins have to cooperate, and this is achieved with a
757 special plug-in, which must be marked with the keyword @code{\type}.
758 This should always be @internalsref{Engraver_group},
759
760 @example
761 \type "Engraver_group"
762 @end example
763
764 Put together, we get
765
766 @example
767 \context @{
768   \name ImproVoice
769   \type "Engraver_group"
770   \consists "Note_heads_engraver"
771   \consists "Text_engraver"
772   \consists Pitch_squash_engraver
773   squashedPosition = #0
774   \override NoteHead #'style = #'slash
775   \override Stem #'transparent = ##t
776   \alias Voice
777 @}
778 @end example
779
780 Contexts form hierarchies.  We want to hang the @context{ImproVoice}
781 under @context{Staff}, just like normal @code{Voice}s.  Therefore, we
782 modify the @code{Staff} definition with the @code{\accepts}
783 command,@footnote{The opposite of @code{\accepts} is @code{\denies},
784 which is sometimes needed when reusing existing context definitions.}
785
786
787
788 @example
789 \context @{
790   \Staff
791   \accepts ImproVoice    
792 @}
793 @end example
794
795 Putting both into a @code{\layout} block, like
796
797 @example
798 \layout @{
799   \context @{
800     \name ImproVoice
801     @dots{}
802   @}
803   \context @{
804     \Staff
805     \accepts "ImproVoice"
806   @}
807 @}
808 @end example
809
810 Then the output at the start of this subsection can be entered as
811
812 @example
813 \relative c'' @{
814   a4 d8 bes8
815   \new ImproVoice @{
816     c4^"ad lib" c 
817     c4 c^"undress"
818     c c_"while playing :)"
819   @}
820   a1
821 @}
822 @end example
823   
824
825     
826
827 @node The \override command
828 @section The \override command
829
830 In the previous section, we have already touched on a command that
831 changes layout details: the @code{\override} command.  In this section,
832 we will look in more detail at how to use the command in practice.
833 First, we will give a few versatile commands that are sufficient
834 for many situations.  The next section will discuss the general use of
835 @code{\override}.
836
837
838 @menu
839 * Constructing a tweak::        
840 * Navigating the program reference::  
841 * Layout interfaces::           
842 * Determining the grob property::  
843 * Objects connected to the input::  
844 * Difficult tweaks::            
845 @end menu
846
847
848
849 @node Constructing a tweak
850 @subsection Constructing a tweak
851
852 The general procedure of changing output, that is, entering
853 a command like
854
855 @example
856 \override Voice.Stem #'thickness = #3.0
857 @end example
858
859 @noindent
860 means that we have to determine these bits of information:
861
862 @itemize
863 @item the context: here @context{Voice}.
864 @item the layout object: here @code{Stem}.
865 @item the layout property: here @code{thickness}
866 @item a sensible value: here @code{3.0}
867 @end itemize  
868
869
870 @cindex internal documentation
871 @cindex finding graphical objects
872 @cindex graphical object descriptions 
873 @cindex tweaking
874 @cindex @code{\override}
875 @cindex internal documentation
876
877 We demonstrate how to glean this information from the notation manual
878 and the program reference.
879
880 @node Navigating the program reference
881 @subsection Navigating the program reference
882
883 Suppose we want to move the fingering indication in the fragment
884 below:
885
886 @lilypond[quote,fragment,relative=2,verbatim]
887 c-2
888 \stemUp
889 f
890 @end lilypond
891
892 If you visit the documentation on fingering instructions (in
893 @ref{Fingering instructions}), you will notice that there is written:
894
895 @quotation
896 @seealso
897
898 Program reference: @internalsref{FingerEvent} and @internalsref{Fingering}.
899
900 @end quotation
901
902
903
904 This fragment points to two parts of the program reference: a page
905 on @code{FingerEvent} and one on @code{Fingering}.
906
907 The page on @code{FingerEvent} describes the properties of the music
908 expression for the input @code{-2}.  The page contains many links
909 forward.  For example, it says
910
911 @quotation
912 Accepted by: @internalsref{Fingering_engraver},
913 @end quotation 
914
915 @noindent
916 That link brings us to the documentation for the Engraver, the
917 plug-in, which says
918
919 @quotation
920 This engraver creates the following layout objects: @internalsref{Fingering}.
921 @end quotation
922
923 In other words, once the @code{FingerEvent}s are interpreted, the
924 @code{Fingering_engraver} plug-in will process them.
925
926 @ignore
927 @c  I can't figure out what this is supposed to mean.  -gp
928
929 The @code{Fingering_engraver} is also listed to create
930 @internalsref{Fingering} objects,
931
932 @c  old info?  it doesn't make any sense to me with our current docs.
933 This is also the
934 second bit of information listed under @b{See also} in the Notation
935 manual.
936 @end ignore
937
938 By clicking around in the program reference, we can follow the
939 flow of information within the program, either forward (like we did
940 here), or backwards, following links like this:
941
942 @itemize @bullet
943
944 @item @internalsref{Fingering}:
945 @internalsref{Fingering} objects are created by:
946 @b{@internalsref{Fingering_engraver}}
947
948 @item @internalsref{Fingering_engraver}:
949 Music types accepted: @b{@internalsref{fingering-event}}
950
951 @item @internalsref{fingering-event}:
952 Music event type @code{fingering-event} is in Music expressions named
953 @b{@internalsref{FingerEvent}}
954 @end itemize
955
956 This path goes against the flow of information in the program: it
957 starts from the output, and ends at the input event.
958
959 The program reference can also be browsed like a normal document.  It
960 contains a chapter on
961 @ifhtml
962 @internalsref{Music definitions},
963 @end ifhtml
964 @ifnothtml
965 @code{Music definitions}
966 @end ifnothtml
967 on @internalsref{Translation}, and the @internalsref{Backend}.  Every
968 chapter lists all the definitions used, and all properties that may be
969 tuned.
970
971  
972 @node Layout interfaces
973 @subsection Layout interfaces
974
975 @cindex interface, layout
976 @cindex layout interface
977 @cindex grob
978
979 The HTML page that we found in the previous section, describes the
980 layout object called @internalsref{Fingering}.  Such an object is a
981 symbol within the score.  It has properties that store numbers (like
982 thicknesses and directions), but also pointers to related objects.  A
983 layout object is also called a @emph{Grob}, which is short for Graphical
984 Object.  For more details about Grobs, see @internalsref{grob-interface}.
985
986 The page for @code{Fingering} lists the definitions for the
987 @code{Fingering} object.  For example, the page says
988
989 @quotation
990 @code{padding} (dimension, in staff space):
991   
992 @code{0.6}
993 @end quotation
994
995 @noindent
996 which means that the number will be kept at a distance of at least 0.6
997 of the note head.
998
999
1000 Each layout object may have several functions as a notational or
1001 typographical element.  For example, the Fingering object
1002 has the following aspects
1003
1004 @itemize @bullet
1005 @item
1006 Its size is independent of the horizontal spacing, unlike slurs or beams.
1007
1008 @item
1009 It is a piece of text.  Granted, it is usually a very short text.
1010
1011 @item
1012 That piece of text is typeset with a font, unlike slurs or beams.
1013
1014 @item
1015 Horizontally, the center of the symbol should be aligned to the
1016 center of the notehead.
1017
1018 @item
1019 Vertically, the symbol is placed next to the note and the staff.
1020
1021 @item
1022 The vertical position is also coordinated with other super- and subscript
1023 symbols.
1024 @end itemize
1025
1026 Each of these aspects is captured in so-called @emph{interface}s,
1027 which are listed on the @internalsref{Fingering} page at the bottom
1028
1029 @quotation
1030 This object supports the following interfaces:
1031 @internalsref{item-interface},
1032 @internalsref{self-alignment-interface},
1033 @internalsref{side-position-interface}, @internalsref{text-interface},
1034 @internalsref{text-script-interface}, @internalsref{font-interface},
1035 @internalsref{finger-interface}, and @internalsref{grob-interface}.
1036 @end quotation
1037
1038 Clicking any of the links will take you to the page of the respective
1039 object interface.  Each interface has a number of properties.  Some of
1040 them are not user-serviceable (``Internal properties''), but others
1041 are.
1042
1043 We have been talking of @emph{the} @code{Fingering} object, but actually it
1044 does not amount to much.  The initialization file
1045 @file{scm/@/define@/-grobs@/.scm} shows the soul of the `object',
1046
1047 @example
1048 (Fingering
1049   . ((padding . 0.5)
1050      (avoid-slur . around)
1051      (slur-padding . 0.2)
1052      (staff-padding . 0.5)
1053      (self-alignment-X . 0)
1054      (self-alignment-Y . 0)
1055      (script-priority . 100)
1056      (stencil . ,ly:text-interface::print)
1057      (direction . ,ly:script-interface::calc-direction)
1058      (font-encoding . fetaNumber)
1059      (font-size . -5)           ; don't overlap when next to heads.
1060      (meta . ((class . Item)
1061      (interfaces . (finger-interface
1062                     font-interface
1063                     text-script-interface
1064                     text-interface
1065                     side-position-interface
1066                     self-alignment-interface
1067                     item-interface))))))
1068 @end example
1069
1070 @noindent
1071 As you can see, the @code{Fingering} object is nothing more than a
1072 bunch of variable settings, and the webpage in the Program Reference
1073 is directly generated from this definition.
1074
1075 @node Determining the grob property
1076 @subsection Determining the grob property
1077
1078
1079 Recall that we wanted to change the position of the @b{2} in 
1080
1081 @lilypond[quote,fragment,relative=2,verbatim]
1082 c-2
1083 \stemUp
1084 f
1085 @end lilypond
1086
1087 Since the @b{2} is vertically positioned next to its note, we have to
1088 meddle with the interface associated with this positioning.  This is
1089 done using @code{side-position-interface}.  The page for this interface 
1090 says
1091
1092 @quotation
1093 @code{side-position-interface}
1094
1095 Position a victim object (this one) next to other objects (the
1096 support).  The property @code{direction} signifies where to put the
1097 victim object relative to the support (left or right, up or down?)
1098 @end quotation
1099
1100 @cindex padding
1101 @noindent
1102 below this description, the variable @code{padding} is described as
1103
1104 @quotation
1105 @table @code
1106 @item padding
1107 (dimension, in staff space)
1108
1109 Add this much extra space between objects that are next to each other. 
1110 @end table
1111 @end quotation
1112
1113 By increasing the value of @code{padding}, we can move away the
1114 fingering.  The following command inserts 3 staff spaces of white
1115 between the note and the fingering:
1116 @example
1117 \once \override Voice.Fingering #'padding = #3
1118 @end example
1119
1120 Inserting this command before the Fingering object is created,
1121 i.e., before @code{c2}, yields the following result:
1122
1123 @lilypond[quote,relative=2,fragment,verbatim]
1124 \once \override Voice.Fingering #'padding = #3
1125 c-2
1126 \stemUp
1127 f
1128 @end lilypond
1129
1130
1131 In this case, the context for this tweak is @context{Voice}.  This
1132 fact can also be deduced from the program reference, for the page for
1133 the @internalsref{Fingering_engraver} plug-in says
1134
1135 @quotation
1136 Fingering_engraver is part of contexts: @dots{} @b{@internalsref{Voice}}
1137 @end quotation
1138
1139 @node Objects connected to the input
1140 @subsection Objects connected to the input
1141
1142 In some cases, it is possible to take a short-cut for tuning graphical
1143 objects. For objects that result directly from a piece of the input,
1144 you can use the @code{\tweak} function, for example
1145
1146 @lilypond[relative=2,fragment,verbatim,ragged-right]
1147 <
1148   c
1149   \tweak #'color #red d
1150   g
1151   \tweak #'duration-log #1  a
1152 >4-\tweak #'padding #10 -. 
1153 @end lilypond
1154
1155 As you can see, properties are set directly in the objects directly,
1156 without mentioning the grob name or context where this should be
1157 applied.
1158
1159 This technique only works for objects that are directly connected to
1160 an @internalsref{event} from the input, for example
1161
1162 @itemize @bullet
1163 @item note heads, caused by chord-pitch.
1164 @item articulation signs, caused by articulation instructions
1165 @end itemize
1166
1167 It notably does not work for stems and accidentals (these are caused
1168 by note heads, not by music events) or clefs (these are not caused by
1169 music inputs, but rather by the change of a property value).
1170  
1171
1172 @node Difficult tweaks
1173 @subsection Difficult tweaks
1174
1175 There are a few classes of difficult adjustments.  
1176
1177 @itemize @bullet
1178
1179
1180 @item
1181 One type of difficult adjustment is the appearance of spanner objects,
1182 such as slur and tie.  Initially, only one of these objects is created,
1183 and they can be adjusted with the normal mechanism.  However, in some
1184 cases the spanners cross line breaks.  If this happens, these objects
1185 are cloned.  A separate object is created for every system that it is
1186 in.  These are clones of the original object and inherit all
1187 properties, including @code{\override}s.
1188
1189
1190 In other words, an @code{\override} always affects all pieces of a
1191 broken spanner.  To change only one part of a spanner at a line break,
1192 it is necessary to hook into the formatting process.  The
1193 @code{after-line-breaking} callback contains the Scheme procedure that
1194 is called after the line breaks have been determined, and layout
1195 objects have been split over different systems.
1196
1197 In the following example, we define a procedure
1198 @code{my-callback}.  This procedure
1199  
1200 @itemize @bullet
1201 @item
1202 determines if we have been split across line breaks
1203 @item
1204 if yes, retrieves all the split objects
1205 @item
1206 checks if we are the last of the split objects
1207 @item
1208 if yes, it sets @code{extra-offset}.
1209 @end itemize
1210
1211 This procedure is installed into @internalsref{Tie}, so the last part
1212 of the broken tie is translated up.
1213
1214 @lilypond[quote,verbatim,ragged-right]
1215 #(define (my-callback grob)
1216   (let* (
1217          ; have we been split? 
1218          (orig (ly:grob-original grob))
1219
1220          ; if yes, get the split pieces (our siblings)
1221          (siblings (if (ly:grob? orig)
1222                      (ly:spanner-broken-into orig) '() )))
1223
1224    (if (and (>= (length siblings) 2)
1225              (eq? (car (last-pair siblings)) grob))
1226      (ly:grob-set-property! grob 'extra-offset '(-2 . 5)))))
1227
1228 \relative c'' { 
1229   \override Tie #'after-line-breaking =
1230   #my-callback
1231   c1 ~ \break c2 ~ c
1232 }
1233 @end lilypond
1234
1235 @noindent
1236 When applying this trick, the new @code{after-line-breaking} callback
1237 should also call the old one @code{after-line-breaking}, if there is
1238 one.  For example, if using this with @code{Hairpin},
1239 @code{ly:hairpin::after-line-breaking} should also be called.
1240
1241
1242 @item Some objects cannot be changed with @code{\override} for
1243 technical reasons. Examples of those are @code{NonMusicalPaperColumn}
1244 and @code{PaperColumn}.  They can be changed with the
1245 @code{\outputProperty} function, which works similar to @code{\once
1246 \override}, but uses a different syntax,
1247
1248 @example 
1249 \outputProperty
1250 #"Score.NonMusicalPaperColumn"  % Grob name
1251 #'line-break-system-details     % Property name  
1252 #'((next-padding . 20))         % Value
1253 @end example
1254
1255 @end itemize