]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/user/tweaks.itely
Merge branch 'master' of ssh+git://git.sv.gnu.org/srv/git/lilypond
[lilypond.git] / Documentation / user / tweaks.itely
1 @c -*- coding: utf-8; mode: texinfo; -*-
2 @c This file is part of lilypond-learning.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 Tweaking output
11 @chapter Tweaking output
12
13 This chapter discusses how to modify output.  LilyPond is extremely
14 configurable; virtually every fragment of output may be changed.
15
16
17 @menu
18 * Tweaking basics::             
19 * The Internals Reference manual::  
20 * Appearance of objects::       
21 * Placement of objects::        
22 * Collisions of objects::       
23 * Further tweaking::            
24 @end menu
25
26 @node Tweaking basics
27 @section Tweaking basics
28
29 @menu
30 * Introduction to tweaks::      
31 * Objects and interfaces::      
32 * Naming conventions of objects and properties::  
33 * Tweaking methods::            
34 @end menu
35
36 @node Introduction to tweaks
37 @subsection Introduction to tweaks
38
39 @q{Tweaking} is a LilyPond term for the various methods available
40 to the user for modifying the actions taken during interpretation
41 of the input file and modifying the appearance of the printed 
42 output.  Some tweaks are very easy to use; others are more 
43 complex.  But taken together the methods available for tweaking 
44 permit almost any desired appearance of the printed music to be 
45 achieved.
46
47 In this section we cover the basic concepts required to understand
48 tweaking.  Later we give a variety of ready-made commands which can
49 simply be copied to obtain the same effect in your own scores, and
50 at the same time we show how these commands may be constructed so 
51 that you may learn how to develop your own tweaks.
52
53 Before starting on this Chapter you may wish to review the section
54 @ref{Contexts and engravers}, as Contexts, Engravers, and the
55 Properties contained within them are fundamental to understanding
56 and constructing Tweaks.
57
58 @node Objects and interfaces
59 @subsection Objects and interfaces
60
61 @cindex objects
62 @cindex grobs
63 @cindex spanners
64 @cindex interfaces
65
66 Tweaking involves modifying the internal operation and structures
67 of the LilyPond program, so we must first introduce some terms
68 which are used to describe those internal operations and 
69 structures.
70
71 The term @q{Object} is a generic term used to refer to the
72 multitude of internal structures built by LilyPond during the
73 processing of an input file.  So when a command like @code{\new
74 Staff} is encountered a new object of type @code{Staff} is
75 constructed.  That @code{Staff} object then holds all the
76 properties associated with that particular staff, for example, its
77 name and its key signature, together with details of the engravers
78 which have been assigned to operate within that staff's context.
79 Similarly, there are objects to hold the properties of all other
80 contexts, such as @code{Voice} objects, @code{Score} objects,
81 @code{Lyrics} objects, as well as objects to represent all
82 notational elements such as bar lines,
83 note heads, ties, dynamics, etc.  Every object has its own set of
84 property values.
85
86 Some types of object are given special names.  Objects which
87 represent items of notation on the printed output such as
88  note heads, stems, slurs, ties, fingering, clefs, etc are called
89 @q{Layout objects}, often known as @q{Graphical Objects}, or
90 @q{Grobs} for short.  These are still objects in the generic sense
91 above, and so they too all have properties associated with them,
92 such as their position, size, color, etc.
93
94 Some layout objects are still more specialized.  Phrasing slurs,
95 crescendo hairpins, ottava marks, and many other grobs are not
96 localized in a single place -- they have a starting point, an
97 ending point, and maybe other properties concerned with their
98 shape.  Objects with an extended shape like these are called
99 @q{Spanners}.
100
101 It remains to explain what @q{Interfaces} are.  Many objects,
102 even though they are quite different, share common features 
103 which need to be processed in the same way.
104 For example, all grobs have a color, a size, a position, etc,
105 and all these properties are processed in the same way during
106 LilyPond's
107 interpretation of the input file.  To simplify these internal
108 operations these common actions and properties are grouped 
109 together in an object called a @code{grob-interface}.  There
110 are many other groupings of common properties like this, each 
111 one given a name ending in @code{interface}.  In total there
112 are over 100 such interfaces.  We shall see later why this is
113 of interest and use to the user.
114
115 These, then, are the main terms relating to objects which we 
116 shall use in this chapter.
117
118 @node Naming conventions of objects and properties
119 @subsection Naming conventions of objects and properties
120
121 We met some object naming conventions previously, in 
122 @ref{Contexts and engravers}.  Here for reference is a list
123 of the most common object and property types together with 
124 the conventions for naming them and a couple of examples of 
125 some real names.  We have used A to stand for any capitalized
126 alphabetic character and aaa to stand for any number of
127 lower-case alphabetic characters.  Other characters are used
128 verbatim. 
129
130 @multitable @columnfractions .33 .33 .33
131 @headitem Object/property type           
132   @tab Naming convention
133   @tab Example
134 @item Contexts
135   @tab Aaaa or AaaaAaaaAaaa
136   @tab Staff, GrandStaff
137 @item Layout Objects
138   @tab Aaaa or AaaaAaaaAaaa
139   @tab Slur, NoteHead
140 @item Engravers
141   @tab Aaaa_aaa_engraver
142   @tab Clef_engraver, Note_heads_engraver
143 @item Interfaces
144   @tab aaa-aaa-interface
145   @tab grob-interface, break-aligned-interface 
146 @item Context Properties
147   @tab aaa or aaaAaaaAaaa
148   @tab alignAboveContext, skipBars
149 @item Layout Object Properties
150   @tab aaa or aaa-aaa-aaa
151   @tab direction, beam-thickness
152 @end multitable
153
154 As we shall see shortly, the properties of different types of 
155 object are modified by different commands, so it is useful to
156 be able to recognize the type of object from the names of its
157 properties.
158
159
160 @node Tweaking methods
161 @subsection Tweaking methods
162
163 @strong{\override command}
164
165 @cindex override command
166 @funindex \override
167
168 We have already met the commands @code{\set} and @code{\with},
169 used to change the properties of @strong{contexts} and to remove
170 and add @strong{engravers}, in
171 @ref{Modifying context properties} and @ref{Adding
172 and removing engravers}.  We now must meet some more important
173 commands.
174
175 The command to change the properties of @strong{layout objects} is
176 @code{\override}.  Because this command has to modify
177 internal properties deep within LilyPond its syntax is not
178 as simple as the commands you have met so far.  It needs to
179 know precisely which property of which object in which context
180 has to be modified, and what its new value is to be.  Let's see
181 how this is done.
182
183 The general syntax of this command is:
184
185 @example
186 \override @emph{context}.@emph{layout_object} 
187   #'@emph{layout_property} = #@emph{value}
188 @end example
189
190 @noindent
191 This will set the property with the name @emph{layout_property}
192 of the layout object with the name
193 @emph{layout_object}, which is a member of the @emph{context}
194 context, to the value @emph{value}.
195
196 The @emph{context} can be omitted (and usually is) when the
197 required context is unambiguously implied and is one of lowest
198 level contexts, i.e., @code{Voice}, @code{ChordNames} or
199 @code{Lyrics}, and we shall omit it in many of the following
200 examples.  We shall see later when it must be specified.
201
202 Later sections deal comprehensively with properties and their
203 values, but to illustrate the format and use of these commands
204 we shall use just a few simple properties and values which are
205 easily understood.
206
207 For now, don't worry about the @code{#'}, which must precede the
208 layout property, and the @code{#}, which must precede the value.
209 These must always be present in exactly this form.  This is the
210 most common command used in tweaking, and most of the rest of
211 this chapter will be directed to presenting examples of how it is
212 used.  Here is a simple example to change the color of the
213 note head:
214
215 @lilypond[quote,fragment,ragged-right,verbatim,relative=1]
216 c d
217 \override NoteHead #'color = #red
218 e f g
219 \override NoteHead #'color = #green
220 a b c
221 @end lilypond
222
223 @strong{\revert command}
224
225 @cindex revert command
226 @funindex \revert
227
228 Once overridden, the property retains its new value until it is
229 overridden again or a @code{\revert} command is encountered.
230 The @code{\revert} command has the following syntax and causes
231 the value of the property to revert to its original default
232 value; note, not its previous value if several @code{\override}
233 commands have been issued.
234
235 @example
236 \revert @emph{context}.@emph{layout_object} #'@emph{layout_property}
237 @end example
238
239 Again, just like @emph{context} in the @code{\override} command,
240 @emph{context} is often not needed.  It will be omitted
241 in many of the following examples.  Here we revert the color
242 of the note head to the default value for the final two notes:
243
244 @lilypond[quote,fragment,ragged-right,verbatim,relative=1]
245 c d
246 \override NoteHead #'color = #red
247 e f g
248 \override NoteHead #'color = #green
249 a
250 \revert NoteHead #'color
251 b c
252 @end lilypond
253
254 @strong{\once prefix}
255
256 Both the @code{\override} and the @code{\set} commands may be
257 prefixed by @code{\once}.  This causes the following
258 @code{\override} or @code{\set} command to be effective only
259 during the current musical moment before the property reverts
260 back to its default value.  Using the same example, we can
261 change the color of a single note like this:
262
263 @lilypond[quote,fragment,ragged-right,verbatim,relative=1]
264 c d
265 \once \override NoteHead #'color = #red
266 e f g
267 \once \override NoteHead #'color = #green
268 a b c
269 @end lilypond
270
271 @strong{\overrideProperty command}
272
273 @cindex overrideProperty command
274 @funindex \overrideProperty
275
276 There is another form of the override command,
277 @code{\overrideProperty}, which is occasionally required.
278 We mention it here for completeness, but for details see
279 @ruser{Difficult tweaks}.
280 @c Maybe explain in a later iteration  -td
281
282 @strong{\tweak command}
283
284 @cindex tweak command
285 @funindex \tweak
286
287 The final tweaking command which is available is @code{\tweak}.
288 This should be used to change the properties of objects which
289 occur at the same musical moment, such as the notes within a
290 chord.  Using @code{\override} would affect all the notes
291 within a chord, whereas @code{\tweak} affects just the following
292 item in the input stream.
293
294 Here's an example.  Suppose we wish to change the size of the
295 middle note (the E) in a C major chord.  Let's first see what
296 @code{\once \override} would do:
297
298 @lilypond[quote,fragment,ragged-right,verbatim,relative=1]
299   <c e g>4
300   \once \override NoteHead #'font-size = #-3
301   <c e g>
302   <c e g>
303 @end lilypond
304
305 We see the override affects @emph{all} the notes in the chord.
306 This is because all the notes of a chord occur at the same
307 @emph{musical moment}, and the action of @code{\once} is to
308 apply the override to all layout objects of the type specified
309 which occur at the same musical moment as the @code{\override}
310 command itself.
311
312 The @code{\tweak} command operates in a different way.  It acts
313 on the immediately following item in the input stream.  However,
314 it is effective only on objects which are created directly from
315 the input stream, essentially note heads and articulations.
316 (Objects such as stems and accidentals are created later and
317 cannot be tweaked in this way).  Furthermore, when it is applied
318 to note heads these @emph{must} be within a chord, i.e., within
319 single angle brackets, so to tweak a single note the @code{\tweak}
320 command must be placed inside single angle brackets with the
321 note.
322
323 So to return to our example, the size of the middle note of
324 a chord would be changed in this way:
325
326 @lilypond[quote,fragment,ragged-right,verbatim,relative=1]
327   <c e g>4
328   <c \tweak #'font-size #-3 e g>4
329 @end lilypond
330
331 Note that the syntax of @code{\tweak} is different from that
332 of the @code{\override} command.  Neither the context nor the
333 layout object should be specified; in fact, it would generate
334 an error to do so.  These are both implied by the following
335 item in the input stream.  So the general syntax of the
336 @code{\tweak} command is simply:
337
338 @example
339 \tweak #'@emph{layout_property} = #@emph{value}
340 @end example
341
342 A @code{\tweak} command can also be used to modify just one in
343 a series of articulations, as shown here:
344
345 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
346 a ^Black
347   -\tweak #'color #red ^Red
348   -\tweak #'color #green _Green
349 @end lilypond
350
351 Note that the @code{\tweak} command must be preceded by an
352 articulation mark as if it were an articulation itself.
353
354 @cindex tuplets, nested
355 @cindex triplets, nested
356 @cindex bracket, tuplet
357 @cindex tuplet bracket
358 @cindex triplet bracket
359 @funindex TupletBracket
360
361 The @code{\tweak} command must also be used to change the 
362 appearance of one of a set of nested tuplets which begin at the 
363 same musical moment.  In the following example, the long tuplet
364 bracket and the first of the three short brackets begin at the
365 same musical moment, so any @code{\override} command would apply
366 to both of them.  In the example, @code{\tweak} is used to
367 distinguish between them.  The first @code{\tweak} command
368 specifies that the long tuplet bracket is to be placed above the
369 notes and the second one specifies that the tuplet number is to be
370 printed in red on the first short tuplet bracket.
371
372 @lilypond[quote,ragged-right,verbatim,fragment,relative=2]
373 \tweak #'direction #up
374 \times 4/3 {
375   \tweak #'color #red
376   \times 2/3 { c8[ c8 c8] }
377   \times 2/3 { c8[ c8 c8] }
378   \times 2/3 { c8[ c8 c8] }
379 }
380 @end lilypond
381
382 You can find more details of the @code{\tweak} command in
383 @ruser{Objects connected to the input}.
384
385 If nested tuplets do not begin at the same moment their
386 appearance may be modified in the usual way with
387 @code{\override} commands:
388
389 @c NOTE Tuplet brackets collide if notes are high on staff
390 @c See issue 509
391 @lilypond[quote,ragged-right,verbatim,fragment,relative=1]
392 \times 2/3 { c8[ c c]}
393 \once \override TupletNumber 
394   #'text = #tuplet-number::calc-fraction-text
395 \times 2/3 {
396   c[ c]
397   c[ c]
398   \once \override TupletNumber #'transparent = ##t
399   \times 2/3 { c8[ c c] }
400 \times 2/3 { c8[ c c]}
401 }
402 @end lilypond
403
404
405 @node The Internals Reference manual
406 @section The Internals Reference manual
407
408 @cindex Internals Reference
409
410 @menu
411 * Properties of layout objects::
412 * Properties found in interfaces::
413 * Types of properties::
414 @end menu
415
416 @node Properties of layout objects
417 @subsection Properties of layout objects
418
419 @cindex properties of layout objects
420 @cindex properties of grobs
421 @cindex grobs, properties of
422 @cindex layout objects, properties of
423
424 Suppose you have a slur in a score which, to your mind,
425 appears too thin and you'd like to draw it a little heavier.
426 How do you go about doing this?  You know from the statements
427 earlier about the flexibility of LilyPond that such a thing
428 should be possible, and you would probably guess that an
429 @code{\override} command would be needed.  But is there a
430 heaviness property for a slur, and if there is, how might it 
431 be modified?  This is where the Internals Reference manual
432 comes in.  It contains all the information you might need to
433 construct this and all other @code{\override} commands.
434
435 Before we look at the Internals Reference a word of warning.
436 This is a @strong{reference} document, which means there is
437 little or no explanation contained within it: its purpose is
438 to present information precisely and concisely.  This
439 means it might look daunting at first sight.  Don't worry!
440 The guidance and explanation presented here will enable you
441 to extract the information from the Internals Reference for
442 yourself with just a little practice.
443
444 @cindex override example
445 @cindex Internals Reference, example of using
446
447 Let's use a concrete example with a simple fragment of real
448 music:
449
450 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
451 {
452   \time 6/8
453   {
454     r4 b8 b[( g]) g | 
455     g[( e]) e d[( f]) a | 
456     a g
457   }
458   \addlyrics {
459     The man who feels love's sweet e -- mo -- tion 
460   }
461 }
462 @end lilypond 
463
464 Suppose now that we decide we would like the slurs to be a
465 little heavier.  Is this possible?  The slur is certainly a
466 layout object, so the question is, @q{Is there a property
467 belonging to a slur which controls the heaviness?}  To answer
468 this we must look in the Internals Reference, or IR for short.
469
470 The IR for the version of LilyPond you are using may be found
471 on the LilyPond website at @uref{http://lilypond.org}.  Go to the
472 documentation page and click on the Internals Reference link.
473 For learning purposes you should use the standard html version,
474 not the @q{one big page} or the PDF.  For the next few
475 paragraphs to make sense you will need to actually do this
476 as you read.
477
478 Under the heading @strong{Top} you will see five links.  Select
479 the link to the @emph{Backend}, which is where information about
480 layout objects is to be found.  There, under the heading 
481 @strong{Backend}, select the link to @emph{All layout objects}.
482 The page that appears lists all the layout objects used in your
483 version of LilyPond, in alphabetic order.  Select the link to
484 Slur, and the properties of Slurs are listed.
485
486 (An alternative way of finding this page is from the Notation
487 Reference.  On one of the pages that deals with slurs you may
488 find a link to the Internals Reference.  This link will
489 take you directly to this page, but often it is easier to go
490 straight to the IR and search there.)
491
492 This Slur page in the IR tells us first that Slur objects are 
493 created by the 
494 Slur_engraver.  Then it lists the standard settings.  Note
495 these are @strong{not} in alphabetic order.  Browse down
496 them looking for a property that might control the heaviness
497 of slurs, and you should find
498
499 @example
500 @code{thickness} (number)
501      @code{1.2}
502      Line thickness, generally measured in @code{line-thickness}
503 @end example
504
505 This looks a good bet to change the heaviness. It tells us that
506 the value of @code{thickness} is a simple @emph{number},
507 that the default value is 1.2, and that the units are
508 in another property called @code{line-thickness}.
509
510 As we said earlier, there are few to no explanations in the IR,
511 but we already have enough information to try changing the
512 slur thickness.  We see that the name of the layout object 
513 is @code{Slur}, that the name of the property to change is
514 @code{thickness} and that the new value should be a number
515 somewhat larger than 1.2 if we are to make slurs thicker.
516
517 We can now construct the @code{\override} command by simply
518 substituting the values we have found for the names, omitting
519 the context.  Let's use a very large value for the thickness 
520 at first, so we can be sure the command is working.  We get:
521
522 @example
523 \override Slur #'thickness = #5.0
524 @end example 
525
526 Don't forget the @code{#'} preceding the
527 property name and and @code{#} preceding the new value!
528
529 The final question is, @q{Where should this command be
530 placed?}  While you are unsure and learning, the best 
531 answer is, @q{Within the music, before the first slur and
532 close to it.}  Let's do that:
533
534 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
535 {
536   \time 6/8
537   {
538     % Increase thickness of all following slurs from 1.2 to 5.0
539     \override Slur #'thickness = #5.0
540     r4 b8 b[( g]) g |
541     g[( e]) e d[( f]) a |
542     a g
543   }
544   \addlyrics {
545     The man who feels love's sweet e -- mo -- tion 
546   }
547 }
548 @end lilypond 
549
550 @noindent
551 and we see that the slur is indeed heavier.
552
553 So this is the basic way of constructing @code{\override}
554 commands.  There are a few more complications that we
555 shall meet in later sections, but you now know all the
556 essentials required to make up your own -- but you will
557 still need some practice.  This is provided in the examples
558 which follow.
559
560 @subheading Finding the context
561 @cindex context, finding
562
563 But first, what if we had needed to specify the Context?  
564 What should it be?  We could guess that slurs are in
565 the Voice context, as they are clearly closely associated
566 with individual lines of music, but can we be sure?  To
567 find out, go back to the top of the IR page describing the
568 Slur, where it says @q{Slur objects are created by: Slur
569 engraver}.  So slurs will be created in whichever context
570 the @code{Slur_engraver} is in.  Follow the link to the
571 @code{Slur_engraver} page.  At the very bottom it tells
572 us that @code{Slur_engraver} is part of five Voice contexts,
573 including the standard voice context, @code{Voice}, so our
574 guess was correct.  And because @code{Voice} is one of the
575 lowest level contexts which is implied unambiguously by 
576 the fact that we are entering notes, we can omit it in this 
577 location.
578
579 @subheading Overriding once only
580
581 @cindex overriding once only
582 @cindex once override
583 @funindex \once
584
585 As you can see, @emph{all} the slurs are thicker in the
586 final example above.  But what if we
587 wanted just the first slur to be thicker?  This is achieved
588 with the @code{\once} command.  Placed immediately before
589 the @code{\override} command it causes it to change only the
590 slur which begins on the @strong{immediately following} note.
591 If the
592 immediately following note does not begin a slur the command
593 has no effect at all -- it is not remembered until a slur
594 is encountered, it is simply discarded.  So the command with
595 @code{\once} must be
596 repositioned as follows:
597
598 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
599 {
600   \time 6/8
601   {
602     r4 b8
603     % Increase thickness of immediately following slur only
604     \once \override Slur #'thickness = #5.0
605     b[( g]) g | 
606     g[( e]) e d[( f]) a | 
607     a g 
608   }
609   \addlyrics {
610     The man who feels love's sweet e -- mo -- tion 
611   }
612 }
613 @end lilypond 
614
615 @noindent
616 Now only the first slur is made heavier.
617
618 The @code{\once} command can also be used before the @code{\set}
619 command.
620
621 @subheading Reverting
622
623 @cindex revert
624 @cindex default properties, reverting
625 @funindex \revert
626
627 Finally, what if we wanted just the first two slurs to be
628 heavier?  Well, we could use two commands, each preceded by
629 @code{\once} placed immediately before each of the notes where
630 the slurs begin:
631
632 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
633 {
634   \time 6/8
635   {
636     r4 b8
637     % Increase thickness of immediately following slur only
638     \once \override Slur #'thickness = #5.0
639     b[( g]) g |
640     % Increase thickness of immediately following slur only
641     \once \override Slur #'thickness = #5.0
642     g[( e]) e d[( f]) a | 
643     a g
644   }
645   \addlyrics {
646     The man who feels love's sweet e -- mo -- tion 
647   }
648 }
649 @end lilypond 
650
651 @noindent
652 or we could omit the @code{\once} command and use the
653 @code{\revert} command
654 to return the @code{thickness} property to its default value
655 after the second slur:
656
657 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
658 {
659   \time 6/8
660   {
661     r4 b8
662     % Increase thickness of all following slurs from 1.2 to 5.0
663     \override Slur #'thickness = #5.0
664     b[( g]) g |
665     g[( e]) 
666     % Revert thickness of all following slurs to default of 1.2
667     \revert Slur #'thickness
668     e d[( f]) a | 
669     a g
670   }
671   \addlyrics {
672     The man who feels love's sweet e -- mo -- tion 
673   }
674 }
675 @end lilypond
676
677 @noindent
678 The @code{\revert} command can be used to return any property
679 changed with @code{\override} back to its default value.
680 You may use whichever method best suits what you want to do.
681
682 That concludes our introduction to the IR, and the basic
683 method of tweaking.  Several examples follow in the later
684 sections of this Chapter, partly to introduce you to some of the
685 additional features of the IR, and partly to give you more 
686 practice in extracting information from it.  These examples will
687 contain progressively fewer words of guidance and explanation. 
688
689 @node Properties found in interfaces
690 @subsection Properties found in interfaces
691
692 @cindex interface properties
693 @cindex properties in interfaces
694
695 Suppose now that we wish to print the lyrics in italics.  What
696 form of @code{\override} command do we need to do this?
697 We first look in the IR page listing @q{All layout objects}, as
698 before, and look for an object that might control lyrics.  We
699 find @code{LyricText}, which looks right.  Clicking on this shows
700 the settable properties for lyric text.  These include the 
701 @code{font-series} and @code{font-size}, but nothing that might
702 give an italic shape.   
703 This is because the shape property is one that is common to all
704 font objects, so, rather than including it in every layout
705 object, it is grouped together with other similar common
706 properties and placed in an @strong{Interface}, the
707 @code{font-interface}.
708
709 So now we need to learn how to find the properties of interfaces,
710 and to discover what objects use these interface properties.
711
712 Look again at the IR page which describes LyricText.  At the
713 bottom of the page is a list of clickable (in the html versions
714 of the IR) interfaces which LyricText supports.  The list has 
715 seven items, including @code{font-interface}.
716 Clicking on this brings up the properties associated
717 with this interface, which are also properties of all the objects
718 which support it, including LyricText.
719
720 Now we see all the user-settable properties which control fonts,
721 including @code{font-shape(symbol)}, where @code{symbol} can be
722 set to @code{upright}, @code{italics} or @code{caps}.
723
724 You will notice that @code{font-series} and @code{font-size}
725 are also listed there.
726 This immediately raises the question: Why are the common font
727 properties @code{font-series} and @code{font-size} listed under
728 @code{LyricText} as well as under the interface
729 @code{font-interface} but @code{font-shape} is not?  The answer
730 is that @code{font-series} and @code{font-size} are changed
731 from their global default values when a @code{LyricText} object
732 is created, but @code{font-shape} is not.  The entries in
733 @code{LyricText} then tell you the values for those two
734 properties which apply to @code{LyricText}.  Other objects
735 which support @code{font-interface} will set these
736 properties differently when they are created.
737
738 Let's see if we can now construct the @code{\override} command
739 to change the lyrics to italics.  The object is @code{LyricText},
740 the property is @code{font-shape} and the value is
741 @code{italic}.  As before, we'll omit the context.
742
743 As an aside, although it is an important one, note that because 
744 the values of
745 @code{font-shape} are symbols they must be introduced with a
746 single apostrophe, @code{'}.  That is why apostrophes
747 are needed before @code{thickness} in the earlier example
748 and @code{font-shape}.  These are both symbols too.
749 Symbols are special names which are known internally to 
750 LilyPond.  Some of them are the names of properties,
751 like @code{thickness} or @code{font-shape}, others are in
752 effect special values that can be given to properties, like
753 @code{italic}.  Note the distinction from arbitrary
754 text strings, which would appear as @code{"a text string"}.
755
756 Ok, so the @code{\override} command we need to print the lyrics
757 in italics should be
758
759 @example
760 \override LyricText #'font-shape = #'italic
761 @end example
762
763 @noindent
764 and this should be placed just in front of and close to the
765 lyrics which it should affect, like this:
766
767 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
768 {
769   \time 6/8
770   {
771     r4 b8 b[( g]) g |
772     g[( e]) e d[( f]) a |
773     a g
774   }
775   \addlyrics {
776     \override LyricText #'font-shape = #'italic
777     The man who feels love's sweet e -- mo -- tion 
778   }
779 }
780 @end lilypond
781
782 @noindent
783 and the lyrics are all printed in italics.
784
785 @subheading Specifying the context in lyric mode
786 @cindex context, specifying in lyric mode
787
788 In the case of lyrics, if you try specifying the context in the 
789 format given earlier the command will fail.  A syllable
790 entered in lyricmode is terminated by either a space,
791 a newline or a digit.  All other characters are included 
792 as part of the syllable.  For this reason a space or newline
793 must appear before the terminating @code{@}} to prevent it being
794 included as part of the final syllable.  Similarly,
795 spaces must be inserted before and after the
796 period or dot, @q{.}, separating the context name from the
797 object name, as otherwise the two names are run together and
798 the interpreter cannot recognize them.  So the command should be:
799
800 @example
801 \override Lyrics . LyricText #'font-shape = #'italic
802 @end example
803
804 @warning{In lyrics always leave whitespace between the final
805 syllable and the terminating brace.}
806
807 @warning{In overrides in lyrics always place spaces around
808 the dot between the context name and the object name.}
809
810 @node Types of properties
811 @subsection Types of properties
812
813 @cindex Property types
814
815 So far we have seen two types of property: @code{number} and
816 @code{symbol}.  To be valid, the value given to a property
817 must be of the correct type and obey the rules for that type.
818 The type of property is always shown in brackets after the
819 property name in the IR.  Here is a list of the types you may 
820 need, together with the rules for that type, and some examples.
821 You must always add a hash symbol, @code{#}, of course,
822 to the front of these values when they are entered in the 
823 @code{\override} command.
824
825 @multitable @columnfractions .2 .45 .35
826 @headitem Property type           
827   @tab Rules
828   @tab Examples
829 @item Boolean
830   @tab Either True or False, represented by #t or #f
831   @tab @code{#t}, @code{#f}
832 @item Dimension (in staff space)
833   @tab A positive decimal number (in units of staff space)
834   @tab @code{2.5}, @code{0.34}
835 @item Direction
836   @tab A valid direction constant or its numerical equivalent
837   @tab @code{#LEFT}, @code{#CENTER}, @code{#UP},
838        @code{1}, @code{-1}
839 @item Integer
840   @tab A positive whole number
841   @tab @code{3}, @code{1}
842 @item List
843   @tab A bracketed set of items separated by spaces,
844 preceded by an apostrophe
845   @tab @code{'(left-edge staff-bar)}, @code{'(1)},
846        @code{'(1.0 0.25 0.5)}
847 @item Markup
848   @tab Any valid markup
849   @tab @code{\markup @{ \italic "cresc." @}}
850 @item Moment
851   @tab A fraction of a whole note constructed with the 
852 make-moment function
853   @tab @code{(ly:make-moment 1 4)},
854        @code{(ly:make-moment 3 8)}
855 @item Number
856   @tab Any positive or negative decimal value
857   @tab @code{3.5}, @code{-2.45}
858 @item Pair (of numbers)
859   @tab Two numbers separated by a @q{space . space} and enclosed
860 in brackets preceded by an apostrophe
861   @tab @code{'(2 . 3.5)}, @code{'(0.1 . -3.2)}
862 @item Symbol
863   @tab Any of the set of permitted symbols for that property,
864 preceded by an apostrophe
865   @tab @code{'italic}, @code{'inside}
866 @item Unknown
867   @tab A procedure or @code{#f} (to cause no action)
868   @tab @code{bend::print}, @code{ly:text-interface::print},
869        @code{#f}
870 @item Vector
871   @tab A list of three items enclosed in brackets and preceded
872 by a hash sign, @code{#}.
873   @tab @code{#(#t #t #f)}
874 @end multitable
875
876 @node Appearance of objects
877 @section Appearance of objects
878
879 Let us now put what we have learnt into practice with a few
880 examples which show how tweaks may be used to change the 
881 appearance of the printed music.
882
883 @menu
884 * Visibility and color of objects::  
885 * Size of objects::             
886 * Length and thickness of objects::  
887 @end menu
888
889 @node Visibility and color of objects
890 @subsection Visibility and color of objects
891
892 In the educational use of music we might wish to print a score
893 with certain elements omitted as an exercise for the student,
894 who is required to supply them.  As a simple example,
895 let us suppose the exercise is to supply the missing bar lines 
896 in a piece of music.  But the bar lines are normally inserted
897 automatically.  How do we prevent them printing?
898
899 Before we tackle this, let us remember that object properties
900 are grouped in what are called @emph{interfaces} -- see
901 @ref{Properties found in interfaces}.  This is simply to
902 group together those properties that are commonly required
903 together -- if one of them is required for an object, so are
904 the others.  Some objects then need the properties in some
905 interfaces, others need them from other interfaces.  The
906 interfaces which contain the properties required by a 
907 particular grob are listed in the IR at the bottom of the
908 page describing that grob, and those properties may be
909 viewed by looking at those interfaces.
910
911 We explained how to find information about grobs in 
912 @ref{Properties of layout objects}.  Using the same approach,
913 we go to the IR to find the layout object which prints
914 bar lines.  Going via @emph{Backend} and @emph{All layout objects}
915 we find there
916 is a layout object called @code{BarLine}.  Its properties include
917 two that control its visibility: @code{break-visibility} and
918 @code{stencil}.  Barline also supports a number of interfaces,
919 including the @code{grob-interface}, where we find the
920 @code{transparent} and the @code{color} properties.  All
921 of these can affect the visibility of bar lines (and, of course,
922 by extension, many other layout objects too.)  Let's consider
923 each of these in turn.
924
925 @subheading stencil
926 @cindex stencil property
927
928 This property controls the appearance of the bar lines by specifying
929 the symbol (glyph) which should be printed.  In common
930 with many other properties, it can be set to print nothing by 
931 setting its value to @code{#f}.  Let's try it, as before, omitting
932 the implied Context, @code{Voice}:
933
934 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
935 {
936   \time 12/16
937   \override BarLine #'stencil = ##f
938   c4 b8 c d16 c d8 |
939   g, a16 b8 c d4 e16 |
940   e8
941 }
942 @end lilypond
943
944 The bar lines are still printed.  What is wrong?  Go back to the
945 IR and look again at the page giving the properties of BarLine.
946 At the top of the page it says @qq{Barline objects are created
947 by: Bar_engraver}.  Go to the @code{Bar_engraver} page.
948 At the bottom
949 it gives a list of Contexts in which the bar engraver operates.
950 All of them are of the type @code{Staff}, so the reason the
951 @code{\override} command failed to work as expected is because
952 @code{Barline} is not in the default @code{Voice} context.
953 If the context
954 is specified wrongly, the command simply does not work.  No
955 error message is produced, and nothing is logged in the log
956 file.  Let's try correcting it by adding the correct context:
957
958 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
959 {
960   \time 12/16
961   \override Staff.BarLine #'stencil = ##f
962   c4 b8 c d16 c d8 |
963   g, a16 b8 c d4 e16 |
964   e8
965 }
966 @end lilypond
967
968 Now the bar lines have vanished.
969
970 @subheading break-visibility
971
972 @cindex break-visibility property
973
974 We see from the @code{BarLine} properties in the IR that the
975 @code{break-visibility} property requires a vector of three
976 booleans.
977 These control respectively whether bar lines are printed at
978 the end of a line, in the middle of lines, and at the beginning
979 of lines.  For our example we want all bar lines to be suppressed,
980 so the value we need is @code{#(#f #f #f)}.
981 Let's try that, remembering
982 to include the @code{Staff} context.  Note also that in writing
983 this value we have two hash signs before the opening bracket.
984 One is required as part of the value to introduce a vector, 
985 and one is required, as always, to precede the value itself in 
986 the @code{\override} command.
987
988 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
989 {
990   \time 12/16
991   \override Staff.BarLine #'break-visibility = ##(#f #f #f)
992   c4 b8 c d16 c d8 |
993   g, a16 b8 c d4 e16 |
994   e8
995 }
996 @end lilypond
997
998 And we see this too removes all the bar lines.
999
1000 @subheading transparent
1001 @cindex transparent property
1002
1003 We see from the properties specified in the @code{grob-interface}
1004 page in the IR that the @code{transparent} property is a boolean.
1005 This
1006 should be set to @code{#t} to make the grob transparent.
1007 In this next example let us make the time signature invisible 
1008 rather than the bar lines.  
1009 To do this we need to find the grob name for the time signature.
1010 Back to
1011 the @q{All layout objects} page in the IR to find the properties
1012 of the @code{TimeSignature} layout object.  This is produced by
1013 the @code{Time_signature_engraver} which you can check also lives
1014 in the @code{Staff} context and also supports the
1015 @code{grob-interface}.  So the command to make the time signature
1016 transparent is:
1017
1018 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
1019 {
1020   \time 12/16
1021   \override Staff.TimeSignature #'transparent = ##t
1022   c4 b8 c d16 c d8 |
1023   g, a16 b8 c d4 e16 |
1024   e8
1025 }
1026 @end lilypond
1027
1028 @noindent
1029 The time signature is gone, but this command leaves a gap where 
1030 the time signature should be.  Maybe this is what is wanted for
1031 an exercise for the student to fill it in, but in other 
1032 circumstances a gap might be undesirable.  To remove it, the 
1033 stencil for the time signature should be set to @code{#f}
1034 instead:
1035
1036 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
1037 {
1038   \time 12/16
1039   \override Staff.TimeSignature #'stencil = ##f
1040   c4 b8 c d16 c d8 |
1041   g, a16 b8 c d4 e16 |
1042   e8
1043 }
1044 @end lilypond
1045
1046 @noindent
1047 and the difference is obvious: setting the stencil to @code{#f}
1048 removes the object entirely; making the object @code{transparent}
1049 leaves it where it is, but makes it invisible.
1050
1051 @subheading color
1052 @cindex color property
1053
1054 Finally we could make the bar lines invisible by coloring
1055 them white.  The @code{grob-interface} specifies that the
1056 color property value is a list, but there is no
1057 explanation of what that list should be.  The list it
1058 requires is actually a list of values in internal units,
1059 but, to avoid having to know what these are, several ways
1060 are provided to specify colors.  The first way is to use one
1061 of the @q{normal} colors listed in the first table in
1062 @ruser{List of colors}.  To set the bar lines to white
1063 we write:
1064
1065 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
1066 {
1067   \time 12/16
1068   \override Staff.BarLine #'color = #white
1069   c4 b8 c d16 c d8 |
1070   g, a16 b8 c d4 e16 |
1071   e8
1072 }
1073 @end lilypond
1074
1075 @noindent
1076 and again, we see the bar lines are not visible.  Note that
1077 @emph{white} is not preceded by an apostrophe -- it is not
1078 a symbol, but a @emph{function}.  When called, it provides
1079 the list of internal values required to set the color to
1080 white.  The other colors in the normal list are functions
1081 too.  To convince yourself this is working you might like 
1082 to change the color to one of the other functions in the
1083 list.
1084
1085 @cindex color, X11
1086 @cindex X11 colors
1087
1088 The second way of changing the color is to use the list of
1089 X11 color names in the second list in @ruser{List of colors}.
1090 However, these must be preceded by another function, which
1091 converts X11 color names into the list of internal values,
1092 @code{x11-color}, like this:
1093
1094 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
1095 {
1096   \time 12/16
1097   \override Staff.BarLine #'color = #(x11-color 'white)
1098   c4 b8 c d16 c d8 |
1099   g, a16 b8 c d4 e16 |
1100   e8
1101 }
1102 @end lilypond
1103
1104 @noindent
1105 Note that in this case the function @code{x11-color} takes
1106 a symbol as an argument, so the symbol must be preceded by
1107 an apostrophe and the two enclosed in brackets.
1108
1109 @cindex rgb colors
1110 @cindex color, rgb
1111
1112 There is yet a third function, one which converts RGB values into
1113 internal colors -- the @code{rgb-color} function.  This takes
1114 three arguments giving the intensities of the red, green and 
1115 blue colors.  These take values in the range 0 to 1.  So to
1116 set the color to red the value should be @code{(rgb-color 1 0 0)}
1117 and to white it should be @code{(rgb-color 1 1 1)}:
1118
1119 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
1120 {
1121   \time 12/16
1122   \override Staff.BarLine #'color = #(rgb-color 1 1 1)
1123   c4 b8 c d16 c d8 |
1124   g, a16 b8 c d4 e16 |
1125   e8
1126 }
1127 @end lilypond
1128
1129 Finally, there is also a grey scale available as part of the
1130 X11 set of colors.  These range from black, @code{'grey0'},
1131 to white, @code{'grey100}, in steps of 1.  Let's illustrate
1132 this by setting all the layout objects in our example to
1133 various shades of grey:
1134
1135 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
1136 {
1137   \time 12/16
1138   \override Staff.StaffSymbol   #'color = #(x11-color 'grey30)
1139   \override Staff.TimeSignature #'color = #(x11-color 'grey60)
1140   \override Staff.Clef          #'color = #(x11-color 'grey60)
1141   \override Voice.NoteHead      #'color = #(x11-color 'grey85)
1142   \override Voice.Stem          #'color = #(x11-color 'grey85)
1143   \override Staff.BarLine       #'color = #(x11-color 'grey10)
1144   c4 b8 c d16 c d8 |
1145   g, a16 b8 c d4 e16 |
1146   e8
1147 }
1148 @end lilypond
1149
1150 @noindent
1151 Note the contexts associated with each of the layout objects.
1152 It is important to get these right, or the commands will not
1153 work!  Remember, the context is the one in which the appropriate
1154 engraver is placed.  The default context for engravers can be
1155 found by starting from the layout object, going from there to
1156 the engraver which produces it, and on the engraver page in the
1157 IR it tells you in which context the engraver will normally be 
1158 found.  
1159
1160
1161 @node Size of objects
1162 @subsection Size of objects
1163
1164 Let us begin by looking again at the earlier example 
1165 see @ref{Nesting music expressions}) which showed
1166 how to introduce a new temporary staff, as in an @rglos{ossia}.
1167
1168 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
1169 \new Staff ="main" {
1170        \relative g' {
1171          r4 g8 g c4 c8 d |
1172          e4 r8
1173          <<
1174            { f c c }
1175            \new Staff \with {
1176              alignAboveContext = "main" }
1177            { f8 f c }
1178          >>
1179          r4 |
1180        }
1181      }
1182 @end lilypond
1183
1184 Ossia are normally written without clef and time signature, and
1185 are usually printed slightly smaller than the main staff.  We
1186 already know now how to remove the clef and time signature --
1187 we simply set the stencil of each to @code{#f}, as follows:
1188
1189 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
1190 \new Staff ="main" {
1191   \relative g' {
1192     r4 g8 g c4 c8 d |
1193     e4 r8
1194     <<
1195       { f c c }
1196       \new Staff \with {
1197         alignAboveContext = "main" 
1198       }
1199       {
1200         \override Staff.Clef #'stencil = ##f
1201         \override Staff.TimeSignature #'stencil = ##f
1202         { f8 f c }
1203       }
1204     >>
1205     r4 |
1206   }
1207 }
1208 @end lilypond
1209
1210 @noindent
1211 where the extra pair of braces after the @code{\with} clause are
1212 required to ensure the enclosed overrides and music are applied
1213 to the ossia staff.
1214
1215 But what is the difference between modifying the staff context by 
1216 using @code{\with} and modifying the stencils of the clef and the
1217 time signature with \override?  The main difference is that 
1218 changes made in a @code{\with} clause are made at the time the
1219 context is created, and remain in force as the @strong{default}
1220 values for the duration of that context, whereas
1221 @code{\set} or @code{\override} commands embedded in the
1222 music are dynamic -- they make changes synchronized with
1223 a particular point in the music.  If changes are unset or
1224 reverted using @code{\unset} or @code{\revert} they return to
1225 their default values, which will be the ones set in the 
1226 @code{\with} clause, or if none have been set there, the normal
1227 default values.
1228
1229 Some context properties
1230 can be modified only in @code{\with} clauses.  These are those
1231 properties which cannot sensibly be changed after the context 
1232 has been created.  @code{alignAboveContext} and its partner,
1233 @code{alignBelowContext}, are two such properties -- once the
1234 staff has been created its alignment is decided and it would
1235 make no sense to try to change it later.
1236
1237 The default values of layout object properties can also be set
1238 in @code{\with} clauses.  Simply use the normal @code{\override}
1239 command leaving out the context name, since this is unambiguously
1240 defined as the context which the @code{\with} clause is modifying.
1241 If fact, an error will be generated if a context is specified
1242 in this location.
1243
1244 So we could replace the example above with
1245
1246 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
1247 \new Staff ="main" {
1248   \relative g' {
1249     r4 g8 g c4 c8 d |
1250     e4 r8
1251     <<
1252       { f c c }
1253       \new Staff \with {
1254         alignAboveContext = "main" 
1255         % Don't print clefs in this staff
1256         \override Clef #'stencil = ##f
1257         % Don't print time signatures in this staff
1258         \override TimeSignature #'stencil = ##f
1259       }
1260         { f8 f c }
1261     >>
1262     r4 |
1263   }
1264 }
1265 @end lilypond
1266
1267 Finally we come to changing the size of layout objects.
1268
1269 Some layout objects are created as glyphs selected from
1270 a typeface font.  These include note heads, accidentals, markup, 
1271 clefs, time signatures, dynamics and lyrics.  
1272 Their size is changed by modifying the
1273 @code{font-size} property, as we shall shortly see.  Other
1274 layout objects such as slurs and ties -- in general, spanner
1275 objects -- are drawn individually, so there is no 
1276 @code{font-size}
1277 associated with them.  These objects generally derive their
1278 size from the objects to which they are attached, so usually
1279 there is no need to change their size manually.  Still other 
1280 properties such as the length of stems and bar lines, thickness 
1281 of beams and other lines, and the separation of staff lines all 
1282 need to be modified in special ways. 
1283
1284 Returning to the ossia example, let us first change the font-size.
1285 We can do this in two ways.  We can either change the size of the
1286 fonts of each object type, like @code{NoteHead}s with commands
1287 like
1288
1289 @example
1290 \override NoteHead #'font-size = #-2
1291 @end example
1292
1293 or we can change the size of all fonts by setting a special
1294 property, @code{fontSize}, using @code{\set}, or by including
1295 it in a @code{\with} clause (but without the @code{\set}).
1296
1297 @example
1298 \set fontSize = #-2
1299 @end example
1300
1301 Both of these statements would cause the font size to be reduced
1302 by 2 steps from its previous value, where each
1303 step reduces or increases the size by approximately 12%.
1304
1305 Let's try it in our ossia example:
1306
1307 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
1308 \new Staff ="main" {
1309   \relative g' {
1310     r4 g8 g c4 c8 d |
1311     e4 r8
1312     <<
1313       { f c c }
1314       \new Staff \with {
1315         alignAboveContext = "main" 
1316         \override Clef #'stencil = ##f
1317         \override TimeSignature #'stencil = ##f
1318         % Reduce all font sizes by ~24%
1319         fontSize = #-2
1320       }
1321         { f8 f c }
1322     >>
1323     r4 |
1324   }
1325 }
1326 @end lilypond
1327
1328 This is still not quite right.  The note heads and flags are 
1329 smaller, but the stems are too long in proportion and the
1330 staff lines are spaced too widely apart.  These need to be
1331 scaled down in proportion to the font reduction.  The next
1332 sub-section discusses how this is done.
1333
1334 @node Length and thickness of objects
1335 @subsection Length and thickness of objects
1336
1337 @cindex Distances
1338 @cindex Thickness
1339 @cindex Length
1340 @cindex magstep
1341 @cindex size, changing
1342 @cindex stem length, changing
1343 @cindex staff line spacing, changing
1344
1345 Distances and lengths in LilyPond are generally measured in
1346 staff-spaces, the distance between adjacent lines in the staff,
1347 (or occasionally half staff spaces) while most @code{thickness}
1348 properties are measured in units of an internal property called
1349 @code{line-thickness.}  For example, by default, the lines of
1350 hairpins are given a thickness of 1 unit of @code{line-thickness},
1351 while the @code{thickness} of a note stem is 1.3.  Note, though,
1352 that some thickness properties are different; for example, the
1353 thickness of beams is measured in staff-spaces.
1354
1355 So how are lengths to be scaled in proportion to the font size?
1356 This can be done with the help of a special function called
1357 @code{magstep} provided for exactly this purpose.  It takes
1358 one argument, the change in font size (#-2 in the example above)
1359 and returns a scaling factor suitable for reducing other
1360 objects in proportion.  It is used like this:
1361
1362 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
1363 \new Staff ="main" {
1364   \relative g' {
1365     r4 g8 g c4 c8 d |
1366     e4 r8
1367     <<
1368       { f c c }
1369       \new Staff \with {
1370         alignAboveContext = "main" 
1371         \override Clef #'stencil = ##f
1372         \override TimeSignature #'stencil = ##f
1373         fontSize = #-2
1374         % Reduce stem length and line spacing to match
1375         \override StaffSymbol #'staff-space = #(magstep -2)
1376       }
1377         { f8 f c }
1378     >>
1379     r4 |
1380   }
1381 }
1382 @end lilypond
1383
1384 @noindent
1385 Since the length of stems and many other length-related properties
1386 are always calculated relative to the
1387 value of the @code{staff-space} property these are automatically
1388 scaled down in length too.  Note that this affects only the
1389 vertical scale of the ossia -- the horizontal scale is determined
1390 by the layout of the main music in order to remain synchronized 
1391 with it, so it is not affected by any of these changes in size.
1392 Of course, if the scale of all the main music were changed in this
1393 way then the horizontal spacing would be affected.  This is 
1394 discussed later in the layout section.
1395
1396 This, then, completes the creation of an ossia.  The sizes and
1397 lengths of all other objects may be modified in analogous ways.
1398
1399 For small changes in scale, as in the example above, the
1400 thickness of the various drawn lines such as bar lines,
1401 beams, hairpins, slurs, etc does not usually require global 
1402 adjustment.  If the thickness of any particular layout object
1403 needs to be adjusted this can be best achieved by overriding its
1404 @code{thickness} property.  An example of changing the thickness
1405 of slurs was shown above in @ref{Properties of layout objects}.
1406 The thickness of all drawn objects (i.e., those not produced
1407 from a font) may be changed in the same way. 
1408
1409
1410 @node Placement of objects
1411 @section Placement of objects
1412
1413 @menu
1414 * Automatic behavior::         
1415 * Within-staff objects::        
1416 * Outside staff objects::       
1417 @end menu
1418
1419
1420 @node Automatic behavior
1421 @subsection Automatic behavior
1422
1423 There are some objects in musical notation that belong to
1424 the staff and there are other objects that should be 
1425 placed outside the staff.  These are called within-staff
1426 objects and outside-staff objects respectively.
1427
1428 Within-staff objects are those that are located on the staff
1429 -- note heads, stems, accidentals, etc.  The positions of
1430 these are usually fixed by the music itself -- they are
1431 vertically positioned on specific lines of the staff or are 
1432 tied to other objects that are so positioned.  Collisions of
1433 note heads, stems and accidentals in closely set chords are
1434 normally avoided automatically.  There are commands and
1435 overrides which can modify this automatic behavior, as we
1436 shall shortly see.
1437
1438 Objects belonging outside the staff include things such as
1439 rehearsal marks, text and dynamic markings.  LilyPond's rule for
1440 the vertical placement of outside-staff objects is to place them
1441 as close to the staff as possible but not so close that they
1442 collide with any other object.  LilyPond uses the
1443 @code{outside-staff-priority} property to determine the order in
1444 which the objects should be placed, as follows.
1445
1446 First, LilyPond places all the within-staff objects.
1447 Then it sorts the outside-staff objects according to their 
1448 @code{outside-staff-priority}.  The outside-staff objects are
1449 taken one by one, beginning with the object with the lowest
1450 @code{outside-staff-priority}, and placed so that they do not
1451 collide with any objects that have already been placed. That is, 
1452 if two outside-staff grobs are competing for the same space, the 
1453 one with the lower @code{outside-staff-priority} will be placed
1454 closer to the staff.  If two objects have the same
1455 @code{outside-staff-priority} the one encountered first will be
1456 placed closer to the staff.
1457
1458 In the following example all the markup texts have the same 
1459 priority (since it is not explicitly set).  Note that @q{Text3}
1460 is automatically positioned close to the staff again, nestling
1461 under @q{Text2}.
1462
1463 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
1464 c2^"Text1"
1465 c^"Text2"
1466 c^"Text3"
1467 c^"Text4"
1468 @end lilypond
1469
1470 Staves are also positioned, by default, as closely together as
1471 possible (subject to a minimum separation).  If notes project
1472 a long way towards an adjacent staff they will force the
1473 staves further apart only if an overlap of the notation
1474 would otherwise occur.  The following example demonstrates
1475 this @q{nestling} of the notes on adjacent staves:
1476
1477 @lilypond[quote,ragged-right,verbatim]
1478 <<
1479   \new Staff {
1480     \relative c' { c a, }
1481   }
1482   \new Staff {
1483     \relative c'''' { c a, }
1484   }
1485 >>
1486 @end lilypond 
1487
1488
1489 @node Within-staff objects
1490 @subsection Within-staff objects
1491
1492 We have already seen how the commands @code{\voiceXXX} affect
1493 the direction of slurs, ties, fingering and 
1494 everything else which depends on the direction of the stems.
1495 These commands are essential when writing polyphonic music to
1496 permit interweaving melodic lines to be distinguished.
1497 But occasionally it may be necessary to override this automatic
1498 behavior.  This can be done for whole sections of music or even
1499 for an individual note.  The property which controls this
1500 behavior is the @code{direction} property of each layout object.
1501 We first explain what this does, and then introduce a number of
1502 ready-made commands which avoid your having to code explicit
1503 overrides for the more common modifications.
1504
1505 Some layout objects like slurs and ties curve, bend or point 
1506 either up or down; others like stems and flags also move to
1507 right or left when they point up or down.  This is controlled
1508 automatically when @code{direction} is set.
1509
1510 The following example shows in bar 1 the default behavior of 
1511 stems, 
1512 with those on high notes pointing down and those on low notes
1513 pointing up, followed by four notes with all stems forced down, 
1514 four notes with all stems forced up, and finally four notes
1515 reverted back to the default behavior.
1516
1517 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
1518 a4 g c a
1519 \override Stem #'direction = #DOWN
1520 a g c a
1521 \override Stem #'direction = #UP
1522 a g c a
1523 \revert Stem #'direction
1524 a g c a
1525 @end lilypond
1526
1527 Here we use the constants @code{DOWN} and @code{UP}.
1528 These have the values @code{-1} and @code{+1} respectively, and
1529 these numerical values may be used instead.  The value @code{0}
1530 may also be used in some cases.  It is simply treated as meaning
1531 @code{UP} for stems, but for some objects it means @q{center}.
1532 There is a constant, @code{CENTER} which has the value @code{0}.
1533
1534 However, these explicit overrides are not usually used, as there
1535 are simpler equivalent predefined commands available.  
1536 Here is a table of the commonest.  The meaning of each is stated
1537 where it is not obvious.
1538
1539 @multitable @columnfractions .2 .2 .2 .4
1540 @headitem Down/Left           
1541   @tab Up/Right
1542   @tab Revert
1543   @tab Effect
1544 @item @code{\arpeggioDown}
1545   @tab @code{\arpeggioUp}
1546   @tab @code{\arpeggioNeutral}
1547   @tab Arrow is at bottom, at top, or no arrow
1548 @item @code{\dotsDown}
1549   @tab @code{\dotsUp}
1550   @tab @code{\dotsNeutral}
1551   @tab Direction of movement to avoid staff lines
1552 @item @code{\dynamicDown}
1553   @tab @code{\dynamicUp}
1554   @tab @code{\dynamicNeutral}
1555   @tab
1556 @item @code{\phrasingSlurDown}
1557   @tab @code{\phrasingSlurUp}
1558   @tab @code{\phrasingSlurNeutral}
1559   @tab Note: distinct from slur commands
1560 @item @code{\slurDown}
1561   @tab @code{\slurUp}
1562   @tab @code{\slurNeutral}
1563   @tab
1564 @item @code{\stemDown}
1565   @tab @code{\stemUp}
1566   @tab @code{\stemNeutral}
1567   @tab
1568 @item @code{\textSpannerDown}
1569   @tab @code{\textSpannerUp}
1570   @tab @code{\textSpannerNeutral}
1571   @tab Text entered as spanner is below/above staff
1572 @item @code{\tieDown}
1573   @tab @code{\tieUp}
1574   @tab @code{\tieNeutral}
1575   @tab
1576 @item @code{\tupletDown}
1577   @tab @code{\tupletUp}
1578   @tab @code{\tupletNeutral}
1579   @tab Tuplets are below/above notes
1580 @end multitable
1581
1582 Note that these predefined commands may @strong{not} be
1583 preceded by @code{\once}.  If you wish to limit the
1584 effect to a single note you must either use the equivalent
1585 @code{\once \override} command or use the predefined command
1586 followed after the affected note by the corresponding 
1587 @code{\xxxNeutral} command.
1588
1589 @subheading Fingering
1590 @cindex fingering, placement
1591
1592 The placement of fingering is also affected by the value
1593 of its @code{direction} property, but there are special
1594 commands which allow the fingering of individual notes
1595 of chords to be controlled, with the fingering being placed
1596 above, below, to the left or to the right of each note.
1597
1598 First, here's the effect of @code{direction} on fingering,
1599 the first bar shows the default, then the effect of specifying
1600 @code{DOWN} and @code{UP}:
1601
1602 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
1603 c-5 a-3 f-1 c'-5
1604 \override Fingering #'direction = #DOWN
1605 c-5 a-3 f-1 c'-5
1606 \override Fingering #'direction = #UP
1607 c-5 a-3 f-1 c'-5
1608 @end lilypond
1609
1610 This is how to control fingering on single notes, but the 
1611 @code{direction}
1612 property is ignored for chords.  Instead, by default, the
1613 fingering is automatically placed both above and below the 
1614 notes of a chord, as shown:
1615
1616 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
1617 <c-5 g-3>
1618 <c-5 g-3 e-2>
1619 <c-5 g-3 e-2 c-1>
1620 @end lilypond
1621
1622 Greater control over the placement of fingering of the 
1623 individual notes in a chord is possible by using
1624 the @code{\set fingeringOrientations} command.  The format of
1625 this command is
1626
1627 @example
1628 @code{\set fingeringOrientations = #'([up] [left/right] [down])}
1629 @end example
1630
1631 @noindent
1632 @code{\set} is used because @code{fingeringOrientations} is a
1633 property of the @code{Voice} context, created and used by the
1634 @code{New_fingering_engraver}.
1635
1636 The property may be set to a list of one to three values.
1637 It controls whether fingerings may be placed above (if 
1638 @code{up} appears in the list), below (if @code{down} appears),
1639 to the left (if @code{left} appears, or to the right
1640 (if @code{right} appears).  Conversely, if a location is not
1641 listed, no fingering is placed there.  LilyPond takes these 
1642 constraints and works out the best placement for the fingering
1643 of the notes of the following chords.  Note that @code{left} and
1644 @code{right} are mutually exclusive -- fingering may be placed
1645 only on one side or the other, not both.
1646
1647 To control the placement of the fingering of a single note 
1648 using this command it is necessary to write it as a single 
1649 note chord by placing angle brackets round it.
1650   
1651 Here are a few examples:
1652
1653 @lilypond[quote,fragment,ragged-right,verbatim,relative=1]
1654 \set fingeringOrientations = #'(left)
1655 <f-2>
1656 < c-1  e-2 g-3 b-5 > 4
1657 \set fingeringOrientations = #'(left)
1658 <f-2>
1659 < c-1  e-2 g-3 b-5 > 4
1660 \set fingeringOrientations = #'(up left down)
1661 <f-2>
1662 < c-1  e-2 g-3 b-5 > 4
1663 \set fingeringOrientations = #'(up left)
1664 <f-2>
1665 < c-1  e-2 g-3 b-5 > 4
1666 \set fingeringOrientations = #'(right)
1667 <f-2>
1668 < c-1  e-2 g-3 b-5 > 4
1669 @end lilypond 
1670
1671 @noindent
1672 If the fingering seems a little crowded the @code{font-size}
1673 could be reduced.  The default value can be seen from the
1674 @code{Fingering} object in the IR to be @code{-5}, so let's
1675 try @code{-7}:
1676
1677 @lilypond[quote,fragment,ragged-right,verbatim,relative=1]
1678 \override Fingering #'font-size = #-7
1679 \set fingeringOrientations = #'(left)
1680 <f-2>
1681 < c-1  e-2 g-3 b-5 > 4
1682 \set fingeringOrientations = #'(left)
1683 <f-2>
1684 < c-1  e-2 g-3 b-5 > 4
1685 \set fingeringOrientations = #'(up left down)
1686 <f-2>
1687 < c-1  e-2 g-3 b-5 > 4
1688 \set fingeringOrientations = #'(up left)
1689 <f-2>
1690 < c-1  e-2 g-3 b-5 > 4
1691 \set fingeringOrientations = #'(right)
1692 <f-2>
1693 < c-1  e-2 g-3 b-5 > 4
1694 @end lilypond
1695
1696 @node Outside staff objects
1697 @subsection Outside staff objects
1698
1699 Outside-staff objects are automatically placed to avoid collisions.
1700 Objects with the lower value of the @code{outside-staff-priority}
1701 property are placed nearer to the staff, and other outside-staff
1702 objects are then raised as far as necessary to avoid collisions.
1703 The @code{outside-staff-priority} is defined in the
1704 @code{grob-interface} and so is a property of all layout objects.
1705 By default it is set to @code{#f} for all within-staff objects,
1706 and to a numerical value appropriate to each outside-staff object
1707 when the object is created.  The following table shows
1708 the default numerical values for some of the commonest
1709 outside-staff objects which are, by default, placed in the
1710 @code{Staff} or @code{Voice} contexts.
1711
1712 @multitable @columnfractions .3 .3 .3
1713 @headitem Layout Object           
1714   @tab Priority     
1715   @tab Controls position of:
1716 @item @code{MultiMeasureRestText}
1717   @tab @code{450}
1718   @tab Text over full-bar rests
1719 @item @code{TextScript}
1720   @tab @code{450}
1721   @tab Markup text
1722 @item @code{OttavaBracket}
1723   @tab @code{400}
1724   @tab Ottava brackets
1725 @item @code{TextSpanner}
1726   @tab @code{350}
1727   @tab Text spanners
1728 @item @code{DynamicLineSpanner}
1729   @tab @code{250}
1730   @tab All dynamic markings
1731 @item @code{VoltaBracketSpanner}
1732   @tab @code{100}
1733   @tab Volta brackets
1734 @item @code{TrillSpanner}
1735   @tab @code{50}
1736   @tab Spanning trills
1737 @end multitable
1738
1739 Here is an example showing the default placement of some of
1740 these.
1741
1742 @cindex text spanner
1743 @funindex \startTextSpan
1744 @funindex \stopTextSpan
1745 @cindex ottava bracket
1746
1747 @lilypond[quote,fragment,ragged-right,verbatim,relative=1]
1748 % Set details for later Text Spanner
1749 \override TextSpanner #'bound-details #'left #'text
1750     = \markup { \small \bold Slower }
1751 % Place dynamics above staff
1752 \dynamicUp
1753 % Start Ottava Bracket
1754 #(set-octavation 1)
1755 c' \startTextSpan
1756 % Add Dynamic Text
1757 c\pp
1758 % Add Dynamic Line Spanner
1759 c\<
1760 % Add Text Script
1761 c^Text
1762 c c
1763 % Add Dynamic Text
1764 c\ff c \stopTextSpan
1765 % Stop Ottava Bracket
1766 #(set-octavation 0)
1767 c, c c c
1768 @end lilypond
1769
1770 This example also shows how to create Text Spanners --
1771 text with extender lines above a section of music.  The
1772 spanner extends from the @code{\startTextSpan} command to
1773 the @code{\stopTextSpan} command, and the format of the
1774 text is defined by the @code{\override TextSpanner} command.
1775 For more details see @ruser{Text spanners}.
1776
1777 It also shows how ottava brackets are created.
1778
1779 Note that bar numbers, metronome marks and rehearsal marks
1780 are not shown. By default these are created in the
1781 @code{Score} context and their @code{outside-staff-priority}
1782 is ignored relative to the layout objects which are created
1783 in the @code{Staff} context.
1784 If you wish to place bar numbers, metronome marks or rehearsal
1785 marks in accordance with the value of their
1786 @code{outside-staff-priority} the @code{Bar_number_engraver},
1787 @code{Metronome_mark_engraver} or @code{Mark_engraver} respectively
1788 should be removed from the @code{Score} context and placed in the
1789 top @code{Staff} context.  If this is done, these marks will be
1790 given the following default @code{outside-staff-priority} values:
1791
1792 @multitable @columnfractions .3 .3
1793 @headitem Layout Object           @tab Priority
1794 @item @code{RehearsalMark}        @tab @code{1500}
1795 @item @code{MetronomeMark}        @tab @code{1000}
1796 @item @code{BarNumber}            @tab @code{ 100}
1797 @end multitable
1798
1799 If the default values of @code{outside-staff-priority} do not
1800 give you the placing you want, the priority of any of the objects
1801 may be overridden.  Suppose we would
1802 like the ottava bracket to be placed below the text spanner in the
1803 example above.  All we need to do is to look up the priority of
1804 @code{OttavaBracket} in the IR or in the tables above, and reduce
1805 it to a value lower than that of a @code{TextSpanner}, remembering
1806 that @code{OttavaBracket} is created in the @code{Staff} context:
1807
1808 @lilypond[quote,fragment,ragged-right,verbatim,relative=1]
1809 % Set details for later Text Spanner
1810 \override TextSpanner #'bound-details #'left #'text
1811     = \markup { \small \bold Slower }
1812 % Place dynamics above staff
1813 \dynamicUp
1814 %Place following Ottava Bracket below Text Spanners
1815 \once \override Staff.OttavaBracket #'outside-staff-priority = #340
1816 % Start Ottava Bracket
1817 #(set-octavation 1)
1818 c' \startTextSpan
1819 % Add Dynamic Text
1820 c\pp
1821 % Add Dynamic Line Spanner
1822 c\<
1823 % Add Text Script
1824 c^Text
1825 c c
1826 % Add Dynamic Text
1827 c\ff c \stopTextSpan
1828 % Stop Ottava Bracket
1829 #(set-octavation 0)
1830 c, c c c
1831 @end lilypond
1832
1833 Changing the @code{outside-staff-priority} can also be used to
1834 control the vertical placement of individual objects, although
1835 the results may not always be desirable.  Suppose we would
1836 like @qq{Text3} to be placed above @qq{Text4} in the example
1837 under Automatic behavior, above (see @ref{Automatic behavior}).
1838 All we need to do is to look up the priority of @code{TextScript}
1839 in the IR or in the tables above, and increase the priority of
1840 @qq{Text3} to a higher value:
1841
1842 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
1843 c2^"Text1"
1844 c^"Text2"
1845 \once \override TextScript #'outside-staff-priority = #500
1846 c^"Text3"
1847 c^"Text4"
1848 @end lilypond
1849
1850 This certainly lifts @qq{Text3} above @qq{Text4} but it also
1851 lifts it above @qq{Text2}, and @qq{Text4} now drops down.
1852 Perhaps this is not so good.  What we would really like to do
1853 is to position all the annotation at the same distance above
1854 the staff?  To do this, we clearly
1855 will need to space the notes out horizontally to make more
1856 room for the text.  This is done using the @code{textLengthOn}
1857 command.
1858
1859 @subheading \textLengthOn
1860
1861 @funindex \textLengthOn
1862 @cindex notes, spreading out with text
1863
1864 By default, text produced by markup takes up no horizontal space
1865 as far as laying out the music is concerned.  The @code{\textLengthOn}
1866 command reverses this behavior, causing the notes to be spaced
1867 out as far as is necessary to accommodate the text:
1868
1869 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
1870 \textLengthOn  % Cause notes to space out to accommodate text
1871 c2^"Text1"
1872 c^"Text2"
1873 c^"Text3"
1874 c^"Text4"
1875 @end lilypond  
1876
1877 The command to revert to the default behavior is 
1878 @code{\textLengthOff}.  Remember @code{\once} only works with
1879 @code{\override}, @code{\set}, @code{\revert} or @code{unset},
1880 so cannot be used with @code{\textLengthOn}.
1881
1882 Markup text will also avoid notes which project above the staff.
1883 If this is not desired, the automatic displacement upwards may
1884 be turned off by setting the priority to @code{#f}.  Here's an
1885 example to show how markup text interacts with such notes.
1886
1887 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
1888 % This markup is short enough to fit without collision
1889 c2^"Tex"
1890 c''2
1891 R1
1892 % This is too long to fit, so it is displaced upwards
1893 c,,2^"Text"
1894 c''2
1895 R1
1896 % Turn off collision avoidance
1897 \once \override TextScript #'outside-staff-priority = ##f
1898 c,,2^"Long Text   "
1899 c''2
1900 R1
1901 % Turn off collision avoidance
1902 \once \override TextScript #'outside-staff-priority = ##f
1903 \textLengthOn  % and turn on textLengthOn
1904 c,,2^"Long Text   "  % Spaces at end are honored
1905 c''2
1906 @end lilypond
1907
1908
1909 @subheading Dynamics
1910
1911 Dynamic markings will normally be positioned beneath the
1912 staff, but may be positioned above with the @code{dynamicUp}
1913 command.  They will be positioned vertically relative to the
1914 note to which they are attached, and will float below (or above)
1915 all within-staff objects such as phrasing slurs and bar numbers.
1916 This can give quite acceptable results, as this example
1917 shows:
1918
1919 @lilypond[quote,fragment,ragged-right,verbatim,relative=1]
1920 \clef "bass"
1921 \key aes \major
1922 \time 9/8
1923 \dynamicUp
1924 bes4.~\f\< \( bes4 bes8 des4\ff\> c16 bes\! |
1925 ees,2.~\)\mf ees4 r8 |
1926 @end lilypond
1927
1928 However, if the notes and attached dynamics are close
1929 together the automatic placement will avoid collisions
1930 by displacing later dynamic markings further away, but this may 
1931 not be the optimum placement, as this rather artificial example
1932 shows:
1933
1934 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
1935 \dynamicUp
1936 a4\f b\mf c\mp b\p
1937 @end lilypond
1938
1939 @noindent
1940 Should a similar situation arise in @q{real} music, it may
1941 be preferable to space out the notes
1942 a little further, so the dynamic markings can all fit at the
1943 same vertical distance from the staff.  We were able to do this
1944 for markup text by using the @code{\textLengthOn} command, but there
1945 is no equivalent command for dynamic marks.  So we shall have to
1946 work out how to do this using @code{\override} commands.
1947
1948 @subheading Grob sizing
1949
1950 @cindex grob sizing
1951 @cindex sizing grobs
1952 @cindex @code{X-offset}
1953 @cindex @code{Y-offset}
1954 @cindex @code{X-extent}
1955 @cindex @code{Y-extent}
1956
1957 First we must learn how grobs are sized.  All grobs have a
1958 reference point defined within them which is used to position 
1959 them relative to their parent object.  This point in the grob
1960 is then positioned at a horizontal distance, @code{X-offset},
1961 and at a vertical distance, @code{Y-offset}, from its parent.
1962 The horizontal extent of the object is given by a pair of
1963 numbers, @code{X-extent}, which say where the left and right
1964 edges are relative to the reference point.  The vertical extent
1965 is similarly defined by a pair of numbers, @code{Y-extent}.
1966 These are properties of all grobs which support the 
1967 @code{grob-interface}.
1968
1969 @cindex @code{extra-spacing-width}
1970
1971 By default, outside-staff objects are given a width of zero so
1972 that they may overlap in the horizontal direction.  This is done
1973 by the trick of adding infinity to the leftmost extent and 
1974 minus infinity to the rightmost extent by setting the 
1975 @code{extra-spacing-width} to @code{'(+inf.0 . -inf.0)}.  So
1976 to ensure they do not overlap in the horizontal direction we
1977 must override this value of @code{extra-spacing-width} to
1978 @code{'(0 . 0)} so the true width shines through.  This is
1979 the command to do this for dynamic text:
1980
1981 @example
1982 \override DynamicText #'extra-spacing-width = #'(0 . 0)
1983 @end example
1984
1985 @noindent
1986 Let's see if this works in our previous example:
1987
1988 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
1989 \dynamicUp
1990 \override DynamicText #'extra-spacing-width = #'(0 . 0)
1991 a4\f b\mf c\mp b\p
1992 @end lilypond
1993
1994 @noindent
1995 Well, it has certainly stopped the dynamic marks being
1996 displaced, but two problems remain.  The marks should be
1997 spaced a little further apart and it would be better
1998 if they were all the same distance from the staff.
1999 We can solve the first problem easily.  Instead of making
2000 the @code{extra-spacing-width} zero we could add a little
2001 more to it.  The units are the space between two staff
2002 lines, so moving the left edge half a unit to the left and the
2003 right edge half a unit to the right should do it:
2004
2005 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
2006 \dynamicUp
2007 % Extend width by 1 staff space
2008 \override DynamicText #'extra-spacing-width = #'(-0.5 . 0.5)
2009 a4\f b\mf c\mp b\p
2010 @end lilypond
2011
2012 @noindent
2013 This looks better, but maybe we would prefer the dynamic marks
2014 to be aligned along the same baseline rather than going up and
2015 down with the notes.  The property to do this is
2016 @code{staff-padding} which is covered in the following section.
2017
2018
2019 @node Collisions of objects
2020 @section Collisions of objects
2021
2022 @menu
2023 * Moving objects::              
2024 * Fixing overlapping notation::  
2025 * Real music example::          
2026 @end menu
2027
2028 @node Moving objects
2029 @subsection Moving objects
2030
2031 This may come as a surprise, but LilyPond is not perfect.  Some
2032 notation elements can overlap.  This is unfortunate, but in fact
2033 rather rare.  Usually the need to move objects is for clarity or
2034 aesthetic reasons -- they would look better with a little more
2035 or a little less space around them.
2036
2037 There are three main approaches to resolving overlapping
2038 notation.  They should be considered in the following order:
2039
2040 @enumerate
2041 @item
2042 The @strong{direction} of one of the overlapping objects may
2043 be changed using the predefined commands listed above for
2044 within-staff objects (see @ref{Within-staff objects}).
2045 Stems, slurs, beams, ties, dynamics, text and tuplets may be
2046 repositioned easily in this way.  The limitation is that you
2047 have a choice of only two positions, and neither may be
2048 suitable.
2049
2050 @item
2051 The @strong{object properties}, which LilyPond uses
2052 when positioning layout objects, may be modified using
2053 @code{\override}.  The advantages
2054 of making changes to this type of property are (a) that some
2055 other objects will be moved automatically if necessary to make
2056 room and (b) the single override can apply to all instances of
2057 the same type of object.  Such properties include:
2058 @itemize
2059
2060 @item
2061 @code{direction}
2062
2063 This has already been covered in some detail -- see
2064 @ref{Within-staff objects}.
2065
2066 @item
2067 @code{padding}, @code{left-padding},
2068 @code{right-padding}, @code{staff-padding}
2069
2070 @cindex left-padding property
2071 @cindex padding property
2072 @cindex right-padding property
2073 @cindex staff-padding property
2074 As an object is being positioned the value of its @code{padding}
2075 property specifies the gap that must be left between itself and
2076 the nearest edge of the object against which it is being
2077 positioned.  Note that it is the @code{padding} value of the object
2078 @strong{being placed} that is used;
2079 the @code{padding} value of the object which is already placed is
2080 ignored.  Gaps specified by @code{padding} can be applied
2081 to all objects which support the @code{side-position-interface}.
2082
2083 Instead of @code{padding}, the placement of groups of accidentals
2084 is controlled by @code{left-padding} and @code{right-padding}.
2085 These properties are to be found in the @code{AccidentalPlacement}
2086 object which, note, lives in the @strong{staff} context.  Because
2087 accidentals are always positioned after and to the left of
2088 note heads only the @code{right-padding} property has any effect.
2089
2090 The @code{staff-padding} property is closely related to the
2091 @code{padding} property:  @code{padding}
2092 controls the minimum amount of space between any object which
2093 supports the @code{side-position-interface} and the nearest
2094 other object (generally the note or the staff lines);
2095 @code{staff-padding} applies only to those objects which are always
2096 set outside the staff -- it controls the minimum amount of space
2097 that should be inserted between that object and the staff.  Note
2098 that @code{staff-padding} has no effect on objects which are
2099 positioned relative to the note rather than the staff, even though
2100 it may be overridden without error for such objects -- it is simply
2101 ignored.
2102
2103 To discover which padding property is required for the object
2104 you wish to reposition, you
2105 need to return to the IR and look up the object's properties.
2106 Be aware that the padding properties might not be located in the
2107 obvious object, so look in objects that appear to be related.
2108
2109 All padding values are measured in staff spaces.  For most
2110 objects, this value is set by default to be around 1.0 or less
2111 (it varies with each object).  It may be overridden if a larger
2112 (or smaller) gap is required.
2113
2114 @item
2115 @code{self-alignment-X}
2116
2117 @cindex self-alignment-X property
2118 This property can be used to align the object to the left, to 
2119 the right, or to center it with respect to the parent object's 
2120 reference point.  It may be used with all objects which support
2121 the @code{self-alignment-interface}.  In general these are objects
2122 that contain text.  The values are @code{LEFT}, @code{RIGHT}
2123 or @code{CENTER}.  Alternatively, a numerical value between 
2124 @code{-1} and @code{+1} may be specified, where @code{-1} is 
2125 left-aligned, @code{+1} is right-aligned, and numbers in between 
2126 move the text progressively from left-aligned to right-aligned.
2127 Numerical values greater than @code{1} may be specified to move
2128 the text even further to the left, or less than @code{-1} to
2129 move the text even further to the right.  A change of @code{1}
2130 in the value corresponds to a movement of half the text's length.
2131
2132 @item
2133 @code{extra-spacing-width}
2134
2135 @cindex extra-spacing-width property
2136 This property is available for all objects which support the
2137 @code{item-interface}.  It takes two numbers, the first is added
2138 to the leftmost extent and the second is added to the rightmost
2139 extent.  Negative numbers move the edge to the left, positive to
2140 the right, so to widen an object the first number must be negative,
2141 the second positive.  Note that not all objects honor both
2142 numbers.  For example, the @code{Accidental} object only takes
2143 notice of the first (left edge) number.
2144
2145 @item
2146 @code{staff-position}
2147
2148 @cindex staff-position property
2149 @code{staff-position} is a property of the
2150 @code{staff-symbol-referencer-interface}, which is supported by
2151 objects which are positioned relative to the staff.  It specifies
2152 the vertical position of the object relative to the center line
2153 of the staff in half staff-spaces.  It is useful in resolving
2154 collisions between layout objects like multi-measure rests, ties
2155 and notes in different voices.
2156
2157 @item
2158 @code{force-hshift}
2159
2160 @cindex force-hshift property
2161
2162 Closely spaced notes in a chord, or notes occurring at the same
2163 time in different voices, are arranged in two, occasionally more,
2164 columns to prevent the note heads overlapping.  These are called
2165 note columns, and an object called @code{NoteColumn} is created
2166 to lay out the notes in that column.
2167
2168 The @code{force-hshift}
2169 property is a property of a @code{NoteColumn} (actually of the
2170 @code{note-column-interface}).  Changing it permits a note column
2171 to be moved in units appropriate to a note column, viz. the note
2172 head width of the first voice note.  It should be used in
2173 complex situations where the normal @code{\shiftOn} commands (see
2174 @ref{Explicitly instantiating voices}) do
2175 not resolve the note conflict.  It is preferable to the
2176 @code{extra-offset} property for this purpose as there is no need
2177 to work out the distance in staff-spaces, and moving the notes
2178 into or out of a @code{NoteColumn} affects other actions such as
2179 merging note heads.
2180
2181 @end itemize
2182
2183 Objects do not all have all of these properties in general.
2184 It is necessary to go to the IR to look up which properties
2185 are available for the object in question.
2186
2187 @item
2188 Finally, when all else fails, objects may be manually repositioned
2189 relative to the staff center line vertically, or by
2190 displacing them by any distance to a new position.  The
2191 disadvantages are that the correct values for the repositioning
2192 have to be worked out, often by trial and error, for every object
2193 individually, and, because the movement is done after LilyPond has
2194 placed all other objects, the user is responsible for avoiding any
2195 collisions that might ensue.  But the main difficulty with this
2196 approach is that the repositioning values may need to be reworked
2197 if the music is later modified.  The properties that can be used
2198 for this type of manual repositioning are:
2199
2200 @table @code
2201 @item extra-offset
2202 @cindex extra-offset property
2203 This property applies to any layout object
2204 supporting the @code{grob-interface}.  It takes a pair of
2205 numbers which specify the extra displacement in the horizontal and
2206 vertical directions.  Negative numbers move the object to
2207 the left or down.  The units are staff-spaces.  The extra
2208 displacement is made after the typesetting of objects is
2209 finished, so an object may be repositioned anywhere without
2210 affecting anything else.
2211
2212 @item positions
2213 @cindex positions property
2214 This is most useful for manually adjusting the slope and height
2215 of beams, slurs, and tuplets.   It takes a pair of numbers
2216 giving the position of the left and right ends of the beam, slur,
2217 etc. relative to the center line of the staff.  Units are
2218 staff-spaces.  Note, though, that slurs and phrasing slurs cannot
2219 be repositioned by arbitrarily large amounts.  LilyPond first
2220 generates a list of possible positions for the slur and by default
2221 finds the slur that @qq{looks best}.  If the @code{positions}
2222 property has been overridden the slur that is closest to the
2223 requested positions is selected from the list.
2224 @end table
2225
2226 Objects do not all have all of these properties in general.
2227 It is necessary to go to the IR to look up which properties
2228 are available for the object in question.
2229
2230 @end enumerate
2231
2232 Here is a list of the objects which are most likely to be
2233 involved in collisions, together with the name of the object which
2234 should be looked up in the IR in order to discover which properties
2235 should be used to move them.
2236
2237 @multitable @columnfractions .5 .5
2238 @headitem Object type           @tab Object name
2239 @item Articulations             @tab @code{Script}
2240 @item Beams                     @tab @code{Beam}
2241 @item Dynamics (vertically)     @tab @code{DynamicLineSpanner}
2242 @item Dynamics (horizontally)   @tab @code{DynamicText}
2243 @item Fingerings                @tab @code{Fingering}
2244 @item Rehearsal / Text marks    @tab @code{RehearsalMark}
2245 @item Slurs                     @tab @code{Slur}
2246 @item Text e.g. @code{^"text"}  @tab @code{TextScript}
2247 @item Ties                      @tab @code{Tie}
2248 @item Tuplets                   @tab @code{TupletBracket}
2249 @end multitable
2250
2251
2252 @node Fixing overlapping notation
2253 @subsection Fixing overlapping notation
2254
2255 Let's now see how the properties in the previous section can
2256 help to resolve overlapping notation.
2257
2258 @subheading padding property
2259 @cindex padding property
2260
2261 The @code{padding} property can be set to increase
2262 (or decrease) the distance between symbols that are printed
2263 above or below notes.
2264
2265 @lilypond[quote,fragment,relative=1,verbatim]
2266 c2\fermata
2267 \override Script #'padding = #3
2268 b2\fermata
2269 @end lilypond
2270
2271 @lilypond[quote,fragment,relative=1,verbatim]
2272 % This will not work, see below:
2273 \override MetronomeMark #'padding = #3
2274 \tempo 4=120
2275 c1
2276 % This works:
2277 \override Score.MetronomeMark #'padding = #3
2278 \tempo 4=80
2279 d1
2280 @end lilypond
2281
2282 Note in the second example how important it is to figure out what
2283 context handles a certain object.  Since the @code{MetronomeMark}
2284 object
2285 is handled in the @code{Score} context, property changes in the
2286 @code{Voice} context will not be noticed.  For more details, see
2287 @ruser{Constructing a tweak}.
2288
2289 If the @code{padding} property of an object is increased when that
2290 object is in a stack of objects being positioned according to
2291 their @code{outside-staff-priority}, then that object and all
2292 objects outside it are moved.
2293
2294
2295 @subheading left-padding and right-padding
2296 @cindex left-padding property
2297 @cindex right-padding property
2298
2299 The @code{right-padding} property affects the spacing between the
2300 accidental and the note to which it applies.  It is not often
2301 required, but the following example shows one situation where it
2302 is needed.  Suppose we wish to show a chord containing both
2303 a B-natural and a B-flat.  To avoid ambiguity we would like to
2304 precede the notes with both a natural and a flat sign.  Here
2305 are a few attempts to do this:
2306
2307 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
2308 <b bes>
2309 <b! bes>
2310 <b? bes>
2311 @end lilypond
2312
2313 None work, with the second two showing bad collisions between
2314 the two signs.
2315
2316 One way of achieving this is to override the accidental stencil
2317 with a markup containing the natural and flat symbols in the
2318 order we would like, like this:
2319
2320 @lilypond[quote,ragged-right,verbatim]
2321 naturalplusflat = \markup { \natural \flat }
2322 \relative c'' {
2323   \once \override Accidental
2324     #'stencil = #ly:text-interface::print
2325   \once \override Accidental #'text = #naturalplusflat
2326   \once \override Score.AccidentalPlacement #'right-padding = #1.5
2327   <b bes>
2328 }
2329 @end lilypond
2330
2331 @noindent
2332 This necessarily uses an override for the accidental stencil which
2333 will not be covered until later.  The stencil type must be a
2334 procedure, here changed to print the contents of the @code{text}
2335 property of @code{Accidental}, which itself is set to be a natural 
2336 sign followed by a flat sign.  These are then moved further away 
2337 from the note head by overriding @code{right-padding}.
2338
2339 @noindent
2340
2341 @subheading staff-padding property
2342 @cindex staff-padding property
2343
2344 @code{staff-padding} can be used to align objects such as dynamics
2345 along a baseline at a fixed height above the staff, rather than
2346 at a height dependent on the position of the note to which they
2347 are attached.  It is not a property of
2348 @code{DynamicText} but of @code{DynamicLineSpanner}.
2349 This is because the baseline should apply equally to @strong{all}
2350 dynamics, including those created as extended spanners.
2351 So this is the way to align the dynamic marks in the example
2352 taken from the previous section:
2353
2354 @lilypond[quote,fragment,ragged-right,verbatim,relative=2]
2355 \dynamicUp
2356 % Extend width by 1 unit
2357 \override DynamicText #'extra-spacing-width = #'(-0.5 . 0.5)
2358 % Align dynamics to a base line 2 units above staff
2359 \override DynamicLineSpanner #'staff-padding = #2
2360 a4\f b\mf c\mp b\p
2361 @end lilypond
2362
2363
2364 @subheading self-alignment-X property
2365 @cindex self-alignment-X property
2366
2367 The following example shows how this can resolve the collision
2368 of a string fingering object with a note's stem by aligning the
2369 right edge with the reference point of the parent note:
2370
2371 @lilypond[quote,fragment,ragged-right,verbatim,relative=3]
2372 \voiceOne
2373 < a \2 >
2374 \once \override StringNumber #'self-alignment-X = #RIGHT
2375 < a \2 >
2376 @end lilypond
2377
2378 @subheading staff-position property
2379 @cindex staff-position property
2380
2381 Multimeasure rests in one voice can collide with notes in another.
2382 Since these rests are typeset centered between the bar lines, it
2383 would require significant effort for LilyPond to figure out which
2384 other notes might collide with it, since all the current collision
2385 handling between notes and between notes and rests is done only
2386 for notes and rests that occur at the same time.  Here's an
2387 example of a collision of this type:
2388
2389 @lilypond[quote,verbatim,fragment,ragged-right, relative=1]
2390 << {c c c c} \\ {R1} >>
2391 @end lilypond
2392
2393 The best solution here is to move the multimeasure rest down,
2394 since the rest is in voice two.
2395 The default in @code{\voiceTwo} (i.e. in the second voice of a
2396 @code{<<@{...@} \\ @{...@}>>} construct)
2397 is that @code{staff-position} is set to -4 for MultiMeasureRest,
2398 so we need to move it, say, four half-staff spaces down to
2399 @code{-8}.
2400
2401 @lilypond[quote,verbatim,fragment,ragged-right, relative=1]
2402 <<
2403   {c c c c}
2404 \\
2405   \override MultiMeasureRest #'staff-position = #-8
2406   {R1}
2407 >>
2408 @end lilypond
2409
2410 This is better than using, for example, @code{extra-offset},
2411 because the ledger line above the rest is inserted automatically.
2412
2413 @subheading extra-offset property
2414 @cindex extra-offset property
2415
2416 The @code{extra-offset} property provides complete control over the
2417 positioning of an object both horizontally and vertically.
2418
2419 In the following example, the second fingering is moved a little to
2420 the left, and 1.8 staff space downwards:
2421
2422 @lilypond[quote,fragment,relative=1,verbatim]
2423 \stemUp
2424 f-5
2425 \once \override Fingering
2426     #'extra-offset = #'(-0.3 . -1.8)
2427 f-5
2428 @end lilypond
2429
2430
2431 @subheading positions property
2432 @cindex positions property
2433
2434 The @code{positions} property allows the position and slope of
2435  tuplets, slurs, phrasing slurs and beams to be controlled
2436 manually.  Here's an example which has an ugly phrasing slur
2437 due to its trying to avoid the slur on the acciaccatura.
2438
2439 @lilypond[quote,verbatim,fragment,ragged-right,relative=1]
2440 r4  \acciaccatura e8\( d8 c ~c d c d\)
2441 @end lilypond
2442
2443 @noindent
2444 We could simply move the phrasing slur above the notes, and this
2445 would be the preferred solution:
2446
2447 @lilypond[quote,verbatim,fragment,ragged-right,relative=1]
2448 r4
2449 \phrasingSlurUp
2450 \acciaccatura e8\( d8 c ~c d c d\)
2451 @end lilypond
2452
2453 @noindent
2454 but if there were some reason why this could not be done the
2455 other alternative would be to move the left end of the phrasing
2456 slur down a little using the @code{positions} property.  This
2457 also resolves the rather nasty shape.
2458
2459 @lilypond[quote,verbatim,fragment,ragged-right,relative=1]
2460 r4
2461 \once \override PhrasingSlur #'positions = #'(-4 . -3)
2462 \acciaccatura
2463 e8\( d8 c ~c d c d\)
2464 @end lilypond
2465
2466 Here's a further example taken from the opening of the left-hand
2467 staff of Chopin's Prelude Op 28 No. 2.  We see that the beam
2468 collides with the upper notes:
2469
2470 @lilypond[quote,verbatim,fragment,ragged-right]
2471 {
2472 \clef "bass"
2473 << {b,8 ais, b, g,} \\ {e, g e, g} >>
2474 << {b,8 ais, b, g,} \\ {e, g e, g} >>
2475 }
2476 @end lilypond
2477
2478 @noindent
2479 This can be resolved by manually moving both ends of the beam
2480 up from their position at 2 staff-spaces above the center line to,
2481 say, 3:
2482
2483 @lilypond[quote,verbatim,fragment,ragged-right]
2484 {
2485   \clef "bass"
2486   <<
2487     \override Beam #'positions = #'(3 . 3)
2488     {b,8 ais, b, g,}
2489   \\
2490     {e, g e, g}
2491   >>
2492   << {b,8 ais, b, g,} \\ {e, g e, g} >>
2493 }
2494 @end lilypond
2495
2496 @noindent
2497 Note that the override continues to apply in the first voice of
2498 the second block of quavers, but not to any of the beams in the
2499 second voice.
2500
2501 @subheading force-hshift property
2502 @cindex force-hshift property
2503 @c FIXME: formatting stuff  (ie not important right now IMO)
2504 @c @a nchor Chopin finally corrected TODOgp
2505
2506 We can now see how to apply the final corrections to the Chopin
2507 example introduced at the end of @ref{I'm hearing Voices}, which
2508 was left looking like this:
2509
2510 @lilypond[quote,verbatim,fragment,ragged-right]
2511 \new Staff \relative c'' {
2512   \key aes \major
2513   <<
2514     { c2 aes4. bes8 } \\
2515     { aes2 f4 fes   } \\
2516     { \voiceFour
2517       <ees c>2
2518       des2
2519     }
2520   >> |
2521   <c ees aes c>1 |
2522 }
2523 @end lilypond
2524
2525 @noindent
2526 The lower two notes of the first chord (i.e,
2527 those in the third voice) should not be shifted away from the
2528 note column of the higher two notes.  To correct this we set
2529 @code{force-hshift}, which is a property of
2530 @code{NoteColumn}, of these notes to zero.
2531 The lower note of the second chord is best placed just to the
2532 right of the higher notes.  We achieve this by setting
2533 @code{force-hshift} of this note to 0.5, ie half a note head's
2534 width to the right of the note column of the higher notes.
2535
2536 Here's the final result:
2537
2538 @lilypond[quote,verbatim,fragment,ragged-right]
2539 \new Staff \relative c'' {
2540   \key aes \major
2541   <<
2542     { c2 aes4. bes8 } \\
2543     { aes2 f4 fes   } \\
2544     { \voiceFour
2545       \once \override NoteColumn #'force-hshift = #0 <ees c>2
2546       \once \override NoteColumn #'force-hshift = #0.5 des2
2547     }
2548   >> |
2549   <c ees aes c>1 |
2550 }
2551 @end lilypond
2552
2553
2554 @node Real music example
2555 @subsection Real music example
2556
2557 We end this section on Tweaks by showing the steps to be taken to 
2558 deal with a tricky example which needs several tweaks to produce 
2559 the desired output.  The example has been deliberately chosen to 
2560 illustrate the use of the Notation Reference to resolve unusual 
2561 problems with notation.  It is not representative of more usual 
2562 engraving process, so please do not let these difficulties put 
2563 you off!  Fortunately, difficulties like these are not very common!
2564
2565 The example is from Chopin's Première Ballade, Op. 23, bars 6 to
2566 9, the transition from the opening Lento to Moderato.
2567 Here, first, is what we want the output to look like, but to avoid
2568 over-complicating the example too much we have left out the
2569 dynamics, fingering and pedalling.
2570
2571 @c The following should appear as music without code
2572 @lilypond[quote,ragged-right]
2573 rhMusic = \relative c'' {
2574   r2
2575   c4.\( g8 |
2576   \once \override Tie #'staff-position = #3.5
2577   bes1~ |
2578   \bar "||"
2579   \time 6/4
2580   \override Staff.NoteCollision #'merge-differently-headed = ##t
2581   \override Staff.NoteCollision #'merge-differently-dotted = ##t
2582   bes2.^\markup {\bold "Moderato"} r8
2583   <<
2584     {c,8[ d fis bes a] | }
2585   \\
2586     % Reposition the c2 to the right of the merged note
2587     {c,8~ \once \override NoteColumn #'force-hshift = #1.0
2588     % Move the c2 out of the main note column so the merge will work
2589     \shiftOnn c2}
2590   \\
2591     % Stem on the d2 must be down to permit merging
2592     {s8 \stemDown \once \override Stem #'transparent = ##t d2}
2593   \\
2594     {s4 fis4.}
2595   >>
2596   \revert Staff.NoteCollision #'merge-differently-headed
2597   \revert Staff.NoteCollision #'merge-differently-dotted
2598   g2.\)
2599 }
2600
2601 lhMusic = \relative c' {
2602   r2 <c g ees>2( |
2603   <d g, d>1)\arpeggio |
2604   r2. d,,4 r4 r |
2605   r4
2606 }
2607
2608 \score {
2609   \new PianoStaff <<
2610     \new Staff = "RH"  <<
2611       \key g \minor
2612       \rhMusic
2613     >>
2614     \new Staff = "LH" <<
2615       \key g \minor
2616       \clef "bass"
2617       \lhMusic
2618     >>
2619   >>
2620 }
2621 @end lilypond
2622
2623 We note first that the right hand part in the third bar
2624 requires four voices.  These are the five beamed eighth notes,
2625 the tied C, the half-note D which is merged with the eighth note
2626 D, and the dotted quarter note F-sharp, which is also merged with
2627 the eighth note at the same pitch.  Everything else is in a single
2628 voice, so the easiest way is to introduce these four voices
2629 temporarily at the time they are needed.  If you have forgotten
2630 how to do this, look at @ref{I'm hearing Voices}.  Let us begin
2631 by entering the notes as two variables and setting up the staff
2632 structure in a score block, and see what LilyPond produces by
2633 default:
2634
2635 @lilypond[quote,verbatim,ragged-right]
2636 rhMusic = \relative c'' {
2637   r2 c4. g8 |
2638   bes1~ |
2639   \time 6/4
2640   bes2. r8
2641   % Start polyphonic section of four voices
2642   <<
2643     {c,8 d fis bes a | }
2644   \\
2645     {c,8~ c2 | }
2646   \\
2647     {s8 d2 | }
2648   \\
2649     {s4 fis4. | }
2650   >>
2651   g2.
2652 }
2653
2654 lhMusic = \relative c' {
2655   r2 <c g ees>2 |
2656   <d g, d>1 |
2657   r2. d,,4 r4 r |
2658   r4
2659 }
2660
2661 \score {
2662   \new PianoStaff <<
2663     \new Staff = "RH"  <<
2664       \key g \minor
2665       \rhMusic
2666     >>
2667     \new Staff = "LH" <<
2668       \key g \minor
2669       \clef "bass"
2670       \lhMusic
2671     >>
2672   >>
2673 }
2674 @end lilypond
2675
2676 All the notes are right, but the appearance is far from
2677 satisfactory.  The tie clashes with the change in time signature,
2678 the beaming in the third bar is wrong, the notes are not
2679 merged together, and several notation elements are missing.  
2680 Let's first deal with the easier things.
2681 We can correct the beaming by inserting a beam
2682 manually, and we can easily add the left hand slur and the right 
2683 hand phrasing slur, since these were all covered in the Tutorial.
2684 Doing this gives:
2685
2686 @lilypond[quote,verbatim,ragged-right]
2687 rhMusic = \relative c'' {
2688   r2 c4.\( g8 |
2689   bes1~ |
2690   \time 6/4
2691   bes2. r8
2692   % Start polyphonic section of four voices
2693   <<
2694     {c,8[ d fis bes a] | }
2695   \\
2696     {c,8~ c2 | }
2697   \\
2698     {s8 d2 | }
2699   \\
2700     {s4 fis4. | }
2701   >>
2702   g2.\)
2703 }
2704
2705 lhMusic = \relative c' {
2706   r2 <c g ees>2( |
2707   <d g, d>1) |
2708   r2. d,,4 r4 r |
2709   r4
2710 }
2711
2712 \score {
2713   \new PianoStaff <<
2714     \new Staff = "RH"  <<
2715       \key g \minor
2716       \rhMusic
2717     >>
2718     \new Staff = "LH" <<
2719       \key g \minor
2720       \clef "bass"
2721       \lhMusic
2722     >>
2723   >>
2724 }
2725 @end lilypond
2726
2727 The first bar is now correct.  The second bar contains an arpeggio
2728 and is terminated by a double bar line.  How do we do these, as they
2729 have not been mentioned in this Learning Manual?  This is where
2730 we need to turn to the Notation Reference.  Looking up @q{arpeggio}
2731 and @q{bar line} in the
2732 index quickly shows us that an arpeggio is produced by appending
2733 @code{\arpeggio} to a chord, and a double bar line is produced by
2734 the @code{\bar "||"} command.  That's easily done.  We next need
2735 to correct the collision of the tie with the time signature.  This
2736 is best done by moving the tie upwards.  Moving objects was covered
2737 earlier in @ref{Moving objects}, which says that objects positioned
2738 relative to the staff can be moved by overriding their
2739 @code{staff-position} property, which is specified in half staff
2740 spaces relative to the center line of the staff.  So the following
2741 override placed just before the first tied note would move the tie
2742 up to 3.5 half staff spaces above the center line:
2743
2744 @code{\once \override Tie #'staff-position = #3.5}
2745
2746 This completes bar two, giving:
2747
2748 @lilypond[quote,verbatim,ragged-right]
2749 rhMusic = \relative c'' {
2750   r2 c4.\( g8 |
2751   \once \override Tie #'staff-position = #3.5
2752   bes1~ |
2753   \bar "||"
2754   \time 6/4
2755   bes2. r8
2756   % Start polyphonic section of four voices
2757   <<
2758     {c,8[ d fis bes a] | }
2759   \\
2760     {c,8~ c2 | }
2761   \\
2762     {s8 d2 | }
2763   \\
2764     {s4 fis4. | }
2765   >>
2766   g2.\)
2767 }
2768
2769 lhMusic = \relative c' {
2770   r2 <c g ees>2( |
2771   <d g, d>1)\arpeggio |
2772   r2. d,,4 r4 r |
2773   r4
2774 }
2775
2776 \score {
2777   \new PianoStaff <<
2778     \new Staff = "RH"  <<
2779       \key g \minor
2780       \rhMusic
2781     >>
2782     \new Staff = "LH" <<
2783       \key g \minor
2784       \clef "bass"
2785       \lhMusic
2786     >>
2787   >>
2788 }
2789 @end lilypond
2790
2791 On to bar three and the start of the Moderato section.  The
2792 tutorial showed how to add embolded text with the
2793 @code{\markup} command, so adding @q{Moderato} in bold is easy.
2794 But how do we merge notes in different voices together?  The
2795 index in the Notation Reference does not mention merging,
2796 but a search of the text for @q{merge} quickly leads us to
2797 the overrides for merging differently headed and differently
2798 dotted notes in @ruser{Collision Resolution}.  In our
2799 example we need to merge both types of note for the duration
2800 of the polyphonic section in bar 3, so using the information
2801 in the Notation Reference we add
2802
2803 @example
2804 \override Staff.NoteCollision #'merge-differently-headed = ##t
2805 \override Staff.NoteCollision #'merge-differently-dotted = ##t
2806 @end example
2807
2808 @noindent
2809 to the start of that section and
2810
2811 @example
2812 \revert Staff.NoteCollision #'merge-differently-headed
2813 \revert Staff.NoteCollision #'merge-differently-dotted
2814 @end example
2815
2816 @noindent
2817 to the end, giving:
2818
2819 @lilypond[quote,verbatim,ragged-right]
2820 rhMusic = \relative c'' {
2821   r2 c4.\( g8 |
2822   \once \override Tie #'staff-position = #3.5
2823   bes1~ |
2824   \bar "||"
2825   \time 6/4
2826   bes2.^\markup {\bold "Moderato"} r8
2827   \override Staff.NoteCollision #'merge-differently-headed = ##t
2828   \override Staff.NoteCollision #'merge-differently-dotted = ##t
2829   % Start polyphonic section of four voices
2830   <<
2831     {c,8[ d fis bes a] | }
2832   \\
2833     {c,8~ c2 | }
2834   \\
2835     {s8 d2 | }
2836   \\
2837     {s4 fis4. | }
2838   >>
2839   \revert Staff.NoteCollision #'merge-differently-headed
2840   \revert Staff.NoteCollision #'merge-differently-dotted  
2841   g2.\)
2842 }
2843
2844 lhMusic = \relative c' {
2845   r2 <c g ees>2( |
2846   <d g, d>1)\arpeggio |
2847   r2. d,,4 r4 r |
2848   r4
2849 }
2850
2851 \score {
2852   \new PianoStaff <<
2853     \new Staff = "RH"  <<
2854       \key g \minor
2855       \rhMusic
2856     >>
2857     \new Staff = "LH" <<
2858       \key g \minor
2859       \clef "bass"
2860       \lhMusic
2861     >>
2862   >>
2863 }
2864 @end lilypond
2865
2866 These overrides have merged the two F-sharp notes, but not the two 
2867 on D.  Why not?  The answer is there in the same section in the
2868 Notation Reference -- notes being merged must have stems in 
2869 opposite directions and two notes cannot be merged successfully if 
2870 there is a third note in the same note column.  Here the two D's 
2871 both have upward stems and there is a third note -- the C.  We know
2872 how to change the stem direction using @code{\stemDown}, and
2873 the Notation Reference also says how to move the C -- apply a shift
2874 using one of the @code{\shift} commands.  But which one?
2875 The C is in voice two which has shift off, and the two D's are in
2876 voices one and three, which have shift off and shift on,
2877 respectively.  So we have to shift the C a further level still
2878 using @code{\shiftOnn} to avoid it interfering with the two D's.  
2879 Applying these changes gives:
2880
2881 @lilypond[quote,verbatim,ragged-right]
2882 rhMusic = \relative c'' {
2883   r2 c4.\( g8 |
2884   \once \override Tie #'staff-position = #3.5
2885   bes1~ |
2886   \bar "||"
2887   \time 6/4
2888   bes2.^\markup {\bold "Moderato"} r8
2889   \override Staff.NoteCollision #'merge-differently-headed = ##t
2890   \override Staff.NoteCollision #'merge-differently-dotted = ##t
2891   % Start polyphonic section of four voices
2892   <<
2893     {c,8[ d fis bes a] | }
2894   \\
2895     % Move the c2 out of the main note column so the merge will work
2896     {c,8~ \shiftOnn c2 | }
2897   \\
2898     % Stem on the d2 must be down to permit merging
2899     {s8 \stemDown d2 | }
2900   \\
2901     {s4 fis4. | }
2902   >>
2903   \revert Staff.NoteCollision #'merge-differently-headed
2904   \revert Staff.NoteCollision #'merge-differently-dotted
2905   g2.\)
2906 }
2907
2908 lhMusic = \relative c' {
2909   r2 <c g ees>2( |
2910   <d g, d>1)\arpeggio |
2911   r2. d,,4 r4 r |
2912   r4
2913 }
2914
2915 \score {
2916   \new PianoStaff <<
2917     \new Staff = "RH"  <<
2918       \key g \minor
2919       \rhMusic
2920     >>
2921     \new Staff = "LH" <<
2922       \key g \minor
2923       \clef "bass"
2924       \lhMusic
2925     >>
2926   >>
2927 }
2928 @end lilypond
2929
2930 Nearly there.  Only two problems remain: The downward stem on the
2931 merged D should not be there, and the C would be better positioned
2932 to the right of the D's.  We know how to do both of these from the
2933 earlier tweaks: we make the stem transparent, and move the C with
2934 the @code{force-hshift} property.  Here's the final result:
2935
2936 @lilypond[quote,verbatim,ragged-right]
2937 rhMusic = \relative c'' {
2938   r2
2939   c4.\( g8 |
2940   \once \override Tie #'staff-position = #3.5
2941   bes1~ |
2942   \bar "||"
2943   \time 6/4
2944   bes2.^\markup {\bold "Moderato"} r8
2945   \override Staff.NoteCollision #'merge-differently-headed = ##t
2946   \override Staff.NoteCollision #'merge-differently-dotted = ##t
2947   <<
2948     {c,8[ d fis bes a] | }
2949   \\
2950     % Reposition the c2 to the right of the merged note
2951     {c,8~ \once \override NoteColumn #'force-hshift = #1.0
2952     % Move the c2 out of the main note column so the merge will work
2953     \shiftOnn c2}
2954   \\
2955     % Stem on the d2 must be down to permit merging
2956     {s8 \stemDown \once \override Stem #'transparent = ##t d2}
2957   \\
2958     {s4 fis4.}
2959   >>
2960   \revert Staff.NoteCollision #'merge-differently-headed
2961   \revert Staff.NoteCollision #'merge-differently-dotted
2962   g2.\)
2963 }
2964
2965 lhMusic = \relative c' {
2966   r2 <c g ees>2( |
2967   <d g, d>1)\arpeggio |
2968   r2. d,,4 r4 r |
2969   r4
2970 }
2971
2972 \score {
2973   \new PianoStaff <<
2974     \new Staff = "RH"  <<
2975       \key g \minor
2976       \rhMusic
2977     >>
2978     \new Staff = "LH" <<
2979       \key g \minor
2980       \clef "bass"
2981       \lhMusic
2982     >>
2983   >>
2984 }
2985 @end lilypond
2986
2987
2988 @node Further tweaking
2989 @section Further tweaking
2990
2991 @menu
2992 * Other uses for tweaks::
2993 * Using variables for tweaks::
2994 * Other sources of information::
2995 * Avoiding tweaks with slower processing::
2996 * Advanced tweaks with Scheme::
2997 @end menu
2998
2999 @node Other uses for tweaks
3000 @subsection Other uses for tweaks
3001
3002 @itemize
3003
3004 @cindex transparent property, use of
3005 @cindex objects, making invisible
3006 @cindex removing objects
3007 @cindex objects, removing
3008 @cindex hiding objects
3009 @cindex invisible objects
3010 @cindex tying notes across voices
3011
3012 @item
3013 @subheading Tying notes across voices
3014
3015 The following example demonstrates how to connect notes in
3016 different voices using ties.  Normally, only two notes in the
3017 same voice can be connected with ties.  By using two voices,
3018 with the tied notes in one of them
3019
3020 @lilypond[quote,fragment,relative=2]
3021 << { b8~ b8\noBeam }
3022 \\ { b[ g8] }
3023 >>
3024 @end lilypond
3025
3026 @noindent
3027 and blanking the first up-stem in that voice, the tie appears to
3028 cross voices:
3029
3030 @lilypond[quote,fragment,relative=2,verbatim]
3031 <<
3032   {
3033     \once \override Stem #'transparent = ##t
3034     b8~ b8\noBeam
3035   }
3036 \\
3037   { b[ g8] }
3038 >>
3039 @end lilypond
3040
3041 To make sure that the just-blanked stem doesn't squeeze the tie
3042 too much, we can lengthen the stem by setting the
3043 @code{length} to @code{8},
3044
3045 @lilypond[quote,fragment,relative=2,verbatim]
3046 <<
3047   {
3048     \once \override Stem #'transparent = ##t
3049     \once \override Stem #'length = #8
3050     b8~ b8\noBeam
3051   }
3052 \\
3053   { b[ g8] }
3054 >>
3055 @end lilypond
3056
3057 @item
3058 @subheading Simulating a fermata
3059
3060 @cindex stencil property, use of
3061 @cindex fermata, implementing in MIDI
3062
3063 For outside-staff objects it is usually better to override the
3064 object's @code{stencil} property rather than its @code{transparent}
3065 property when you wish to remove it from the printed output.
3066 Setting the @code{stencil} property of an object to @code{#f} will
3067 remove that object entirely from the printed output.  This means it
3068 has no effect on the placement of other objects placed relative to
3069 it.
3070
3071 For example, if we wished to change the metronome setting in order
3072 to simulate a fermata in the MIDI output we would not want the
3073 metronome markings to appear in the printed output, and we would
3074 not want it to influence the spacing between the two systems or
3075 the spacing of the notes on the staff.  So setting its
3076 @code{stencil} property to @code{#f} would be the best way.
3077 We show here the effect of the two methods:
3078
3079 @lilypond[quote,verbatim,ragged-right]
3080 \score {
3081   \relative c'' {
3082     % Visible tempo marking
3083     \tempo 4=120
3084     a4 a a
3085     \once \override Score.MetronomeMark #'transparent = ##t
3086     % Invisible tempo marking to lengthen fermata note in MIDI
3087     \tempo 4=80
3088     a\fermata
3089     \once \override Score.MetronomeMark #'stencil = ##f
3090     % Invisible tempo marking to restore tempo in MIDI
3091     \tempo 4=120
3092     a a a a
3093   }
3094   \layout { }
3095   \midi { }
3096 }
3097 @end lilypond
3098
3099 @noindent
3100 Both methods remove the metronome mark from the printed output,
3101 and both affect the MIDI timing as required, but the first
3102 (transparent) metronome mark still influences the note spacing
3103 while the second (with no stencil) does not.
3104
3105 @end itemize
3106
3107 @node Using variables for tweaks
3108 @subsection Using variables for tweaks
3109
3110 Override commands are often long and tedious to type, and they
3111 have to be absolutely correct.  If the same overrides are to be
3112 used many times it may be worth defining variables to hold them.
3113 Suppose we wish to emphasize certain words in lyrics by printing
3114 them in bold italics.  The @code{\italic} and @code{\bold}
3115 commands do not work within lyrics so we must instead use the
3116 following @code{\override} and @code{\revert} commands:
3117
3118 @example
3119 @code{\override Lyrics . LyricText #'font-shape = #'italic}
3120 @code{\override Lyrics . LyricText #'font-series = #'bold}
3121
3122 @code{\revert Lyrics . LyricText #'font-shape}
3123 @code{\revert Lyrics . LyricText #'font-series}
3124 @end example
3125
3126 These would be extremely tedious to enter if there were many words
3127 requiring emphasis.  So instead we define these as two variables,
3128 and use them as follows:
3129
3130 @lilypond[quote,verbatim]
3131 emph = {
3132   \override Lyrics . LyricText #'font-shape = #'italic
3133   \override Lyrics . LyricText #'font-series = #'bold
3134 }
3135 norm = {
3136   \revert Lyrics . LyricText #'font-shape
3137   \revert Lyrics . LyricText #'font-series
3138 }
3139
3140 global = { \time 4/4 \partial 4 \key c \major}
3141 SopMusic   = \relative c' { c4 | e4. e8 g4  g  | a a g }
3142 AltoMusic  = \relative c' { c4 | c4. c8 e4  e  | f f e }
3143 TenorMusic = \relative c  { e4 | g4. g8 c4. b8 | a8 b c d e4 }
3144 BassMusic  = \relative c  { c4 | c4. c8 c4  c  | f8 g a b c4 }
3145 VerseOne   = \lyrics { E -- | ter -- nal \emph Fa -- ther, \norm | strong to save, }
3146 VerseTwo   = \lyricmode { O | \emph Christ, \norm whose voice the | wa -- ters heard, }
3147 VerseThree = \lyricmode { O | \emph Ho -- ly Spi -- rit, \norm | who didst brood }
3148 VerseFour  = \lyricmode { O | \emph Tri -- ni -- ty \norm of | love and pow'r }
3149
3150 \score {
3151   \new ChoirStaff <<
3152     \new Staff <<
3153       \clef "treble"
3154       \new Voice = "Sop"  { \voiceOne \global \SopMusic }
3155       \new Voice = "Alto" { \voiceTwo \AltoMusic }
3156       \new Lyrics \lyricsto "Sop" { \VerseOne   }
3157       \new Lyrics \lyricsto "Sop" { \VerseTwo   }
3158       \new Lyrics \lyricsto "Sop" { \VerseThree }
3159       \new Lyrics \lyricsto "Sop" { \VerseFour  }
3160     >>
3161     \new Staff <<
3162       \clef "bass"
3163       \new Voice = "Tenor" { \voiceOne \TenorMusic }
3164       \new Voice = "Bass"  { \voiceTwo \BassMusic  }
3165     >>
3166   >>
3167 }
3168 @end lilypond
3169
3170
3171
3172 @node Other sources of information
3173 @subsection Other sources of information
3174
3175 The Internals Reference documentation contains a lot of information
3176 about LilyPond, but even more information can be gathered by
3177 looking at the internal LilyPond files.  To explore these, first
3178 find the directory appropriate to your system, as follows:
3179
3180 @strong{Linux}
3181
3182 Navigate to
3183 @file{@var{installdir}/lilypond/usr/share/lilypond/current/}
3184
3185 @strong{OSX}
3186
3187 Navigate to
3188 @file{@var{installdir}/LilyPond.app/Contents/Resources/share/lilypond/current/}
3189 by either @code{cd}-ing into this directory from the
3190 Terminal, or control-clicking on the LilyPond application and
3191 selecting @q{Show Package Contents}.
3192
3193 @strong{Windows}
3194
3195 Using Windows Explorer, navigate to
3196 @file{@var{installdir}/LilyPond/usr/share/lilypond/current/}
3197
3198 Within this directory the two interesting subdirectories are
3199
3200 @itemize
3201 @item @file{../ly/ } - contains files in LilyPond format
3202 @item @file{../scm/} - contains files in Scheme format
3203 @end itemize
3204
3205 Let's begin by looking at some files in @file{../ly/}.
3206 Open @file{../ly/property-init.ly} in a text editor.  The one
3207 you normally use for @code{.ly} files will be fine.  This file
3208 contains the definitions of all the standard LilyPond built-in
3209 commands, such as @code{\stemUp} and @code{\slurDotted}.  You will
3210 see that these are nothing more than definitions of variables
3211 containing one or a group of @code{\override} commands.  For
3212 example, @code{/tieDotted} is defined to be:
3213
3214 @example
3215 tieDotted = @{
3216   \override Tie #'dash-period = #0.75
3217   \override Tie #'dash-fraction = #0.1
3218 @}
3219 @end example
3220
3221 If you do not like the default values these built-in commands can
3222 be redefined easily, just like any other variable, at the
3223 head of your input file.
3224
3225 The following are the most useful files to be found in
3226 @file{../ly/}:
3227
3228 @multitable @columnfractions .4 .6
3229 @headitem Filename
3230   @tab Contents
3231 @item @file{../ly/engraver-init.ly}
3232   @tab Definitions of engraver Contexts
3233 @item @file{../ly/paper-defaults.ly}
3234   @tab Specifications of paper-related defaults
3235 @item @file{../ly/performer-init.ly}
3236   @tab Definitions of performer Contexts
3237 @item @file{../ly/property-init.ly}
3238   @tab Definitions of all common built-in commands
3239 @end multitable
3240
3241 Other settings (such as the definitions of markup commands) are
3242 stored as @code{.scm} (Scheme) files.  The Scheme programming
3243 language is used to provide a programmable interface into
3244 LilyPond internal operation.  Further explanation of these files
3245 is currently outside the scope of this manual, as a knowledge of
3246 the Scheme language is required.  Users should be warned that
3247 a substantial amount of technical knowledge or time is required
3248 to understand Scheme and these files (see @ref{Scheme tutorial}).
3249
3250 If you have this knowledge, the Scheme files which may be of
3251 interest are:
3252
3253 @multitable @columnfractions .4 .6
3254 @headitem Filename
3255   @tab Contents
3256 @item @file{../scm/auto-beam.scm}
3257   @tab Sub-beaming defaults
3258 @item @file{../scm/define-grobs.scm}
3259   @tab Default settings for grob properties
3260 @item @file{../scm/define-markup-commands.scm}
3261   @tab Specify all markup commands
3262 @item @file{../scm/midi.scm}
3263   @tab Default settings for MIDI output
3264 @item @file{../scm/output-lib.scm}
3265   @tab Settings that affect appearance of frets, colors,
3266        accidentals, bar lines, etc
3267 @item @file{../scm/parser-clef.scm}
3268   @tab Definitions of supported clefs
3269 @item @file{../scm/script.scm}
3270   @tab Default settings for articulations
3271 @end multitable
3272
3273
3274
3275 @node Avoiding tweaks with slower processing
3276 @subsection Avoiding tweaks with slower processing
3277
3278 LilyPond can perform extra checks while it processes files.  These
3279 checks will take extra time to perform, but fewer manual tweaks
3280 may be required to obtain an acceptable result.  If a text script
3281 or part of the lyrics extends over the margins these checks will
3282 compress that line of the score just enough to fit within the
3283 margins.
3284
3285 To be effective under all circumstances these checks must be enabled
3286 by placing the overrides in a Score @code{\with} block, rather than
3287 in-line in music, as follows:
3288
3289 @example
3290 \new Score \with @{
3291   % Makes sure text scripts and lyrics are within the paper margins
3292   \override PaperColumn #'keep-inside-line = ##t
3293   \override NonMusicalPaperColumn #'keep-inside-line = ##t
3294 @} @{
3295    ..
3296 @}
3297 @end example
3298
3299 @node Advanced tweaks with Scheme
3300 @subsection Advanced tweaks with Scheme
3301
3302 Although many things are possible with the @code{\override} and
3303 @code{\tweak} commands, an even more powerful way of modifying
3304 the action of LilyPond is available through a programmable
3305 interface to the LilyPond internal operation.  Code written in
3306 the Scheme programming language can be incorporated directly in
3307 the internal operation of LilyPond.  Of course, at least a basic
3308 knowledge of programming in Scheme is required to do this, and an
3309 introduction is provided in the @ref{Scheme tutorial}.
3310
3311 As an illustration of one of the many possibilities, instead of
3312 setting a property to a constant it can be set to a Scheme
3313 procedure which is then called whenever that property is accessed
3314 by LilyPond.  The property can then be set dynamically to a value
3315 determined by the procedure at the time it is called.  In this
3316 example we color the note head in accordance with its position on
3317 the staff.
3318
3319 @lilypond[quote,verbatim,ragged-right]
3320 #(define (color-notehead grob)
3321   "Color the notehead according to its position on the staff."
3322   (let ((mod-position (modulo (ly:grob-property grob 'staff-position) 7)))
3323     (case mod-position
3324       ;;   Return rainbow colors
3325       ((1) (x11-color 'red    ))  ; for C
3326       ((2) (x11-color 'orange ))  ; for D
3327       ((3) (x11-color 'yellow ))  ; for E
3328       ((4) (x11-color 'green  ))  ; for F
3329       ((5) (x11-color 'blue   ))  ; for G
3330       ((6) (x11-color 'purple ))  ; for A
3331       ((0) (x11-color 'violet ))  ; for B
3332     )
3333   )
3334 )
3335
3336 \relative c' {
3337   % Arrange to obtain color from color-notehead procedure
3338   \override NoteHead #'color = #color-notehead
3339   c2 c' | 
3340   b4 g8 a b4 c | 
3341   c,2 a' | 
3342   g1 |
3343 }
3344 \addlyrics {
3345   Some -- where o -- ver the Rain -- bow way up high,
3346 }
3347 @end lilypond
3348
3349 Further examples showing the use of these programmable interfaces
3350 can be found in @ref{Tweaking with Scheme}.
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367