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