@c -*- coding: utf-8; mode: texinfo; -*-

@ignore
    Translation of GIT committish: FILL-IN-HEAD-COMMITTISH

    When revising a translation, copy the HEAD committish of the
    version that you are working on.  For details, see the Contributors'
    Guide, node Updating translation committishes..
@end ignore

@c \version "2.19.22"

@node Tweaking output
@chapter Tweaking output

This chapter discusses how to modify output.  LilyPond is extremely
configurable; virtually every fragment of output may be changed.


@node Tweaking basics
@section Tweaking basics

@q{Tweaking} is a LilyPond term for the various methods available
to the user for modifying the actions taken during interpretation
of the input file and modifying the appearance of the printed
output.  Some tweaks are very easy to use; others are more
complex.  But taken together the methods available for tweaking
permit almost any desired appearance of the printed music to be
achieved.

In this section we cover the basic concepts required to understand
tweaking.  Later we give a variety of ready-made commands which can
simply be copied to obtain the same effect in your own scores, and
at the same time we show how these commands may be constructed so
that you may learn how to develop your own tweaks.

Before starting on this chapter you may wish to review the section
@ref{Contexts and engravers}, as contexts, engravers, and the
properties contained within them are fundamental to understanding
and constructing tweaks.


@node Objects and interfaces
@subsection Objects and interfaces

@cindex object
@cindex grob
@cindex spanner
@cindex interface
@cindex property, object
@cindex object, properties
@cindex layout object
@cindex object, layout
@cindex interface

Tweaking involves modifying the internal operation and structures
of the LilyPond program, so we must first introduce some terms
which are used to describe those internal operations and
structures.

The term @dfn{Object} is a generic term used to refer to the
multitude of internal structures built by LilyPond during the
processing of an input file.  So when a command like @code{\new
Staff} is encountered a new object of type @code{Staff} is
constructed.  That @code{Staff} object then holds all the
properties associated with that particular staff, for example, its
name and its key signature, together with details of the engravers
which have been assigned to operate within that staff's context.
Similarly, there are objects to hold the properties of all other
contexts, such as @code{Voice} objects, @code{Score} objects,
@code{Lyrics} objects, as well as objects to represent all
notational elements such as bar lines,
note heads, ties, dynamics, etc.  Every object has its own set of
property values.

Some types of object are given special names.  Objects that represent
items of notation on the printed output such as note heads, stems,
slurs, ties, fingering, clefs, etc are called @q{Layout objects},
often known as @q{Graphical Objects}, or @dfn{grobs} for short.  These
are still objects in the generic sense above, and so they too all have
properties associated with them, such as their position, size, color,
etc.

Some layout objects are still more specialized.  Phrasing slurs,
crescendo hairpins, ottava marks, and many other grobs are not
localized in a single place -- they have a starting point, an
ending point, and maybe other properties concerned with their
shape.  Objects with an extended shape like these are called
@dfn{spanners}.

Spanners cannot be tweaked after their creation.  This includes both
@code{StaffSymbol} and @code{LedgerLineSpanner}, which continue
throughout the score, except if they are terminated by the
@code{\stopStaff} command and then recreated using @code{\startStaff}
command.

What is more, there are @q{abstract} grobs which do not print
anything of their own, but rather collect, position and manage
other grobs.  Common examples for this are
@code{DynamicLineSpanner}, @code{BreakAlignment},
@code{NoteColumn}, @code{VerticalAxisGroup},
or @code{NonMusicalPaperColumn}.  We will see how some of
these are used later.

It remains to explain what @dfn{interfaces} are.  Many objects, even
though they are quite different, share common features which need to
be processed in the same way.  For example, all grobs have a color, a
size, a position, etc, and all these properties are processed in the
same way during LilyPond's interpretation of the input file.  To
simplify these internal operations these common actions and properties
are grouped together in an object called a @code{grob-interface}.
There are many other groupings of common properties like this, each
one given a name ending in @code{interface}.  In total there are over
100 such interfaces.  We shall see later why this is of interest and
use to the user.

These, then, are the main terms relating to objects which we
shall use in this chapter.


@node Naming conventions of objects and properties
@subsection Naming conventions of objects and properties

@cindex naming convention, for objects
@cindex naming convention, for properties
@cindex object, naming conventions
@cindex property, naming conventions

We met some object naming conventions previously, in
@ref{Contexts and engravers}.  Here for reference is a list
of the most common object and property types together with
the conventions for naming them and a couple of examples of
some real names.  We have used @q{A} to stand for any capitalized
alphabetic character and @q{aaa} to stand for any number of
lower-case alphabetic characters.  Other characters are used
verbatim.

@indentedblock
@multitable {Layout Object Properties} {@code{Aaaa} or @code{AaaaAaaaAaaa}} {@code{break-aligned-interface}}
@headitem Object/property type
  @tab Naming convention
  @tab Examples
@item Contexts
  @tab @code{Aaaa} or @code{AaaaAaaaAaaa}
  @tab @code{Staff}, @code{GrandStaff}
@item Layout Objects
  @tab @code{Aaaa} or @code{AaaaAaaaAaaa}
  @tab @code{Slur}, @code{NoteHead}
@item Engravers
  @tab @code{Aaaa_aaa_engraver}
  @tab @code{Clef_engraver},@* @code{Note_heads_engraver}
@item Interfaces
  @tab @code{aaa-aaa-interface}
  @tab @code{grob-interface},@* @code{break-aligned-interface}
@item Context Properties
  @tab @code{aaa} or @code{aaaAaaaAaaa}
  @tab @code{alignAboveContext}, @code{skipBars}
@item Layout Object Properties
  @tab @code{aaa} or @code{aaa-aaa-aaa}
  @tab @code{direction},@* @code{beam-thickness}
@end multitable
@end indentedblock

As we shall see shortly, the properties of different types of object are
modified by different commands, so it is useful to be able to recognize
the types of objects and properties from their names.

@morerefs
Notation Reference:
@rnotation{Naming conventions},
@rnotation{Modifying properties}.
@endmorerefs


@node Tweaking methods
@subsection Tweaking methods

@cindex tweaking methods

This subsection discusses the most important tweaking commands.


@node The override command
@unnumberedsubsubsec The @code{\override} command

@funindex \override

We have already met the commands @code{\set} and @code{\with}, used to
change the properties of @strong{contexts} and to remove and add
@strong{engravers}, in @ref{Modifying context properties}, and
@ref{Adding and removing engravers}.  We must now introduce some more
important commands.

The command to change the properties of @strong{layout objects} is
@code{\override}.  Because this command has to modify
internal properties deep within LilyPond its syntax is not
as simple as the commands you have used so far.  It needs to
know precisely which property of which object in which context
has to be modified, and what its new value is to be.  Let's see
how this is done.

The general syntax of this command is:

@example
\override @var{Context}.@var{LayoutObject}.@var{layout-property} = @var{value}
@end example

@noindent
This sets the property with the name @var{layout-property} of the
layout object with the name @var{LayoutObject}, which is a member of
the @var{Context} context, to the value @var{value}.

The @var{Context} may be omitted (and usually is) when the
required context is unambiguously implied and is one of lowest
level contexts, i.e., @code{Voice}, @code{ChordNames} or
@code{Lyrics}, and we shall omit it in many of the following
examples.  We shall see later when it must be specified.

Later sections deal comprehensively with properties and their
values, @pxref{Types of properties}.  But in this section we shall
use just a few simple properties and values which are easily
understood in order to illustrate the format and use of these
commands.

LilyPond's primary expressions are musical items like notes and
durations, as well as strings and markups.  More specific expressions
like symbols and lists are processed in @q{Scheme mode}, which is
invoked by prefixing the value with @samp{#}.  A special case are
numbers, which must be prefixed with @samp{#} only if used as
arguments to a markup function (i.e., within
@code{\markup}).@footnote{Note, however, that for LilyPond a
number @emph{must} start with a digit or a minus sign followed by
a digit if not in Scheme mode.  In other words, @samp{0.2} or
@samp{-0.2} are valid numbers, while @samp{.2} or @samp{-.2} are
not.  If really necessary you can write @samp{#.2} or @samp{#-.2}
instead -- Scheme supports a much broader range of number
representations.}

For more information about Scheme mode, see @rextend{LilyPond
Scheme syntax}.

@code{\override} is the most common command used in tweaking, and
most of the rest of this chapter will be directed to presenting
examples of how it is used.  Here is a simple example to change the
color of the note head:

@cindex @code{color} property, example
@cindex @code{NoteHead}, example of overriding

@lilypond[quote,ragged-right,verbatim]
\relative {
  c'4 d
  \override NoteHead.color = "red"
  e4 f |
  \override NoteHead.color = "green"
  g4 a b c |
}
@end lilypond


@node The revert command
@unnumberedsubsubsec The @code{\revert} command

@funindex \revert

Once overridden, the property retains its new value until it is
overridden again or a @code{\revert} command is encountered.
The @code{\revert} command has the following syntax and causes
the value of the property to revert to its original default
value; note, not its previous value if several @code{\override}
commands have been issued.@footnote{The behaviour of
@code{\revert} can be changed by using @code{\temporary
\override}; see @rextend{Intermediate substitution functions}.}

@example
\revert @var{Context}.@var{LayoutObject}.@var{layout-property}
@end example

Again, just like @var{Context} in the @code{\override} command,
@var{Context} is often not needed.  It will be omitted
in many of the following examples.  Here we revert the color
of the note head to the default value for the final two notes:

@cindex @code{color} property, example
@cindex @code{NoteHead}, example of overriding

@lilypond[quote,ragged-right,verbatim]
\relative {
  c'4 d
  \override NoteHead.color = "red"
  e4 f |
  \override NoteHead.color = "green"
  g4 a
  \revert NoteHead.color
  b4 c |
}
@end lilypond


@node The once prefix
@unnumberedsubsubsec The @code{\once} prefix

@funindex \once

@code{\override}, @code{\revert}, @code{\set}, and @code{\unset}
commands may be prefixed with @code{\once}.  This causes such a
command to be effective only during the current musical
moment before the property reverts back to its previous value (this can
be different from the default if another @code{\override} is still in
effect).  Using the same example, we can change the color of a single
note like this:

@cindex @code{color} property, example
@cindex @code{NoteHead}, example of overriding

@lilypond[quote,verbatim]
\relative {
  c'4 d
  \override NoteHead.color = "red"
  e4 f |
  \once \override NoteHead.color = "green"
  g4 a
  \once \revert NoteHead.color
  b c |
  \revert NoteHead.color
  f2 c |
}
@end lilypond

The @code{\once} prefix may also be used in front of many
predefined commands to limit their effect to one musical moment:

@lilypond[quote,verbatim]
\relative {
  c'4( d)
  \once \slurDashed
  e4( f) |
  g4( a)
  \once \hideNotes
  b( c) |
}
@end lilypond


@node The tweak and offset commands
@unnumberedsubsubsec The @code{\tweak} and @code{\offset} commands

@funindex \tweak
@funindex \offset

The final tweaking commands of LilyPond are @code{\tweak} and @code{\offset}.  They
should be used when several objects occur at the same musical moment,
but you only want to change the properties of selected ones, such as a
single note within a chord.  Using @code{\override} would affect all the
notes within a chord, whereas @code{\tweak} affects just the following
item in the input stream.

Note that @code{\offset} can be used as an replacement for
@code{\override}, too; it also has more syntax forms.  See
@rnotation{The offset command}, for all the details.

Here's an example.  Suppose we wish to change the size of the
middle note head (the E) in a C major chord.  Let's first see what
@code{\once \override} would do:

@cindex @code{font-size} property, example
@cindex @code{NoteHead}, example of overriding

@lilypond[quote,ragged-right,verbatim]
\relative {
  <c' e g>4
  \once \override NoteHead.font-size = -3
  <c e g>4
  <c e g>4
}
@end lilypond

We see the override affects @emph{all} the note heads in the chord.
This is because all the notes of a chord occur at the same
@emph{musical moment}, and the action of @code{\once} is to
apply the override to all layout objects of the type specified
which occur at the same musical moment as the @code{\override}
command itself.

The @code{\tweak} and @code{\offset} commands operate in a different way.  They act on
the immediately following item in the input stream.  In their simplest
forms, they are effective only on objects which are created directly
from the following item, essentially note heads and articulations.

So to return to our example, the size of the middle note of
a chord would be changed in this way:

@cindex @code{font-size} property, example
@cindex @code{\tweak}, example

@lilypond[quote,ragged-right,verbatim]
\relative {
  <c' e g>4
  <c \tweak font-size -3 e g>4
}
@end lilypond

Let's now try to change the vertical position of the
@code{\breathe} symbol.  By default, LilyPond computes a vertical
distance from the middle line automatically.  If we would like to
move it up by two units, say, @code{\offset} is the right command.
With @code{\tweak} it would be necessary to find the correct
position manually by trial and error.

@cindex @code{\offset}, example

@lilypond[quote,ragged-right,verbatim]
\relative c'' {
  c4
  \breathe
  c4
  \offset Y-offset 2 \breathe
  c2
  \tweak Y-offset 4 \breathe
}
@end lilypond

Obviously, @code{\offset} only works for values that actually have
a numeric value.  There are more restrictions to that what can be
changed; again, please refer to @rnotation{The offset command},
for more details.

Note that the syntax of @code{\tweak} and @code{\offset} is different from that of the
@code{\override} command.  The context should not be specified; in
fact, it would generate an error to do so.  Both context and layout
object are implied by the following item in the input stream.  Note
also that an equals sign should not be present.  So the simple form
of the @code{\tweak} and @code{\offset} commands is

@example
\tweak @var{layout-property} @var{value}
\offset @var{layout-property} @var{value}
@end example

@code{\tweak} and @code{\offset} can also be used to modify just one in
a series of articulations, as shown here:

@cindex @code{color} property, example
@cindex @code{\tweak}, example

@lilypond[quote,fragment,ragged-right,verbatim]
a'4^"Black"
  -\tweak color "red" ^"Red"
  -\tweak color "green" _"Green"
@end lilypond

@noindent
Both @code{\tweak} and @code{\offset} must be preceded by an articulation
mark since the tweaked expression needs to be applied as an articulation
itself.  In case of multiple direction overrides (@samp{^} or @samp{_}),
the leftmost override wins since it is applied last.

@cindex @code{\tweak}, accidental
@cindex @code{\tweak}, specific layout object

Objects such as stems and accidentals are created later, and not
directly from the following event.  It is still possible to use
@code{\tweak} or @code{\offset} on such indirectly created objects by explicitly naming
the layout object, provided that LilyPond can trace its origin back to
the original event:

@lilypond[quote,fragment,ragged-right,verbatim]
<\tweak Accidental.color "red"   cis''4
 \tweak Accidental.color "green" es''
 g''>
@end lilypond

This long form of the @code{\tweak} and @code{\offset} commands can be described as

@example
\tweak @var{LayoutObject}.@var{layout-property} @var{value}
\offset @var{LayoutObject}.@var{layout-property} @var{value}
@end example

@cindex tuplets, nested
@cindex triplets, nested
@cindex bracket, tuplet
@cindex bracket, triplet
@cindex tuplet bracket
@cindex triplet bracket

@cindex @code{TupletBracket}

The @code{\tweak} command must also be used to change the
appearance of one of a set of nested tuplets which begin at the
same musical moment.  In the following example, the long tuplet
bracket and the first of the three short brackets begin at the
same musical moment, so any @code{\override} command would apply
to both of them.  In the example, @code{\tweak} is used to
distinguish between them.  The first @code{\tweak} command
specifies that the long tuplet bracket is to be placed above the
notes and the second one specifies that the tuplet number is to be
printed in red on the first short tuplet bracket.

@cindex @code{\tweak}, example
@cindex @code{direction} property, example
@cindex @code{color} property, example

@lilypond[quote,ragged-right,verbatim]
\relative c'' {
  \tweak direction #up
  \tuplet 3/4 {
    \tweak color "red"
    \tuplet 3/2 { c8[ c c] }
    \tuplet 3/2 { c8[ c c] }
    \tuplet 3/2 { c8[ c c] }
  }
}
@end lilypond

If nested tuplets do not begin at the same moment, their
appearance may be modified in the usual way with
@code{\override} commands:

@cindex @code{text} property, example
@cindex @code{tuplet-number} function, example
@cindex @code{transparent} property, example
@cindex @code{TupletNumber}, example of overriding

@lilypond[quote,ragged-right,verbatim]
\relative {
  \tuplet 3/2 { c'8[ c c] }
  \once \override TupletNumber.text =
    #tuplet-number::calc-fraction-text
  \tuplet 3/2 {
    c8[ c]
    c8[ c]
    \once \override TupletNumber.transparent = ##t
    \tuplet 3/2 { c8[ c c] }
    \tuplet 3/2 { c8[ c c] }
  }
}
@end lilypond


@morerefs
Notation Reference:
@rnotation{Tweak and single},
@rnotation{The offset command}
@endmorerefs


@node The single prefix
@unnumberedsubsubsec The @code{\single} prefix

@funindex \single
@cindex tweak, generated from @code{\override}

Suppose we wanted to emphasize particular note heads by coloring them
red and increasing their size, and to make it easy suppose also we
have defined a function to do this:

@lilypond[quote,verbatim]
emphNoteHead = {
  \override NoteHead.color = "red"
  \override NoteHead.font-size = 2
}
\relative {
  c''4 a \once \emphNoteHead f d |
}
@end lilypond

The @code{\once} prefix works fine to emphasize single notes or
complete chords, but it cannot be used to emphasize a single note
@emph{within} a chord.  Earlier we have seen how @code{\tweak} can
be used to do this, see
@ref{The tweak and offset commands}.  But
@code{\tweak} cannot be used with a function; that's where
@code{\single} comes in:

@lilypond[quote,verbatim]
emphNoteHead = {
  \override NoteHead.color = "red"
  \override NoteHead.font-size = 2
}
\relative {
   <c'' a \single \emphNoteHead f d>4
}
@end lilypond

In summary, @code{\single} converts overrides into tweaks so when
there are several objects at the same point in musical time (like
note heads in a chord), @code{\single} will only affect a single one,
the one generated by the immediately following music expression, in
contrast to @code{\once} which will affect all of those objects.

By using @code{\single} in this way any shorthand function
containing just overrides may be applied to individual notes in a
chord.  However, @code{\single} does not convert @code{\revert},
@code{\set} or @code{\unset} into tweaks.

@morerefs
Learning Manual:
@ref{The tweak and offset commands},
@ref{Using variables for layout adjustments}.
@endmorerefs


@node The Internals Reference manual
@section The Internals Reference manual

@cindex Internals Reference

The @dfn{Internals Reference} manual (IR) is the ultimate answer
to LilyPond's configurability.  To understand how it can be used,
however, you need a bunch of information that has not yet been
presented, and which follows in the next subsections.


@node Properties of layout objects
@subsection Properties of layout objects

@cindex property, of layout object
@cindex property, of grob
@cindex grob, properties of
@cindex layout object, properties of

Suppose you have a slur in a score which, to your mind,
appears too thin and you'd like to draw it a little heavier.
How do you go about doing this?  You know from the statements
earlier about the flexibility of LilyPond that such a thing
should be possible, and you would probably guess that an
@code{\override} command would be needed.  But is there a
heaviness property for a slur, and if there is, how might it
be modified?  This is where the Internals Reference manual
comes in.  It contains all the information you might need to
construct this and all other @code{\override} commands.

Before we look at the Internals Reference a word of warning.
This is a @strong{reference} document, which means there is
little or no explanation contained within it: its purpose is
to present information precisely and concisely.  This
means it might look daunting at first sight.  Don't worry!
The guidance and explanation presented here will enable you
to extract the information from the Internals Reference for
yourself with just a little practice.

@cindex @code{\override}, example
@cindex Internals Reference, example of using
@cindex @code{\addlyrics}, example

Let's use a concrete example with a simple fragment of real
music:

@c Mozart, Die Zauberflöte Nr.7 Duett

@lilypond[quote,verbatim]
{
  \key es \major
  \time 6/8
  \relative {
    r4 bes'8 bes[( g]) g |
    g8[( es]) es d[( f]) as |
    as8 g
  }
  \addlyrics {
    The man who | feels love's sweet e -- | mo -- tion
  }
}
@end lilypond

Suppose now that we decide we would like the slurs to be a
little heavier.  Is this possible?  The slur is certainly a
layout object, so the question is, @q{Is there a property
belonging to a slur which controls the heaviness?}  To answer
this we must check the Internals Reference, or IR for short.

The IR for the version of LilyPond you are using may be found
on the LilyPond website at @uref{https://lilypond.org}.  Go to the
documentation page and click on the Internals Reference link.
For learning purposes you should use the standard HTML version,
not the @q{one big page} or the PDF.  For the next few
paragraphs to make sense you will need to actually do this
as you read.

Under the heading @strong{Top} you will see five links.  Select
the link to the @emph{Backend}, which is where information about
layout objects is to be found.  There, under the heading
@strong{Backend}, select the link to @emph{All layout objects}.
The page that appears lists all the layout objects used in your
version of LilyPond, in alphabetic order.  Select the link to
@code{Slur}, and the properties of slurs are listed.

An alternative way of finding this page is from the Notation
Reference.  On one of the pages that deals with slurs you may find a
link to the Internals Reference.  This link will take you directly to
this page, but if you have an idea about the name of the layout object
to be tweaked, it is easier to go straight to the IR and search there.

This @code{Slur} page in the IR tells us first that @code{Slur} objects are created
by the @code{Slur_engraver}.  Then it lists the standard settings.  Browse
through them looking for a property that might control the heaviness of
slurs, and you should find

@example
thickness (number)
    1.2
    [...] For slurs and ties, this is the distance between the two
    arcs of the curve's outline at its thickest point, [...]  This
    property is expressed as a multiple of the current staff-line
    thickness [...]
@end example

This looks a good bet to change the heaviness.  It tells us that
the value of @code{thickness} is a simple @emph{number},
that the default value is 1.2, and that this value depends on the
thickness of staff lines -- which means more or less that smaller
staves have thinner slurs.

As we said earlier, there are few to no explanations in the IR,
but we already have enough information to try changing the
slur thickness.  We see that the name of the layout object
is @code{Slur}, that the name of the property to change is
@code{thickness} and that the new value should be a number
somewhat larger than 1.2 if we are to make slurs thicker.

We can now construct the @code{\override} command by simply
substituting the values we have found for the names, omitting
the context.  Let's use a very large value for the thickness
at first, so we can be sure the command is working.  We get:

@example
\override Slur.thickness = 5.0
@end example

The final question is, @q{Where should this command be
placed?}  While you are unsure and learning, the best
answer is, @q{Within the music, before the first slur and
close to it.}  Let's do that:

@cindex @code{Slur}, example of overriding
@cindex @code{thickness} property, example

@lilypond[quote,verbatim]
{
  \key es \major
  \time 6/8
  \relative {
    % Increase thickness of all following slurs from 1.2 to 5.0
    \override Slur.thickness = 5.0
    r4 bes'8 bes[( g]) g |
    g8[( es]) es d[( f]) as |
    as8 g
  }
  \addlyrics {
    The man who | feels love's sweet e -- | mo -- tion
  }
}
@end lilypond

@noindent
and we see that the slur is indeed heavier.

So this is the basic way of constructing @code{\override}
commands.  There are a few more complications that we
shall meet in later sections, but you now know all the
essentials required to make up your own -- but you will
still need some practice.  This is provided in the examples
that follow.

@subsubsubheading Finding the context

@cindex context, finding
@cindex context, identifying correct

But first, what if we had needed to specify the context?
What should it be?  We could guess that slurs are in
the @code{Voice} context, as they are clearly closely associated
with individual lines of music, but can we be sure?  To
find out, go back to the top of the IR page describing the
@code{Slur} grob, where it says @q{@code{Slur} objects are created
by the following engraver(s): @code{Slur_engraver}.}
So slurs will be created in whichever context
the @code{Slur_engraver} is in.  Follow the link to the
@code{Slur_engraver} page.  At the very bottom it tells
us that @code{Slur_engraver} is part of ten voice contexts,
including the standard voice context, @code{Voice}, so our
guess was correct.  And because @code{Voice} is one of the
lowest-level contexts, which is implied unambiguously by
the fact that we are entering notes, we can omit it in this
location.

@subsubsubheading Overriding once only

@cindex override, only once
@cindex @code{\once} @code{\override}

@funindex \once

As you can see, @emph{all} the slurs are thicker in the final example
above.  But what if we wanted just the first slur to be thicker?  This
is achieved with the @code{\once} command.  Placed immediately before
the @code{\override} command it causes it to change only the slur
which begins on the @strong{immediately following} note.  If the
immediately following note does not begin a slur the command has no
effect at all -- it is not remembered until a slur is encountered, it
is simply discarded.  So the command with @code{\once} must be
repositioned as follows:

@cindex @code{Slur}, example of overriding
@cindex @code{thickness} property, example

@lilypond[quote,verbatim]
{
  \key es \major
  \time 6/8
  \relative {
    r4 bes'8
    % Increase thickness of immediately following slur only
    \once \override Slur.thickness = 5.0
    bes8[( g]) g |
    g8[( es]) es d[( f]) as |
    as8 g
  }
  \addlyrics {
    The man who | feels love's sweet e -- | mo -- tion
  }
}
@end lilypond

@noindent
Now only the first slur is made heavier.

The @code{\once} command can also be used before the @code{\set}
command.

An alternative to @code{\once \override} is to use the
@code{\tweak} command; you get the same result if you replace

@example
\once \override Slur.thickness = 5.0
bes8[(
@end example

@noindent
in the previous example with

@example
bes8[\tweak thickness 5.0 (
@end example

@subsubsubheading Reverting

@cindex default properties, reverting to
@funindex \revert

Finally, what if we wanted just the first two slurs to be
heavier?  Well, we could use two commands, each preceded by
@code{\once} placed immediately before each of the notes where
the slurs begin:

@cindex @code{Slur}, example of overriding
@cindex @code{thickness} property, example

@lilypond[quote,verbatim]
{
  \key es \major
  \time 6/8
  \relative {
    r4 bes'8
    % Increase thickness of immediately following slur only
    \once \override Slur.thickness = 5.0
    bes[( g]) g |
    % Increase thickness of immediately following slur only
    \once \override Slur.thickness = 5.0
    g8[( es]) es d[( f]) as |
    as8 g
  }
  \addlyrics {
    The man who | feels love's sweet e -- | mo -- tion
  }
}
@end lilypond

@noindent
or we could omit the @code{\once} command and use the @code{\revert}
command to return the @code{thickness} property to its default value
after the second slur:

@cindex @code{Slur}, example of overriding
@cindex @code{thickness} property, example

@lilypond[quote,verbatim]
{
  \key es \major
  \time 6/8
  \relative {
    r4 bes'8
    % Increase thickness of all following slurs from 1.2 to 5.0
    \override Slur.thickness = 5.0
    bes[( g]) g |
    g8[( es]) es
    % Revert thickness of all following slurs to default of 1.2
    \revert Slur.thickness
    d8[( f]) as |
    as8 g
  }
  \addlyrics {
    The man who | feels love's sweet e -- | mo -- tion
  }
}
@end lilypond

@noindent
The @code{\revert} command can be used to return any property
changed with @code{\override} back to its default value.
You may use whichever method best suits what you want to do.

That concludes our introduction to the IR, and the basic
method of tweaking.  Several examples follow in the later
sections of this chapter, partly to introduce you to some of the
additional features of the IR, and partly to give you more
practice in extracting information from it.  These examples will
contain progressively fewer words of guidance and explanation.


@node Properties found in interfaces
@subsection Properties found in interfaces

@cindex interface
@cindex interface properties
@cindex property, in interfaces

Suppose now that we wish to print the lyrics in italics.  What form of
@code{\override} command do we need to do this?  We first look in the
IR page listing @q{All layout objects}, as before, and look for an
object that might control lyrics.  We find @code{LyricText}, which
looks right.  Clicking on this shows the settable properties for lyric
text.  These include the @code{font-series} and @code{font-size}, but
nothing that might give an italic shape.  This is because the shape
property is one that is common to all font objects, so, rather than
including it in every layout object, it is grouped together with other
similar common properties and placed in an @strong{Interface}, the
@code{font-interface}.

So now we need to learn how to find the properties of interfaces,
and to discover what objects use these interface properties.

Look again at the IR page that describes @code{LyricText}.  At the bottom of
the page is a list of clickable interfaces that @code{LyricText} supports.
The list has several items, including @code{font-interface}.  Clicking
on this brings up the properties associated with this interface, which
are also properties of all the objects which support it, including
@code{LyricText}.

Now we see all the user-settable properties which control fonts,
including @q{@code{font-shape} (symbol)}.  The value in
parentheses gives the variable type expected by the property; here
it is a (Scheme) symbol.  The IR also documents the possible
values, which are @code{upright}, @code{italics}, or @code{slanted}.

You will notice that @code{font-series} and @code{font-size} are also
listed there.  This immediately raises the question: Why are the
common font properties @code{font-series} and @code{font-size} listed
under @code{LyricText} as well as under the interface
@code{font-interface} but @code{font-shape} is not?  The answer is
that @code{font-series} and @code{font-size} are changed from their
global default values when a @code{LyricText} object is created, but
@code{font-shape} is not.  The entries in @code{LyricText} then tell
you the values for those two properties which apply to
@code{LyricText}.  Other objects which support @code{font-interface}
will set these properties differently when they are created.

Let's see if we can now construct the @code{\override} command
to change the lyrics to italics.  The object is @code{LyricText},
the property is @code{font-shape} and the value is
@code{italic}.  As before, we'll omit the context.

As an aside, although it is an important one, note that some
properties take values that are symbols, like @code{italic}, and
must be preceded by an apostrophe, @samp{'}.  Symbols are then
read internally by LilyPond.  Note the distinction from arbitrary
text strings, which would appear as @code{"a text string"}; for
more details about symbols and strings, see @rextend{Scheme tutorial}.

So we see that the @code{\override} command needed to print the lyrics
in italics is:

@example
\override LyricText.font-shape = #'italic
@end example

@noindent
This should be placed just in front of the lyrics we wish to affect,
like so:

@cindex @code{font-shape} property, example
@cindex italic, example
@cindex @code{LyricText}, example of overriding
@cindex @code{\addlyrics}, example

@lilypond[quote,ragged-right,verbatim]
{
  \key es \major
  \time 6/8
  \relative {
    r4 bes'8 bes[( g]) g |
    g8[( es]) es d[( f]) as |
    as8 g
  }
  \addlyrics {
    \override LyricText.font-shape = #'italic
    The man who | feels love's sweet e -- | mo -- tion
  }
}
@end lilypond

@noindent
and the lyrics are all printed in italics.

@warning{In lyrics, always leave whitespace between the final
syllable and the terminating brace.}


@morerefs
Extending: @rextend{Scheme tutorial}.
@endmorerefs


@node Types of properties
@subsection Types of properties

@cindex property, types
@cindex @code{#t} (true)
@cindex @code{#f} (false)
@cindex true (@code{#t})
@cindex false (@code{#f})
@cindex Scheme expressions (@code{#})
@cindex @code{#}, Scheme expressions

So far we have seen two types of property: @q{number} and
@q{symbol}.  To be valid, the value given to a property
must be of the correct type and obey the rules for that type.
The type of property is always shown in parentheses after the
property name in the IR.  Here is a list of the types you may
need, together with the rules for that type, and some examples.
If you use a Scheme expression you must add a hash symbol, @samp{#},
to the front of these values when they are entered in the
@code{\override} command, even if the value itself already
starts with @samp{#}.  We only give examples for constants
here: if you want to compute a value using Scheme, see
@rextend{Calculations in Scheme}.

@indentedblock
@raggedright
@multitable {@b{Property Type}} {either @q{true} or @q{false}, represented} {@code{\markup @{ \italic "cresc." @}},}
@headitem Property type
  @tab Rules
  @tab Input examples
@item Boolean
  @tab either @q{true} or @q{false}, represented by @code{#t} or
       @code{#f}
  @tab @code{##t}, @code{##f}
@item dimension@*(in staff spaces)
  @tab a decimal number (in units of staff spaces)
  @tab @code{2.5}, @code{0.34}
@item direction
  @tab a valid direction constant or its numerical equivalent
       (value@tie{}@code{0} or @code{CENTER} indicate a neutral
       direction)
  @tab @code{#LEFT}, @code{#CENTER}, @code{#UP}, @code{1}, @code{-1}
@item integer
  @tab a whole number
  @tab @code{3}, @code{-1}
@item list
  @tab a sequence of constants or symbols separated by spaces, enclosed
       in parentheses and preceded by an apostrophe (quote mark)
  @tab @code{#'(left-edge staff-bar)},@*
       @code{#'(1)}, @code{#'()},@*
       @code{#'(1.0 0.25 0.5)}
@item markup
  @tab any valid markup
  @tab @code{\markup @{ \italic "cresc." @}},@*
       @code{"bagpipe"}
@item moment
  @tab a musical length; this type is distinct from a note
       duration
  @tab @code{\musicLength 4.},@*
       @code{\musicLength @{ 2 2. @}}
@item number
  @tab any positive or negative, possibly decimal, value
  @tab @code{3}, @code{-2.45}
@item pair@*(of numbers)
  @tab two numbers separated by the sequence @q{whitespace},
       @samp{.}, @q{whitespace}, enclosed in parentheses, and
       preceded by an apostrophe
  @tab @code{#'(2 . 3.5)},@*
       @code{#'(0.1 . -3.2)}
@item symbol
  @tab any of the set of permitted symbols for that property,
       preceded by an apostrophe
  @tab @code{#'italic}, @code{#'inside}
@item unknown
  @tab a procedure, or @code{#f} to cause no action
  @tab @code{#bend::print},@*
       @code{#ly:text-interface::print},@*
       @code{##f}
@item vector
  @tab constants enclosed in @code{#(}@dots{}@code{)}.
  @tab @code{##(#t #t #f)}
@end multitable
@end raggedright
@end indentedblock

Note that number arguments to markup functions within a
@code{\markup} block need a leading hash mark.


@morerefs
Extending: @rextend{Scheme tutorial}.
@endmorerefs


@node Appearance of objects
@section Appearance of objects

Let us now put what we have learned into practice with a few
examples which show how tweaks may be used to change the
appearance of the printed music.


@node Visibility and color of objects
@subsection Visibility and color of objects

In the educational use of music we might wish to print a score
with certain elements omitted as an exercise for the student,
who is required to supply them.  As a simple example,
let us suppose the exercise is to supply the missing bar lines
in a piece of music.  But the bar lines are normally inserted
automatically.  How do we prevent them printing?

Before we tackle this, let us remember that object properties are
grouped in what are called @dfn{interfaces} -- @pxref{Properties
found in interfaces}.  This is simply to group together those
properties that may be used together to tweak a graphical object -- if
one of them is allowed for an object, so are the others.  Some objects
then use the properties in some interfaces, others use them from other
interfaces.  The interfaces that contain the properties used by a
particular grob are listed in the IR at the bottom of the page
describing that grob, and those properties may be viewed by looking at
those interfaces.

We explained how to find information about grobs in @ref{Properties of
layout objects}.  Using the same approach, we go to the IR to find the
layout object which prints bar lines.  Going via @emph{Backend} and
@emph{All layout objects} we find there is a layout object called
@code{BarLine}.  Its properties include two that control its
visibility: @code{break-visibility} and @code{stencil}.  @code{BarLine} also
supports a number of interfaces, including the @code{grob-interface},
where we find the @code{transparent} and the @code{color} properties.
All of these can affect the visibility of bar lines (and, of course,
by extension, many other layout objects, too).  Let's consider each of
these in turn.


@node The stencil property
@unnumberedsubsubsec The @code{stencil} property

@cindex @code{stencil} property

This property controls the appearance of the bar lines by specifying
the symbol (glyph) that should be printed.  In common
with many other properties, it can be set to print nothing by
setting its value to @code{#f}.  Let's try it, as before, omitting
the implied context, @code{Voice}.

@cindex @code{BarLine}, example of overriding
@cindex @code{stencil} property, example

@lilypond[quote,ragged-right,verbatim]
\relative {
  \time 12/16
  \override BarLine.stencil = ##f
  c''4 b8 c d16 c d8 |
  g,8 a16 b8 c d4 e16 |
  e8
}
@end lilypond

The bar lines are still printed.  What is wrong?  Go back to the IR
and look again at the page giving the properties of @code{BarLine}.  At the
top of the page it says @qq{@code{BarLine} objects are created by
the following engraver(s):
@code{Bar_engraver}}.  Go to the @code{Bar_engraver} page.  At the bottom it
gives a list of contexts in which the bar engraver operates.  All of
them are of the type @code{Staff}, so the reason the @code{\override}
command failed to work as expected is because @code{BarLine} is not in
the default @code{Voice} context.  If the context is specified
incorrectly, the command simply does not work, without producing
an error message or warning.  Let's try correcting
it by adding the correct context:

@cindex @code{BarLine}, example of overriding
@cindex @code{stencil} property, example

@lilypond[quote,ragged-right,verbatim]
\relative {
  \time 12/16
  \override Staff.BarLine.stencil = ##f
  c''4 b8 c d16 c d8 |
  g,8 a16 b8 c d4 e16 |
  e8
}
@end lilypond

Now the bar lines have vanished.  Setting the @code{stencil}
property to @code{#f} is such a frequent operation that there is a
shorthand for it called @code{\omit}:

@funindex \omit

@lilypond[quote,ragged-right,verbatim]
\relative {
  \time 12/16
  \omit Staff.BarLine
  c''4 b8 c d16 c d8 |
  g,8 a16 b8 c d4 e16 |
  e8
}
@end lilypond

Note, though, that setting the @code{stencil} property to @code{#f}
will cause errors when the dimensions of the object are required for
correct processing.  For example, errors will be generated if the
@code{stencil} property of the @code{NoteHead} object is set to
@code{#f}.  If this is the case, you can instead use the
@code{point-stencil} function, which sets the stencil to an object
with zero size:

@lilypond[quote,verbatim]
\relative {
  c''4 c
  \once \override NoteHead.stencil = #point-stencil
  c4 c
}
@end lilypond

@node The break-visibility property
@unnumberedsubsubsec The @code{break-visibility} property

@cindex @code{break-visibility} property

We see from the @code{BarLine} properties in the IR that the
@code{break-visibility} property requires a vector of three Booleans.
These control respectively whether bar lines are printed at the end of
a line, in the middle of lines, and at the beginning of lines.  For
our example we want all bar lines to be suppressed, so the value we
need is @code{#(#f #f #f)} (also available
under the name @code{all-invisible}).  Let's try that, remembering to include
the @code{Staff} context.  Note also that in writing this value we
have @code{##} before the opening parenthesis.  One @samp{#} is required
as part of vector constant syntax, and the first @samp{#} is
required, as always, to precede the value itself in the
@code{\override} command.

@cindex @code{BarLine}, example of overriding
@cindex @code{break-visibility} property, example

@lilypond[quote,ragged-right,verbatim]
\relative {
  \time 12/16
  \override Staff.BarLine.break-visibility = ##(#f #f #f)
  c''4 b8 c d16 c d8 |
  g,8 a16 b8 c d4 e16 |
  e8
}
@end lilypond

And we see this too removes all the bar lines.

@node The transparent property
@unnumberedsubsubsec The @code{transparent} property

@cindex @code{transparent} property

We see from the properties specified in the @code{grob-interface} page
in the IR that the @code{transparent} property is a Boolean.  This
should be set to @code{#t} to make the grob transparent.  In this next
example let us make the time signature invisible rather than the bar
lines.  To do this we need to find the grob name for the time
signature.  Back to the @q{All layout objects} page in the IR to find
the properties of the @code{TimeSignature} layout object.  This is
produced by the @code{Time_signature_engraver}; you can check that it
also lives in the @code{Staff} context and also supports the
@code{grob-interface}.  So the command to make the time signature
transparent is

@cindex @code{TimeSignature}, example of overriding
@cindex @code{transparent} property, example

@lilypond[quote,ragged-right,verbatim]
\relative {
  \time 12/16
  \override Staff.TimeSignature.transparent = ##t
  c''4 b8 c d16 c d8 |
  g,8 a16 b8 c d4 e16 |
  e8
}
@end lilypond

Again, setting the @code{transparent} property is a rather
frequent operation, so we have a shorthand for it called
@code{\hide}.

@funindex \hide

@lilypond[quote,ragged-right,verbatim]
\relative {
  \time 12/16
  \hide Staff.TimeSignature
  c''4 b8 c d16 c d8 |
  g,8 a16 b8 c d4 e16 |
  e8
}
@end lilypond

In either case, the time signature is gone, but this command
leaves a gap where
the time signature should be.  Maybe this is what is wanted for
an exercise for the student to fill it in, but in other
circumstances a gap might be undesirable.  To remove it, the
stencil for the time signature should be set to @code{#f}
instead:

@cindex @code{TimeSignature}, example of overriding
@cindex @code{stencil} property, example

@lilypond[quote,ragged-right,verbatim]
\relative {
  \time 12/16
  \omit Staff.TimeSignature
  c''4 b8 c d16 c d8 |
  g,8 a16 b8 c d4 e16 |
  e8
}
@end lilypond

@noindent
and the difference is obvious: setting the stencil to @code{#f}
(possibly via @code{\omit})
removes the object entirely; making the object @code{transparent}
(which can be done using @code{\hide})
leaves it where it is, but makes it invisible.

@xref{Simulating a fermata in MIDI}, for another example
demonstrating the difference between @code{\omit} and
@code{\hide}.

@node The color property
@unnumberedsubsubsec The @code{color} property

@cindex @code{color} property

Finally let us try making the bar lines invisible by coloring
them white.  (There is a difficulty with this in that the
white bar line may or may not blank out the staff lines where
they cross.  You may see in some of the examples below that this
happens unpredictably.  The details of why this is so and how to
control it are covered in @rnotation{Painting objects white}.  But at
the moment we are learning about color, so please just accept this
limitation for now.)

The @code{grob-interface} specifies that the
@code{color} property value is a list, but there is no
explanation of what that list should be.  The list it
requires is actually a list of values in internal units,
but, to avoid having to know what these are, several ways
are provided to specify colors.  The first way is to use one
of the predefined @q{CSS} colors listed in
@rnotation{List of colors}.  To set the bar lines to white
we write

@cindex @code{BarLine}, example of overriding
@cindex @code{color} property, example
@cindex color, css
@cindex css colors

@lilypond[quote,ragged-right,verbatim]
\relative {
  \time 12/16
  \override Staff.BarLine.color = "white"
  c''4 b8 c d16 c d8 |
  g,8 a16 b8 c d4 e16 |
  e8
}
@end lilypond

@noindent
and again, we see the bar lines are not visible.  Note that
@code{"white"} is not preceded by an apostrophe -- it is not
a symbol, but a character string, mapped to a predefined list
of internal values.  In that regard, LilyPond's syntax mimics the
@uref{https://www.w3.org/Style/CSS/, CSS language}
commonly used in webpages; in addition to predefined names,
we can specify a hexadecimal color code:

@cindex @code{BarLine}, example of overriding
@cindex @code{color} property, example

@lilypond[quote,ragged-right,verbatim]
\relative {
  \time 12/16
  \override Staff.BarLine.color = "#FFFFFF"
  c''4 b8 c d16 c d8 |
  g,8 a16 b8 c d4 e16 |
  e8
}
@end lilypond

We could even define that color as a variable, and
then use that variable as a property definition.
Since it is both a LilyPond variable and a Scheme
object, it can be prefixed with a backslash
or with a hash character without any difference:

@lilypond[quote,ragged-right,verbatim]
whiteVar = "#FFFFFF"

\relative {
  \time 12/16
  \override Staff.BarLine.color = \whiteVar
  c''4 b8 c d16 c d8 |
  \override Staff.BarLine.color = #whiteVar
  g,8 a16 b8 c d4 e16 |
  e8
}
@end lilypond

@cindex color, rgb
@funindex rgb-color

Another way of adding colors to your score is by using a function.
There are two useful functions in this regard; one is the
@code{x11-color} function, which we'll get to use shortly.
The other one, the @code{rgb-color} function, closely
demonstrates LilyPond's internal logic:  it takes
three arguments giving the intensities of the red, green, and
blue colors.  These take values in the range 0 to@tie{}1.  So to
set the color to red the value should be @code{(rgb-color 1 0 0)}
and to white it should be @code{(rgb-color 1 1 1)}:

@cindex @code{BarLine}, example of overriding
@cindex @code{color} property, example

@lilypond[quote,ragged-right,verbatim]
\relative {
  \time 12/16
  \override Staff.BarLine.color = #(rgb-color 1 1 1)
  c''4 b8 c d16 c d8 |
  g,8 a16 b8 c d4 e16 |
  e8
}
@end lilypond

@noindent
Note that in this case the whole function call has to be
enclosed in parentheses.  The same can be said of the
@code{x11-color} function which we just skipped over.

@cindex color, X11
@funindex x11-color

@code{x11-color}, again, maps predefined color names
to internal values, but offers many more choices
than CSS names, as you can see in @rnotation{List of colors}.
For example, the X11 set of colors includes an extensive
grey scale, whose names range from black, @code{'grey0},
to white, @code{'grey100}, in steps of@tie{}1.  Let's illustrate
this by setting all the layout objects in our example to
various shades of grey:

@cindex @code{StaffSymbol}, example of overriding
@cindex @code{TimeSignature}, example of overriding
@cindex @code{Clef}, example of overriding
@cindex @code{NoteHead}, example of overriding
@cindex @code{Stem}, example of overriding
@cindex @code{BarLine}, example of overriding
@cindex @code{color} property, example
@cindex @code{x11-color}, example of using

@lilypond[quote,ragged-right,verbatim]
\relative {
  \time 12/16
  \override Staff.StaffSymbol.color = #(x11-color 'grey30)
  \override Staff.TimeSignature.color = #(x11-color 'grey60)
  \override Staff.Clef.color = #(x11-color 'grey60)
  \override Voice.NoteHead.color = #(x11-color 'grey85)
  \override Voice.Stem.color = #(x11-color 'grey85)
  \override Staff.BarLine.color = #(x11-color 'grey10)
  c''4 b8 c d16 c d8 |
  g,8 a16 b8 c d4 e16 |
  e8
}
@end lilypond

@noindent
Note the contexts associated with each of the layout objects.
It is important to get these right, or the commands will not
work!  Remember, the context is the one in which the appropriate
engraver is placed.  The default context for engravers can be
found by starting from the layout object, going from there to
the engraver which produces it, and on the engraver page in the
IR it tells you in which context the engraver will normally be
found.


@node Size of objects
@subsection Size of objects

@cindex changing size, of objects
@cindex size, of objects
@cindex object, size of
@cindex object, changing size of

Let us begin by looking again at the earlier example
(see @ref{Nesting music expressions}) which showed
how to introduce a new temporary staff, as in an @rglos{ossia}.

@cindex @code{alignAboveContext} property, example
@cindex @code{\with}, example

@lilypond[quote,ragged-right,verbatim]
\new Staff ="main" {
  \relative {
    r4 g'8 g c4 c8 d |
    e4 r8
    <<
      { f8 c c }
      \new Staff \with {
        alignAboveContext = "main" }
      { f8 f c }
    >>
    r4 |
  }
}
@end lilypond

Ossia are normally written without clef and time signature, and
are usually printed slightly smaller than the main staff.  We
already know now how to remove the clef and time signature --
we simply set the stencil of each to @code{#f}, as follows:

@cindex @code{alignAboveContext} property, example
@cindex @code{\with}, example
@cindex @code{Clef}, example of overriding
@cindex @code{TimeSignature}, example of overriding
@funindex \omit

@lilypond[quote,ragged-right,verbatim]
\new Staff ="main" {
  \relative {
    r4 g'8 g c4 c8 d |
    e4 r8
    <<
      { f8 c c }
      \new Staff \with {
        alignAboveContext = "main"
      }
      {
        \omit Staff.Clef
        \omit Staff.TimeSignature
        { f8 f c }
      }
    >>
    r4 |
  }
}
@end lilypond

@noindent
where the extra pair of braces after the @code{\with} clause are
required to ensure the enclosed overrides and music are applied
to the ossia staff.

But what is the difference between modifying the staff context by
using @code{\with} and modifying the stencils of the clef and the
time signature with @code{\override}, or in this case
@code{\omit}?
The main difference is that
changes made in a @code{\with} clause are made at the time the
context is created, and remain in force as the @strong{default}
values for the duration of that context, whereas
@code{\set} or @code{\override} commands embedded in the
music are dynamic -- they make changes synchronized with
a particular point in the music.  If changes are unset or
reverted using @code{\unset} or @code{\revert} they return to
their default values, which will be the ones set in the
@code{\with} clause, or if none have been set there, the normal
default values.

Some context properties can be modified only in @code{\with} clauses.
These are those properties which cannot sensibly be changed after the
context has been created.  @code{alignAboveContext} and its partner,
@code{alignBelowContext}, are two such properties -- once the staff
has been created its alignment is decided and it would make no sense
to try to change it later.

The default values of layout object properties can also be set
in @code{\with} clauses.  Simply use the normal @code{\override}
command leaving out the context name, since this is unambiguously
defined as the context which the @code{\with} clause is modifying.
In fact, an error will be generated if a context is specified
in this location.

So we could replace the example above with

@cindex @code{alignAboveContext} property, example
@cindex @code{\with}, example
@cindex @code{Clef}, example of overriding
@cindex @code{TimeSignature}, example of overriding

@lilypond[quote,ragged-right,verbatim]
\new Staff ="main" {
  \relative {
    r4 g'8 g c4 c8 d |
    e4 r8
    <<
      { f8 c c }
      \new Staff \with {
        alignAboveContext = "main"
        % Don't print clefs in this staff
        \override Clef.stencil = ##f
        % Don't print time signatures in this staff
        \override TimeSignature.stencil = ##f
      }
      { f8 f c }
    >>
    r4 |
  }
}
@end lilypond

It turns out that we can also employ the shorthands @code{\hide}
and @code{\omit} for setting the @code{transparent} property and
clearing the @code{stencil} here, leading to the result

@lilypond[quote,ragged-right,verbatim]
\new Staff ="main" {
  \relative {
    r4 g'8 g c4 c8 d |
    e4 r8
    <<
      { f8 c c }
      \new Staff \with {
        alignAboveContext = "main"
        % Don't print clefs in this staff
        \omit Clef
        % Don't print time signatures in this staff
        \omit TimeSignature
      }
      { f8 f c }
    >>
    r4 |
  }
}
@end lilypond

Finally we come to changing the size of layout objects.

Some layout objects are created as glyphs selected from a typeface
font.  These include note heads, accidentals, markup, clefs, time
signatures, dynamics, and lyrics.  Their size is changed by modifying
the @code{font-size} property, as we shall shortly see.  Other layout
objects such as slurs and ties -- in general, spanner objects -- are
drawn individually, so there is no @code{font-size} associated with
them.  These objects generally derive their size from the objects to
which they are attached, so usually there is no need to change their
size manually.  Still other properties such as the length of stems and
bar lines, thickness of beams and other lines, and the separation of
staff lines all need to be modified in special ways.

Returning to the ossia example, let us first change the font size.
We can do this in two ways.  We can either change the size of the
fonts of each object type, like @code{NoteHead}s with commands
like

@example
\override NoteHead.font-size = -2
@end example

or we can change the size of all fonts by setting a special
property, @code{fontSize}, using @code{\set}, or by including
it in a @code{\with} clause (but without the @code{\set}).

@example
\set fontSize = -2
@end example

Both of these statements would cause the font size to be reduced
by two steps from its previous value, where each
step reduces or increases the size by approximately 12%.

Let's try it in our ossia example:

@cindex @code{alignAboveContext} property, example
@cindex @code{\with}, example
@cindex @code{Clef}, example of overriding
@cindex @code{TimeSignature}, example of overriding
@cindex @code{fontSize} property, example

@lilypond[quote,ragged-right,verbatim]
\new Staff ="main" {
  \relative {
    r4 g'8 g c4 c8 d |
    e4 r8
    <<
      { f8 c c }
      \new Staff \with {
        alignAboveContext = "main"
        \omit Clef
        \omit TimeSignature
        % Reduce all font sizes by ~24%
        fontSize = -2
      }
      { f8 f c }
    >>
    r4 |
  }
}
@end lilypond

This is still not quite right.  The note heads and flags are
smaller, but the stems are too long in proportion and the
staff lines are spaced too widely apart.  These need to be
scaled down in proportion to the font reduction.  The next
sub-section discusses how this is done.

@node Length and thickness of objects
@subsection Length and thickness of objects

@cindex distance, between objects
@cindex thickness, of objects
@cindex length, of objects
@cindex size, changing
@cindex stem length, changing
@cindex staff line spacing, changing
@cindex staff space, unit

Distances and lengths in LilyPond are generally measured in
staff spaces, i.e., the distance between adjacent lines in the staff
(or occasionally half staff spaces), while most thickness
properties are measured as multiples of the current staff-line
thickness (which is given by the @code{thickness} property of the
@code{StaffSymbol} grob).  For example, by default, the lines of
hairpins are given a thickness of 1@tie{}unit of the staff-line thickness,
while the thickness of a note stem is 1.3@tie{}units.  Note, though,
that some thickness properties are different; for example, the
thickness of beams is controlled by the value of the
@code{beam-thickness} property, which is measured in staff spaces.

So how are lengths to be scaled in proportion to the font size?
This can be done with the help of a special function called
@code{magstep} provided for exactly this purpose.  It takes
one argument, the change in font size (value@tie{}@w{-2} in the example above)
and returns a scaling factor suitable for reducing other
objects in proportion.  The trick is now not to actually change
the lengths of stems and other grobs but to rather change the unit on
which the length values are based -- it is the @code{staff-space}
property of the @code{StaffSymbol} grob.

@cindex @code{alignAboveContext} property, example
@cindex @code{\with}, example
@cindex @code{Clef}, example of overriding
@cindex @code{TimeSignature}, example of overriding
@cindex @code{fontSize} property, example
@cindex @code{StaffSymbol}, example of overriding
@cindex @code{magstep} function, example of using
@cindex @code{staff-space} property, example
@cindex @code{stencil} property, example

@lilypond[quote,ragged-right,verbatim]
\new Staff ="main" {
  \relative {
    r4 g'8 g c4 c8 d |
    e4 r8
    <<
      { f8 c c }
      \new Staff \with {
        alignAboveContext = "main"
        \omit Clef
        \omit TimeSignature
        fontSize = -2
        % Reduce stem length and line spacing to match
        \override StaffSymbol.staff-space = #(magstep -2)
      }
      { f8 f c }
    >>
    r4 |
  }
}
@end lilypond

While changing @code{staff-space} affects the scale of the ossia, it
does not affect the horizontal spacing -- this is determined by
the layout of the main music in order to remain
synchronized with it, so it is not affected by any of these changes in
size.  Of course, if the scale of all the main music were changed in
this way then the horizontal spacing would be affected.  This is
discussed later in the layout section.

This, then, completes the creation of an ossia.  The sizes and
lengths of all other objects may be modified in analogous ways.

For small changes in scale, as in the example above, the
thickness of the various drawn lines such as bar lines,
beams, hairpins, slurs, etc., does not usually require global
adjustment.  If the thickness of any particular layout object
needs to be adjusted this can be best achieved by overriding its
@code{thickness} property.  An example of changing the thickness
of slurs was shown above in @ref{Properties of layout objects}.
The thickness of all drawn objects (i.e., those not produced
from a font) may be changed in the same way.


@node Offsets and alignments
@section Offsets and alignments

LilyPond needs to know where each and every grob should be placed.
This placement information is stored using relative coordinates:
the position of a grob is defined relative to the position of its
parent (if we had to use absolute coordinates instead -- e.g.,
distances from the edges of the page -- it would be hard to
maintain spatial relationships between grobs).

Every grob knows which grob is its parent in the respective axis.
For example, a @code{Flag} grob knows that its x-parent is a
particular @code{Stem} grob.

@cindex @code{X-offset} property
@cindex @code{Y-offset} property

Relative coordinates that describe a grob's placement are stored
in grob properties called @code{X-offset} and @code{Y-offset}.
They are measured in staff spaces.  @code{X-offset} is the
horizontal displacement between a grob's @dfn{reference point} and
the reference point of a grob's x-parent (similarly with
@code{Y-offset}).

What is a reference point?  It's a special point that defines the
grob's position.  Think about geometry: if you have to define
where a figure is placed on a plane, you'll usually say something
like @qq{the lower left corner of this square has coordinates
(0,@tie{}2)}, or @qq{the center of this circle is at
(@w{-1},@tie{}3)}. @q{Lower left corner} and @q{center} would be
the reference points for square and circle, respectively.

This illustration shows where reference points of particular grobs
are located, indicated as red dots (for example, the middle staff
line for bars and stems, the intersection between the baseline and
the left edge for a text box, or the vertical center at the left
side for a note head).

@example
@{
  \override NoteHead.style = #'altdefault     % for breve

  \time 4/2 g'2-> c''\fermata as'1^"Yogi" |
  b'\breve _"Larry" |
  \mark "Twinkle" e''8 s4.. \bar "|."
@}
@end example

@lilypond[quote,staffsize=26]
\include "reference-points.ly"

\new Staff \with {
  \printRefpoint ##f #'all-grobs
} {
  \override NoteHead.style = #'altdefault     % for breve

  \time 4/2 g'2-> c''\fermata as'1^"Yogi" |
  b'\breve _"Larry" |
  \mark "Twinkle" e''8 s4.. \bar "|."
}

\layout {
  \context {
    \Score
    \remove "Mark_engraver"
  }
  \context {
    \Staff
    \consists "Mark_engraver"
  }
}
@end lilypond

By overriding the @code{X-offset} or @code{Y-offset} value we can
move grobs relative to their parents:

@cindex @code{X-offset} property, example
@cindex @code{Y-offset} property, example

@example
@{
  \override Script.X-offset = 3               % fermata, accent
  \override TextScript.X-offset = 2           % "Yogi", "Larry"
  \override Stem.X-offset = -2
  \override Score.RehearsalMark.Y-offset = 5  % "Twinkle"

  \override NoteHead.style = #'altdefault     % for breve

  \time 4/2 g'2-> c''\fermata as'1^"Yogi" |
  b'\breve _"Larry" |
  \mark "Twinkle" e''8 s4.. \bar "|."
@}
@end example

@lilypond[quote,staffsize=26]
\include "reference-points.ly"

\new Staff \with {
  \printRefpoint ##f #'all-grobs
} {
  \override Script.X-offset = 3               % fermata, accent
  \override TextScript.X-offset = 2           % "Yogi", "Larry"
  \override Stem.X-offset = -2
  \override Score.RehearsalMark.Y-offset = 5  % "Twinkle"

  \override NoteHead.style = #'altdefault     % for breve

  \time 4/2 g'2-> c''\fermata as'1^"Yogi" |
  b'\breve _"Larry" |
  \mark "Twinkle" e''8 s4.. \bar "|."
}

\layout {
  \context {
    \Score
    \remove "Mark_engraver"
  }
  \context {
    \Staff
    \consists "Mark_engraver"
  }
}
@end lilypond

@noindent
Note that the @code{Flag} grob moved together with its @code{Stem}
grob.

@warning{Changing @code{X-offset} or @code{Y-offset} doesn't
always work as expected, see @rnotation{Aligning objects}.}

@cindex @code{X-extent} property
@cindex @code{Y-extent} property

Now, let's explain another pair of properties: @code{X-extent} and
@code{Y-extent}.  Each of them is a pair of numbers (internally,
LilyPond calls this an @dfn{interval}), and they store a grob's
dimensions relative to its reference point.  For example,
@code{X-extent} equal to (@w{-1} .@tie{}4) means that the left
edge of the grob is 1@tie{}staff space to the left of its
reference point, and the right edge is 4@tie{}staff spaces to the
right from reference point, for a total width of 4@tie{}- @w{(-1)}
= 4 +@tie{}1 = 5@tie{}staff spaces.  Both numbers in an extent may
be positive; for example, (2 .@tie{}3) is a valid extent: it means
that the whole grob is on the right of its reference point, and
the width of the grob is 3@tie{}- 2 =@tie{}1 staff space.
Similarly, both numbers can be negative; these situations are
quite unusual but won't give LilyPond headaches.  The most common
situation (at least for @code{X-extent}) is that the first number
is@tie{}0, which means that the reference point is on the left
edge of the grob.

The previous example demonstrates this nicely: both the accent's
and the fermata's left edge is left of the reference point; the
same holds for the breve note head (we selected the
@code{'altdefault} note head style to make this better visible).

Now, suppose that we want to position a @code{RehearsalMark} grob
so that its right edge is aligned with a bar line (by default,
LilyPond horizontally centers the rehearsal mark).  With
@code{X-offset} equal to zero (i.e., its reference point is
aligned on the parent, which is equal to the bar line in this
situation), the @code{RehearsalMark} grob is placed like this:

@lilypond[quote,verbatim]
{
  \override Score.RehearsalMark.X-offset = 0
  b4 b b b \mark "Twinkle" b b b b
}
@end lilypond

@noindent
So, we need to shift it.  Remember what the second number in
@code{X-extent} means?  It's the position of a grob's right edge
relative to its reference point.  If we subtract this value from
zero, we'll get the x-offset we want:

@lilypond[quote,verbatim]
{
  % value -10.4 found by trial and error
  \override Score.RehearsalMark.X-offset = -10.4
  b4 b b b \mark "Twinkle" b b b b
}
@end lilypond

What if we wanted to center some grob on the reference point on
its parent?  That's simple: calculate the displacement between a
grob's reference point and the @emph{center} of its extent.  Some
examples:

@itemize
@item @code{X-extent} = (-2 . 2)@*
      @result{} @code{X-offset} = 0 - 0 = 0
@item @code{X-extent} = (0 . 4)@*
      @result{} @code{X-offset} = 0 - (0 + 0.5 *@tie{}4) = @w{-2}
@item @code{X-extent} = (-2 . 4)@*
      @result{} @code{X-offset} = 0 - ((0.5 * @w{-2}) + (0.5 * 4))
                                = 0 - (@w{-1} +@tie{}2) = @w{-1}
@end itemize

There is already a function with a super-long name available that
can do these calculations for us:

@example
\override @var{GrobName}.X-offset =
  #ly:self-alignment-interface::x-aligned-on-self
@end example

@noindent
and we only need to specify what alignment we want:

@example
\override @var{GrobName}.self-alignment-X = #RIGHT
@end example

This code tells the procedure to find the displacement between a
grob's reference point and its right edge, and use it as the
x-offset, which will result in the grob being right-aligned.

So, if you change a grob's extent, you will affect how it will be
aligned (because LilyPond will think that the grob's dimensions
are different):

@lilypond[quote,verbatim]
{
  \override Score.RehearsalMark.X-extent = #'(0 . 8)
  \override Score.RehearsalMark.self-alignment-X = #RIGHT
  b4 b b b \mark "Twinkle" b b b b
}
@end lilypond

Also, if a grob's extent is empty (i.e., it's not an interval
pair), procedures like @code{x-aligned-on-self} won't have any
information about a grob's dimensions, so they won't be able to
calculate an offset (they'll just return value@tie{}0).  In other
words, a grob with an empty extent can only be @q{aligned} on its
reference point because there's no other information that can be
used for alignment:

@lilypond[quote,verbatim]
{
  \override Score.RehearsalMark.X-extent = ##f
  b4 b b b \mark "Twinkle" b b b b
}
@end lilypond

Notice that an empty extent and a zero extent result in the same
positioning:

@lilypond[quote,verbatim]
{
  \override Score.RehearsalMark.X-extent = #'(0 . 0)
  b4 b b b \mark "Twinkle" b b b b
}
@end lilypond

However, an empty extent (or a zero extent, or any other extent)
doesn't prevent us from placing the grob at any location we want
-- we just can't use alignment procedures for that.  We can still
specify any offset we want, and it will work as usual:

@lilypond[quote,verbatim]
{
  \override Score.RehearsalMark.X-extent = ##f
  \override Score.RehearsalMark.X-offset = -10.4
  b4 b b b \mark "Twinkle" b b b b
}
@end lilypond

Now, there is one more thing to keep in mind: a grob's parent has
its own dimensions, too, and we need to take them into account.
For example, if we write

@lilypond[quote,verbatim]
{ a'1 }
\addlyrics {
  \override LyricText.X-offset =
    #ly:self-alignment-interface::x-aligned-on-self
  \override LyricText.self-alignment-X = #CENTER
  nn
}
@end lilypond

@noindent
the @code{LyricText} grob will be centered, but @emph{on the
reference point of its parent} (i.e., the center of the syllable
will be aligned to the reference point of the note head).  If we
want the center of the @code{LyricText} grob to be aligned with
the center of its parent @code{NoteHead} grob, we have to use a
different procedure: @code{aligned-on-x-parent}.  It works very
similar to @code{x-aligned-on-self}, but in addition to
calculating an offset based on a grob's own extent, it also uses
the grob's parent extent and computes positions relative to the
center of the extent:

@lilypond[quote,verbatim]
{ a'1 }
\addlyrics {
  \override LyricText.X-offset =
    #ly:self-alignment-interface::aligned-on-x-parent
  \override LyricText.self-alignment-X = #CENTER
  nn
}
@end lilypond


@node Placement of objects
@section Placement of objects

There are some objects in musical notation that belong to
the staff and there are other objects that should be
placed outside the staff.  These are called @dfn{within-staff}
objects and @dfn{outside-staff} objects, respectively.

Within-staff objects are those that are located on the staff
-- note heads, stems, accidentals, etc.  The positions of
these are usually fixed by the music itself -- they are
vertically positioned on specific lines of the staff or are
tied to other objects that are so positioned.  Collisions of
note heads, stems and accidentals in closely set chords are
normally avoided automatically.  There are commands and
overrides which can modify this automatic behavior, as we
shall shortly see.


@node Automatic behavior
@subsection Automatic behavior

@cindex within-staff objects
@cindex outside-staff objects
@cindex object, within-staff
@cindex object, outside-staff

@cindex @code{outside-staff-priority} property

Objects belonging outside the staff include things such as
rehearsal marks, text and dynamic markings.  LilyPond's rule for
the vertical placement of outside-staff objects is to place them
as close to the staff as possible but not so close that they
collide with any other object.  LilyPond uses the
@code{outside-staff-priority} property to determine the order in
which the objects should be placed, as follows.

First, LilyPond places all the within-staff objects.
Then it sorts the outside-staff objects according to their
@code{outside-staff-priority}.  The outside-staff objects are
taken one by one, beginning with the object with the lowest
@code{outside-staff-priority}, and placed so that they do not
collide with any objects that have already been placed.  That is,
if two outside-staff grobs are competing for the same space, the
one with the lower @code{outside-staff-priority} will be placed
closer to the staff.  If two objects have the same
@code{outside-staff-priority} the one encountered first will be
placed closer to the staff.

In the following example all the markup texts have the same
priority (since it is not explicitly set).  Note that @q{Text3}
is automatically positioned close to the staff again, nestling
under @q{Text2}.

@cindex markup example

@lilypond[quote,fragment,ragged-right,verbatim]
c''2^"Text1"
c''2^"Text2" |
c''2^"Text3"
c''2^"Text4" |
@end lilypond

Staves are also positioned, by default, as closely together as
possible (subject to a minimum separation).  If notes project
a long way towards an adjacent staff they will force the
staves further apart only if an overlap of the notation
would otherwise occur.  The following example demonstrates
this @q{nestling} of the notes on adjacent staves:

@lilypond[quote,ragged-right,verbatim]
<<
  \new Staff {
    \relative { c'4 a, }
  }
  \new Staff {
    \relative { c''''4 a, }
  }
>>
@end lilypond


@node Within-staff objects
@subsection Within-staff objects

@cindex @code{direction} property

We have already seen how the commands @code{\voice@var{XXX}} affect the
direction of slurs, ties, fingering, and everything else that depends on
the direction of the stems -- @pxref{Explicitly instantiating voices}.
These commands are essential when writing polyphonic music to permit
interweaving melodic lines to be distinguished.  But occasionally it may
be necessary to override this automatic behavior.  This can be done for
whole sections of music or even for an individual note.  The property
that controls this behavior is the @code{direction} property of each
layout object.  We first explain what this does, and then introduce a
number of ready-made commands which avoid you having to code explicit
overrides for the more common modifications.

Some layout objects like slurs and ties curve, bend, or point
either up or down; others like stems and flags also move to the
right or left when they point up or down.  This is controlled
automatically when @code{direction} is set.


@node The direction property
@unnumberedsubsubsec The @code{direction} property

@cindex @code{DOWN}
@cindex @code{UP}
@cindex @code{CENTER}

The following example shows the default positioning of slurs in the
first bar, with slurs starting on high notes positioned above the notes
and those starting on low notes positioned below, followed by a bar
with both slurs forced down, a bar with both slurs forced up, and
finally a bar with both slurs reverted back to the default behavior.

@cindex @code{Slur}, example of overriding
@cindex @code{direction} property, example

@lilypond[quote,fragment,verbatim]
a'4( g') c''( a') |
\override Slur.direction = #DOWN
a'4( g') c''( a') |
\override Slur.direction = #UP
a'4( g') c''( a') |
\revert Slur.direction
a'4( g') c''( a') |
@end lilypond

Here we have used the constants @code{DOWN} and @code{UP}.
These have the values @w{@code{-1}} and @code{+1}, respectively, and
these numerical values may be used instead.  The value@tie{}@code{0}
may also be used in some cases.  It is simply treated as meaning
@code{UP} for slurs, but for some objects it means @q{center}.
There is a constant, @code{CENTER}, which has the value@tie{}@code{0}.

However, these explicit overrides are not usually used, as there are
simpler and equivalent predefined commands available.  Here is a table of
the commonest.  The meaning of each is stated where it is not obvious.

@smallindentedblock
@raggedright
@multitable {@code{\phrasingSlurDown}} {@code{\phrasingSlurUp}}{@code{\phrasingSlurNeutral}} {Text entered as spanner is}
@headitem Down
  @tab Up
  @tab Revert
  @tab Effect
@item @code{\dotsDown}
  @tab @code{\dotsUp}
  @tab @code{\dotsNeutral}
  @tab Direction of movement to avoid staff lines
@item @code{\dynamicDown}
  @tab @code{\dynamicUp}
  @tab @code{\dynamicNeutral}
  @tab
@item @code{\phrasingSlurDown}
  @tab @code{\phrasingSlurUp}
  @tab @code{\phrasingSlurNeutral}
  @tab Note: distinct from slur commands
@item @code{\slurDown}
  @tab @code{\slurUp}
  @tab @code{\slurNeutral}
  @tab
@item @code{\stemDown}
  @tab @code{\stemUp}
  @tab @code{\stemNeutral}
  @tab
@item @code{\textSpannerDown}
  @tab @code{\textSpannerUp}
  @tab @code{\textSpannerNeutral}
  @tab Text entered as spanner is below/above staff
@item @code{\tieDown}
  @tab @code{\tieUp}
  @tab @code{\tieNeutral}
  @tab
@item @code{\tupletDown}
  @tab @code{\tupletUp}
  @tab @code{\tupletNeutral}
  @tab Tuplets are below/above notes
@end multitable
@end raggedright
@end smallindentedblock

The @q{neutral} variants of these commands are implemented
using @code{\revert} and these may @strong{not} be
preceded by @code{\once}.  If you wish to limit the
effect of the other commands (which are implemented using
@code{\override}) to a single timestep, you can precede them with
@code{\once} like you would do with explicit overrides.

Or, if just a single layout object needs to be forced up or down, the
direction indicators, @samp{^} or @samp{_}, may be used:

@lilypond[quote,fragment,verbatim]
a'4( g') c''( a') |
a'4^( g') c''_( a') |
@end lilypond

@node Fingering
@unnumberedsubsubsec Fingering

@cindex fingering, placement
@cindex fingering, chords

The placement of fingering on single notes can also be controlled
by the @code{direction} property, but changing @code{direction}
has no effect on chords.  As we shall see, there are special
commands which allow the fingering of individual notes
of chords to be controlled, with the fingering being placed
above, below, to the left, or to the right of each note.

First, here's the effect of @code{direction} on the fingering
attached to single notes.  The first bar shows the default
behavior, and the following two bars shows the effect of
specifying @code{DOWN} and @code{UP}:

@cindex @code{Fingering}, example of overriding
@cindex @code{direction} property, example

@lilypond[quote,verbatim]
\relative {
  c''4-5 a-3 f-1 c'-5 |
  \override Fingering.direction = #DOWN
  c4-5 a-3 f-1 c'-5 |
  \override Fingering.direction = #UP
  c4-5 a-3 f-1 c'-5 |
}
@end lilypond

@funindex -
@funindex ^
@funindex _

However, overriding the @code{direction} property is not the
easiest way of manually setting the fingering above or below
the notes; using @samp{_} or @samp{^} instead of @samp{-} before
the fingering number is usually preferable.  Here is the previous
example using this method:

@cindex fingering, example

@lilypond[quote,verbatim]
\relative {
  c''4-5 a-3 f-1 c'-5 |
  c4_5 a_3 f_1 c'_5 |
  c4^5 a^3 f^1 c'^5 |
}
@end lilypond

The @code{direction} property is ignored for chords, but the
directional prefixes, @samp{_} and @samp{^}, do work.  By default,
the fingering is automatically placed both above and below the
notes of a chord, as shown:

@cindex fingering, example

@lilypond[quote,verbatim]
\relative {
  <c''-5 g-3>4
  <c-5 g-3 e-2>4
  <c-5 g-3 e-2 c-1>4
}
@end lilypond

@noindent
but this may be overridden to manually force all or any of the
individual fingering numbers above or below:

@cindex fingering, example

@lilypond[quote,verbatim]
\relative {
  <c''-5 g-3 e-2 c-1>4
  <c^5 g_3 e_2 c_1>4
  <c^5 g^3 e^2 c_1>4
}
@end lilypond

@cindex @code{fingeringOrientations} property

Even greater control over the placement of fingering of the
individual notes in a chord is possible by using the
@code{\set fingeringOrientations} command.  The format of this
command is:

@example
\set fingeringOrientations = #'([up] [left/right] [down])
@end example

@noindent
@code{\set} is used because @code{fingeringOrientations} is a
property of the @code{Voice} context, created and used by the
@code{New_fingering_engraver}.

The property may be set to a list of one to three values.
It controls whether fingerings may be placed above (if
@code{up} appears in the list), below (if @code{down} appears),
to the left (if @code{left} appears), or to the right
(if @code{right} appears).  Conversely, if a location is not
listed, no fingering is placed there.  LilyPond takes these
constraints and works out the best placement for the fingering
of the notes of the following chords.  Note that @code{left} and
@code{right} are mutually exclusive -- fingerings may be placed
only on one side or the other, not both.

@warning{To control the placement of the fingering of a single
note using this command it is necessary to write it as a
single-note chord by placing angle brackets round it.}

Here are a few examples:

@cindex fingering, example
@cindex @code{\set}, example of using
@cindex @code{fingeringOrientations} property, example

@lilypond[quote,ragged-right,verbatim]
\relative {
  \set fingeringOrientations = #'(left)
  <f'-2>4
  <c-1 e-2 g-3 b-5>4
  \set fingeringOrientations = #'(left)
  <f-2>4
  <c-1 e-2 g-3 b-5>4 |
  \set fingeringOrientations = #'(up left down)
  <f-2>4
  <c-1 e-2 g-3 b-5>4
  \set fingeringOrientations = #'(up left)
  <f-2>4
  <c-1 e-2 g-3 b-5>4 |
  \set fingeringOrientations = #'(right)
  <f-2>4
  <c-1 e-2 g-3 b-5>4
}
@end lilypond

@noindent
If the fingering seems a little crowded the @code{font-size}
could be reduced.  The default value can be seen from the
@code{Fingering} object in the IR to be @w{@code{-5}}, so let's
try @w{@code{-7}}:

@lilypond[quote,ragged-right,verbatim]
\relative {
  \override Fingering.font-size = -7
  \set fingeringOrientations = #'(left)
  <f'-2>4
  <c-1 e-2 g-3 b-5>4
  \set fingeringOrientations = #'(left)
  <f-2>4
  <c-1 e-2 g-3 b-5>4 |
  \set fingeringOrientations = #'(up left down)
  <f-2>4
  <c-1 e-2 g-3 b-5>4
  \set fingeringOrientations = #'(up left)
  <f-2>4
  <c-1 e-2 g-3 b-5>4 |
  \set fingeringOrientations = #'(right)
  <f-2>4
  <c-1 e-2 g-3 b-5>4
}
@end lilypond


@node Outside-staff objects
@subsection Outside-staff objects

Outside-staff objects are automatically placed to avoid collisions.
There are several ways to override the automatic placement if the
positioning is not optimum.


@node The outside-staff-priority property
@unnumberedsubsubsec The @code{outside-staff-priority} property

Objects with the lower value of the @code{outside-staff-priority}
property are placed nearer to the staff, and other outside-staff
objects are then raised as far as necessary to avoid collisions.
The @code{outside-staff-priority} is defined in the
@code{grob-interface} and so is a property of all layout objects.
By default it is set to @code{#f} for all within-staff objects,
and to a numerical value appropriate to each outside-staff object
when the object is created.  See @rnotation{Default values for
outside-staff-priority} for an exhaustive table.

@cindex spanner

Note the unusual names for some of the objects: spanner objects
are automatically created to control the vertical positioning of
grobs which (might) start and end at different musical moments, so
changing the @code{outside-staff-priority} of the underlying grob
will have no effect.  For example, changing
@code{outside-staff-priority} of the @code{Hairpin} object will
have no effect on the vertical positioning of hairpins -- you must
change @code{outside-staff-priority} of the associated
@code{DynamicLineSpanner} object instead.  This override must be
placed at the start of the spanner, which might include several
linked hairpins and dynamics.

Here is an example showing the default placement of some of
these.

@cindex text spanner
@cindex ottava bracket

@funindex \startTextSpan
@funindex \stopTextSpan

@cindex @code{TextSpanner}, example of overriding
@cindex @code{bound-details} property, example

@lilypond[quote,fragment,ragged-right,verbatim]
% set details for later TextSpanner
\override TextSpanner.bound-details.left.text =
  \markup { \small \bold Slower }
% place dynamics above staff
\dynamicUp
% start ottava bracket
\ottava 1
c''4 \startTextSpan
% add dynamic text and hairpin
c''4\pp\<
c''4
% add text script
c''4^Text |
c''4 c''
% add dynamic text and terminate hairpin
c''4\ff c'' \stopTextSpan |
% stop ottava bracket
\ottava 0
c'4 c' c' c' |
@end lilypond

This example also shows how to create @dfn{text spanners} --
text with extender lines above a section of music.  The
spanner extends from the @code{\startTextSpan} command to
the @code{\stopTextSpan} command, and the format of the
text is defined by the @code{\override TextSpanner} command.
For more details see @rnotation{Text spanners}.

It also shows how ottava brackets are created.

@cindex tweaking bar number placement
@cindex bar number, tweaking placement
@cindex tweaking metronome mark placement
@cindex metronome mark, tweaking placement
@cindex tweaking rehearsal mark placement
@cindex rehearsal mark, tweaking placement

If the default values of @code{outside-staff-priority} do not give you
the placing you want, the priority of any of the objects may be
overridden.  Suppose we would like the ottava bracket to be placed
below the text spanner in the example above.  All we need to do is to
look up the priority of @code{OttavaBracket} in the table at
@rnotation{Default values for outside-staff-priority} or in the IR, and
reduce it to a value lower than that of a @code{TextSpanner},
remembering that @code{OttavaBracket} is created in the @code{Staff}
context:

@cindex @code{TextSpanner}, example of overriding
@cindex @code{bound-details} property, example

@lilypond[quote,fragment,ragged-right,verbatim]
% set details for later text spanner
\override TextSpanner.bound-details.left.text =
  \markup { \small \bold Slower }
% place dynamics above staff
\dynamicUp
% place following ottava bracket below text spanners
\once \override Staff.OttavaBracket.outside-staff-priority = 340
% start ottava bracket
\ottava 1
c''4 \startTextSpan
% add dynamic text
c''4\pp
% add dynamic line spanner
c''4\<
% add text script
c''4^Text |
c''4 c''
% add dynamic text
c''4\ff c'' \stopTextSpan |
% stop ottava bracket
\ottava 0
c'4 c' c' c' |
@end lilypond

Note that some of these objects, in particular bar numbers,
metronome marks, and rehearsal marks, live by default in the
@code{Score} context, so be sure to use the correct context
when these are being overridden.

@cindex slur, and @code{outside-staff-priority}
@cindex slur, and articulations
@cindex articulation, and slurs

@cindex @code{avoid-slur} property

Slurs by default are classed as within-staff objects, but
they often appear above the staff if the notes to
which they are attached are high on the staff.  This can push
outside-staff objects such as articulations too high, as the slur
will be placed first.  The @code{avoid-slur} property of the
articulation can be set to @code{'inside} to bring the articulation
inside the slur, but the @code{avoid-slur} property is effective
only if the @code{outside-staff-priority} is also set to @code{#f}.
Alternatively, the @code{outside-staff-priority} of the slur
can be set to a numerical value to cause it to be placed along with
other outside-staff objects according to that value.  Here's an
example showing the effect of the two methods:

@lilypond[quote,verbatim]
\relative c'' {
  c4( c^\markup { \tiny \sharp } d4.) c8 |
  c4(
    \once \override TextScript.avoid-slur = #'inside
    \once \override TextScript.outside-staff-priority = ##f
    c4^\markup { \tiny \sharp } d4.) c8 |
  \once \override Slur.outside-staff-priority = 500
    c4( c^\markup { \tiny \sharp } d4.) c8 |
}
@end lilypond

Changing the @code{outside-staff-priority} can also be used to
control the vertical placement of individual objects, although
the results may not always be desirable.  Suppose we would
like @qq{Text3} to be placed above @qq{Text4} in the example
under Automatic behavior, above (@pxref{Automatic behavior}).
All we need to do is to look up the priority of @code{TextScript}
in the IR or in the tables above, and increase the priority of
@qq{Text3} to a higher value:

@cindex @code{TextScript}, example of overriding
@cindex @code{outside-staff-priority} property, example

@lilypond[quote,fragment,ragged-right,verbatim]
c''2^"Text1"
c''2^"Text2" |
\once \override TextScript.outside-staff-priority = 500
c''2^"Text3"
c''2^"Text4" |
@end lilypond

This certainly lifts @qq{Text3} above @qq{Text4} but it also lifts it
above @qq{Text2}, and @qq{Text4} now drops down.  Perhaps this is not
so good.  What we would really like to do is to position all the
annotation at the same distance above the staff.  To do this, we
clearly will need to space the notes out horizontally to make more
room for the text.  This is done using the @code{\textLengthOn}
command.

@node The textLengthOn command
@unnumberedsubsubsec The @code{\textLengthOn} command

@cindex note, increase width with text

@funindex \textLengthOn
@funindex \textLengthOff

By default, text produced by markup takes up no horizontal space
as far as laying out the music is concerned.  The @code{\textLengthOn}
command changes this behavior, causing the notes to be spaced
out as far as is necessary to accommodate the text:

@lilypond[quote,fragment,ragged-right,verbatim]
\textLengthOn  % Cause notes to space out to accommodate text
c''2^"Text1"
c''2^"Text2" |
c''2^"Text3"
c''2^"Text4" |
@end lilypond

@funindex \markLengthOn
@funindex \markLengthOff

The command to revert to the default behavior is
@code{\textLengthOff}.  Alternatively, @code{\once} may be used
with @code{\textLengthOn} if the effect is to be limited to just a
single musical moment.
The corresponding spacing behavior for rehearsal marks and tempo
indications is independently controlled with the commands
@code{\markLengthOn} and @code{\markLengthOff}.

@cindex markup text, allowing collisions

Markup text will also avoid notes which project above the staff.
If this is not desired, the automatic displacement upwards may
be turned off by setting the priority to @code{#f}.  Here's an
example to show how markup text interacts with such notes.

@cindex @code{TextScript}, example of overriding
@cindex @code{outside-staff-priority} property, example

@lilypond[quote,ragged-right,verbatim]
\relative {
  % This markup is short enough to fit without collision
  c''2^"Tex" c'' |
  R1 |

  % This is too long to fit, so it is displaced upwards
  c,,2^"Text" c'' |
  R1 |

  % Turn off collision avoidance
  \once \override TextScript.outside-staff-priority = ##f
  c,,2^"Long Text   " c'' |
  R1 |

  % Turn off collision avoidance
  \once \override TextScript.outside-staff-priority = ##f
  \textLengthOn        % and turn on textLengthOn
  c,,2^"Long Text   "  % Spaces at end are honored
  c''2 |
}
@end lilypond

@node Dynamics placement
@unnumberedsubsubsec Dynamics placement

@cindex tweaking dynamics placement
@cindex dynamics, tweaking placement

Dynamic markings will normally be positioned beneath the
staff, but may be positioned above with the @code{\dynamicUp}
command.  They will be positioned vertically relative to the
note to which they are attached, and will float below (or above)
all within-staff objects such as phrasing slurs and bar numbers.
This can give quite acceptable results, as this example
shows:

@lilypond[quote,ragged-right,verbatim]
\relative {
  \clef "bass"
  \key aes \major
  \time 9/8
  \dynamicUp
  bes4.~\f\< \( bes4 bes8 des4\ff\> c16 bes\! |
  ees,2.~\)\mf ees4 r8 |
}
@end lilypond

However, if the notes and attached dynamics are close
together the automatic placement will avoid collisions
by displacing later dynamic markings further away, but this may
not be the optimum placement, as this rather artificial example
shows:

@lilypond[quote,ragged-right,verbatim,fragment]
\dynamicUp
\relative { a'4\f b\mf a\mp b\p }
@end lilypond

@noindent
Should a similar situation arise in @q{real} music, it may be
preferable to space out the notes a little further, so the dynamic
markings can all fit at the same vertical distance from the staff.  We
were able to do this for markup text by using the @code{\textLengthOn}
command, but there is no equivalent command for dynamic marks.  So we
shall have to work out how to do this using @code{\override} commands.

@node Grob sizing
@unnumberedsubsubsec Grob sizing

@cindex grob, sizing
@cindex sizing grobs

In general, all grobs support the @code{grob-interface}, which
contains, among other things, the properties @code{X-offset},
@code{Y-offset}, @code{X-extent}, and @code{Y-extent}
(@pxref{Offsets and alignments} for a thorough introduction).

@cindex @code{extra-spacing-width} property

By default, outside-staff objects are given a width (i.e., a
horizontal extent) of zero so that they may overlap in the
horizontal direction.  By convention, this is done by positioning
a grob's left edge at infinity and the right edge at minus
infinity by setting the @code{extra-spacing-width} property to
@code{'(+inf.0 .@tie{}-inf.0)}.  To ensure grobs do not overlap in
the horizontal direction we must override this value of
@code{extra-spacing-width} to give them a little extra spacing.
The units are the space between two staff lines, so moving the
left edge half a unit to the left and the right edge half a unit
to the right should do it:

@example
\override DynamicText.extra-spacing-width = #'(-0.5 . 0.5)
@end example

@noindent
Let's see whether this works in our previous example:

@cindex @code{DynamicText}, example of overriding
@cindex @code{extra-spacing-width} property, example


@lilypond[quote,ragged-right,verbatim,fragment]
\dynamicUp
% Extend width by 1 staff space
\override DynamicText.extra-spacing-width = #'(-0.5 . 0.5)
\relative { a'4\f b\mf a\mp b\p }
@end lilypond

@noindent
This looks better, but maybe we would prefer the dynamic marks
to be aligned along the same baseline rather than going up and
down with the notes.  The property to do this is
@code{staff-padding}, which is covered in the section on collisions
(see @ref{Collisions of objects}).


@node Vertical spacing
@section Vertical spacing

As a rule, LilyPond's vertical spacing of musical objects is pretty
good.  Let's see how it does with a simple song, with 2 voices and
piano accompaniment:

@lilypond[quote,fragment,ragged-right]
<<
  \new ChoirStaff
  <<
    \new Staff {
      \new Voice = "music" {
        b'2 c' c' c'
      }
    }
    \new Lyrics
    \lyricsto "music" {
      Here are some lyrics
    }
    \new Staff {
      \clef bass e'2 f e c
    }
  >>
  \new PianoStaff
  <<
    \new Staff {
      g''2 c'' c'' a''
    }
    \new Staff {
      \clef bass e2 f c e
    }
  >>
>>
@end lilypond

There's nothing wrong with the default vertical spacing.  However, let's
assume that you're working with a publisher with some specific
requirements for vertical spacing of staves and lyrics: they want
the lyrics spaced away from any notes, they want the piano
accompaniment spaced away from the vocal line and they want the two
piano staves pushed together tightly.  Let's start with the lyrics.

Lyrics sit within a system, and therefore the commands to space
them are found in @rnotation{Flexible vertical spacing within
systems}.  It tells us that lyrics are @q{non-staff lines} and
therefore the command to change their spacing will refer to a
property that has the word @code{nonstaff} in its name.  Spacing
them away from the staff to which they relate (the top line) is
indicated by the the word @code{relatedstaff}.  Spacing them from
the lower line is indicated by the word @code{unrelatedstaff}.
The vocal parts are part of a @code{VerticalAxisGroup}, so we need
to adjust its properties.  Let's try it and see if it works.

@cindex @code{VerticalAxisGroup}
@cindex @code{padding} sub-property
@cindex @code{nonstaff-relatedstaff-spacing} property
@cindex @code{nonstaff-unrelatedstaff-spacing} property

@lilypond[quote,fragment,ragged-right,verbatim]
<<
  \new ChoirStaff
  <<
    \new Staff {
      \new Voice = "music" {
        b'2 c' c' c'
      }
    }
    \new Lyrics \with {
      \override VerticalAxisGroup
                .nonstaff-relatedstaff-spacing.padding = 5
      \override VerticalAxisGroup
                .nonstaff-unrelatedstaff-spacing.padding = 5
    }
    \lyricsto "music" {
      Here are some lyrics
    }
    \new Staff {
      \clef bass e'2 f e c
    }
  >>
  \new PianoStaff
  <<
    \new Staff {
      g''2 c'' c'' a''
    }
    \new Staff {
      \clef bass e2 f c e
    }
  >>
>>
@end lilypond

Well -- yes it does, but perhaps too well.  When we set the
@code{padding} to@tie{}5, LilyPond adds 5@tie{}staff spaces to the
distance between objects, which is too much for us here.  We'll
use value@tie{}2 instead.

@cindex @code{StaffGrouper}
@cindex @code{ChoirStaff} context
@cindex @code{basic-distance} sub-property
@cindex @code{staffgroup-staff-spacing} property

Next, let's move the piano music away from the vocal parts.  The
vocal music is a @code{ChoirStaff} context, so we need to increase the
spacing between that group of staves and the piano staff below.
We'll do this by changing the @code{basic-distance} sub-property of the
@code{StaffGrouper} grob's @code{staffgroup-staff-spacing}.

@lilypond[quote,fragment,ragged-right,verbatim]
<<
  \new ChoirStaff \with {
    \override StaffGrouper
              .staffgroup-staff-spacing.basic-distance = 15
  }
  <<
    \new Staff {
      \new Voice = "music" {
        b'2 c' c' c'
      }
    }
    \new Lyrics \with {
      \override VerticalAxisGroup
                .nonstaff-relatedstaff-spacing.padding = 2
      \override VerticalAxisGroup
                .nonstaff-unrelatedstaff-spacing.padding = 2
    }
    \lyricsto "music" {
      Here are some lyrics
    }
    \new Staff {
      \clef bass e'2 f e c
    }
  >>
  \new PianoStaff
  <<
    \new Staff {
      g''2 c'' c'' a''
    }
    \new Staff {
      \clef bass e2 f c e
    }
  >>
>>
@end lilypond

Excellent.  Now just for the last requirement to make the piano staves
closer together.  To do this, we again alter the properties of the
@code{StaffGrouper} grob, but this time we're going to reduce both
the @code{basic-distance} and the @code{padding}.  We can do this
as shown below.

@lilypond[quote,fragment,ragged-right,verbatim]
<<
  \new ChoirStaff \with {
    \override StaffGrouper
              .staffgroup-staff-spacing.basic-distance = 15
  }
  <<
    \new Staff {
      \new Voice = "music" {
        b'2 c' c' c'
      }
    }
    \new Lyrics \with {
      \override VerticalAxisGroup
                .nonstaff-relatedstaff-spacing.padding = 2
      \override VerticalAxisGroup
                .nonstaff-unrelatedstaff-spacing.padding = 2
    }
    \lyricsto "music" {
      Here are some lyrics
    }
    \new Staff {
      \clef bass e'2 f e c
    }
  >>
  \new PianoStaff \with {
    \override StaffGrouper.staff-staff-spacing =
                #'((basic-distance . 0)
                   (padding . 0))
  }
  <<
    \new Staff {
      g''2 c'' c'' a''
    }
    \new Staff {
      \clef bass e2 f c e
    }
  >>
>>
@end lilypond

That's put them really close together -- but it's what
the publisher wanted.  They could be moved further
apart by altering the @code{padding} or @code{basic-distance}
if wanted.

There are many ways of altering vertical spacing.  A key point
to remember is that the spacing between objects in a
@code{StaffGroup} (like @code{GrandStaff} or
@code{PianoStaff} groups) is controlled by the spacing variables
of the @code{StaffGrouper}. Spacing from ungrouped staves
(like @code{Lyrics} and @code{Staff}) is controlled by the
variables of the @code{VerticalAxisGroup}.  See the
@rnotation{Flexible vertical spacing paper variables} and
@rnotation{Flexible vertical spacing within systems} for more
details.


@node Collisions of objects
@section Collisions of objects

The tips and tricks covered in this section are applications and
extensions of previous parts of this manual.  In particular, you
should be comfortable with the concepts of @dfn{extents},
@dfn{offsets}, and @dfn{alignments}, @pxref{Offsets and
alignments}.


@node Moving objects
@subsection Moving objects

@cindex moving overlapping objects
@cindex moving colliding objects
@cindex moving colliding grobs
@cindex object, moving colliding
@cindex grob, moving colliding

This may come as a surprise, but LilyPond is not perfect.  Some
notation elements can overlap.  This is unfortunate, but in fact
rather rare.  Usually the need to move objects is for clarity or
aesthetic reasons -- they would look better with a little more
or a little less space around them.

There are three main approaches to resolving overlapping
notation.  They should be considered in the following order:

@enumerate
@item
The @strong{direction} of one of the overlapping objects may
be changed using the predefined commands listed earlier for
within-staff objects (see @ref{Within-staff objects}).
Stems, slurs, beams, ties, dynamics, text, and tuplets may be
repositioned easily in this way.  The limitation is that you
have a choice of only two positions, and neither may be
suitable.

@item
The @strong{object properties}, which LilyPond uses when positioning
layout objects, may be modified using @code{\override}.  The
advantages of making changes to this type of property are (a)@tie{}that
some other objects will be moved automatically if necessary to make
room, and (b)@tie{}that the single override can apply to all instances of the
same type of object.  Such properties include:

@itemize

@item
@code{direction}

This has already been covered in some detail --
@pxref{Within-staff objects}.

@item
@code{padding}, @code{right-padding}, @code{staff-padding}

@cindex @code{padding} property
@cindex @code{right-padding} property
@cindex @code{staff-padding} property

As an object is being positioned the value of its @code{padding}
property specifies the gap that must be left between itself and the
nearest edge of the object against which it is being positioned.  Note
that it is the @code{padding} value of the object @strong{being
placed} that is used; the @code{padding} value of the object which is
already placed is ignored.  Gaps specified by @code{padding} can be
applied to all objects that support the
@code{side-position-interface}.

Instead of @code{padding}, the placement of groups of accidentals
is controlled by @code{right-padding}.  This property is to be found
in the @code{AccidentalPlacement} grob which, note, lives in the
@strong{Staff} context.  In the typesetting process the note heads
are typeset first and then the accidentals, if any, are added to the
left of the note heads using the @code{right-padding} property to determine
the separation from the note heads and between individual accidentals.
So only the @code{right-padding} property of the @code{AccidentalPlacement}
grob has any effect on the placement of the accidentals.

The @code{staff-padding} property is closely related to the
@code{padding} property: @code{padding} controls the minimum amount of
space between any object that supports the
@code{side-position-interface} and the nearest other object (generally
the note or the staff lines); @code{staff-padding} applies only to
those objects that are always set outside the staff -- it controls
the minimum distance from the staff to the outside-staff object.
Note that @code{staff-padding} has no effect on
objects that are positioned relative to the note rather than the
staff, even though it may be overridden without error for such objects
-- it is simply ignored.

To discover which padding property is required for the object you wish
to reposition, you need to return to the IR and look up the object's
properties.  Be aware that the padding properties might not be located
in the obvious object, so look in objects that appear to be related.

All padding values are measured in staff spaces.  For most
objects, this value is set by default to be around 1.0 or less
(it varies with each object).  It may be overridden if a larger
(or smaller) gap is required.

@item
@code{self-alignment-X}

@cindex @code{self-alignment-X} property

Use this property to align the object to the left, to
the right, or to center it with respect to the parent object's
reference point.  It may be used with all objects that support
the @code{self-alignment-interface}.  In general these are objects
that contain text.  The values are @code{LEFT}, @code{RIGHT}
or @code{CENTER}.  Alternatively, a numerical value between
@w{@code{-1}} and @code{+1} may be specified, where @w{@code{-1}} is
left-aligned, @code{+1} is right-aligned, and numbers in between
move the text progressively from left-aligned to right-aligned.
Numerical values greater than@tie{}@code{1} may be specified to move
the text even further to the left, or less than @w{@code{-1}} to
move the text even further to the right.  A change of@tie{}@code{1}
in the value corresponds to a movement of half the text's length.

@item
@code{extra-spacing-width}

@cindex @code{extra-spacing-width} property

This property is available for all objects that support the
@code{item-interface}.  It takes two numbers, the first is added
to the left edge of the extent and the second is added to the right edge.
Negative numbers move the edge to the left, positive to
the right, so to widen an object the first number must be negative,
the second positive.  Note that not all objects honor both
numbers.  For example, the @code{Accidental} object only takes
notice of the first (left edge) number.

@item
@code{staff-position}

@cindex @code{staff-position} property

This property is part of the
@code{staff-symbol-referencer-interface}, which is supported by
objects that are positioned relative to the staff.  It specifies
the vertical position of the object relative to the center line
of the staff in half staff spaces.  It is useful in resolving
collisions between layout objects like multi-measure rests, ties,
and notes in different voices.

@item
@code{horizontal-shift}

@cindex @code{horizontal-shift} property
@cindex note column
@cindex note, collision
@cindex collision, notes
@cindex shift commands
@funindex \shiftOff
@funindex \shiftOn
@funindex \shiftOnn
@funindex \shiftOnnn

Within a voice, all the notes occurring at the same musical moment are
grouped into a @dfn{note column}, and a @code{NoteColumn} grob is created
to control the horizontal positioning of that group of notes (see
@qq{Note columns} in @ref{Explicitly instantiating voices}).  If
@emph{and only if} two or more note columns within a single @code{Staff}
context, both with stems in the same direction, occur at the same
musical moment, the values of their @code{horizontal-shift} properties
are used to rank them, and the columns in the higher ranks are
progressively offset to avoid collisions of the note heads.  This
property is set by the @code{\voice@var{XXX}} commands and may be overridden
directly with an @code{\override} command or, more usually, by the
@code{\shiftOn} commands.  Note that this property is used to
@emph{rank} the note columns for off-setting -- it does not specify the
magnitude of the offset, which is progressively increased in steps
based on the note head's width for each rank.  The steps are usually
of half a note head's width, but may be a full note head's width when
a closely spaced group of notes is involved.

@item
@code{force-hshift}

@cindex @code{force-hshift} property

The @code{force-hshift} property is part of @code{NoteColumn} grobs
(actually of the @code{note-column-interface}).  Changing it permits a
note column to be moved in situations where the note columns overlap.
Note that it has no effect on note columns that do not overlap.
It is specified in units appropriate to a note column, viz.@: the note
head width of the first voice note.  It should be used in complex
situations where the normal @code{\shiftOn} commands
(@pxref{Explicitly instantiating voices}) do not resolve the note
conflict satisfactorily.  It is preferable to the @code{extra-offset}
property for this purpose as there is no need to work out the distance
in staff spaces, and moving the notes into or out of a
@code{NoteColumn} affects other actions such as merging note heads.

@end itemize

@item
Finally, when everything else fails, objects may be manually repositioned
relative to the staff center line vertically, or by displacing them by
any distance to a new position.  The disadvantages are that the
correct values for the repositioning have to be worked out, often by
trial and error, for every object individually, and, because the
movement is done after LilyPond has placed all other objects, the user
is responsible for avoiding any collisions that might ensue.  However, the
main difficulty with this approach is that the repositioning values
may need to be reworked if the music is later modified.  The
properties that can be used for this type of manual repositioning
are as follows.

@itemize

@item
@code{extra-offset}

@cindex @code{extra-offset} property

This property applies to any layout object supporting the
@code{grob-interface}.  It takes a pair of numbers that specify the
extra displacement in the horizontal and vertical directions.
Negative numbers move the object to the left or down.  The units are
staff spaces.  The extra displacement is made after the typesetting of
objects is finished, so an object may be repositioned anywhere without
affecting anything else.

@item
@code{positions}

@cindex @code{positions} property

This is most useful for manually adjusting the slope and height
of beams, slurs, and tuplets.  It takes a pair of numbers
giving the position of the left and right ends of the beam, slur,
etc., relative to the center line of the staff.  Units are
staff spaces.  Note, though, that slurs and phrasing slurs cannot
be repositioned by arbitrarily large amounts.  LilyPond first
generates a list of possible positions for the slur and by default
finds the slur that @qq{looks best}.  If the @code{positions}
property has been overridden the slur that is closest to the
requested positions is selected from the list.
@end itemize

@end enumerate

A particular object may not have all of these properties.
It is necessary to go to the IR to look up which properties
are available for the object in question.

Here is a list of the objects which are most likely to be
involved in collisions, together with the name of the object which
should be looked up in the IR in order to discover which properties
should be used to move them.

@indentedblock
@multitable {Dynamics (horizontally)} {@code{DynamicLineSpanner}}
@headitem Object type
  @tab Object name
@item articulations, e.g., @samp{->}
  @tab @code{Script}
@item beams
  @tab @code{Beam}
@item dynamics (vertically)
  @tab @code{DynamicLineSpanner}
@item dynamics (horizontally)
  @tab @code{DynamicText}
@item fingerings
  @tab @code{Fingering}
@item rehearsal marks
  @tab @code{RehearsalMark}
@item text marks
  @tab @code{TextMark}
@item slurs
  @tab @code{Slur}
@item text, e.g., @samp{^"text"}
  @tab @code{TextScript}
@item ties
  @tab @code{Tie}
@item tuplets
  @tab @code{TupletBracket}
@end multitable
@end indentedblock


@node Fixing overlapping notation
@subsection Fixing overlapping notation

Let's now see how the properties in the previous section can
help resolve overlapping notation.


@node The padding property
@unnumberedsubsubsec The @code{padding} property

@cindex padding
@cindex fixing overlapping notation
@cindex overlapping notation

The @code{padding} property can be set to increase
(or decrease) the distance between symbols that are printed
above or below notes.

@cindex @code{Script}, example of overriding
@cindex @code{padding} property, example

@lilypond[quote,fragment,verbatim]
c'2\fermata
\override Script.padding = 3
b2\fermata
@end lilypond

@cindex @code{MetronomeMark}, example of overriding
@cindex @code{padding} property, example

@lilypond[quote,fragment,verbatim]
% This will not work, see below
\override MetronomeMark.padding = 3
\tempo 4 = 120
c'1 |
% This works
\override Score.MetronomeMark.padding = 3
\tempo 4 = 80
d'1 |
@end lilypond

Note in the second example how important it is to figure out what
context handles a certain object.  Since the @code{MetronomeMark}
grob is handled in the @code{Score} context, property changes in the
@code{Voice} context will not be noticed.  For more details, see
@rnotation{Modifying properties}.

If the @code{padding} property of an object is increased when that
object is in a stack of objects being positioned according to
their @code{outside-staff-priority}, then that object and all
objects outside of it are moved.


@node The right-padding property
@unnumberedsubsubsec The @code{right-padding} property

@cindex @code{right-padding} property

The @code{right-padding} property affects the spacing between the
accidental and the note to which it applies.@footnote{The
@code{DynamicText} grob also has a @code{right-padding} property
but with a completely different function: it controls the
horizontal space between an absolute dynamic and a dynamic text
span that starts at the same moment.}  It is not often required,
but it can help in horizontally tight situations, for example.

@cindex @code{AccidentalPlacement}, example of overriding
@cindex @code{right-padding} property, example

@lilypond[quote,ragged-right,verbatim]
\relative c' {
  cis8[ dis eis fis gis ais bis cis]
}

\relative c' {
  % move accents nearer to note heads
  \override Score.AccidentalPlacement.right-padding = -0.1
  cis8[ dis eis fis gis ais bis cis]
}
@end lilypond


@node The staff-padding property
@unnumberedsubsubsec The @code{staff-padding} property

@cindex aligning object, on a baseline
@cindex object, aligning on a baseline

@code{staff-padding} can be used to align objects such as dynamics
along a baseline at a fixed distance from the staff, when no other
notation forces them further from the staff.
It is not a property of @code{DynamicText} but of
@code{DynamicLineSpanner}.  This is because the baseline should apply
equally to @strong{all} dynamics, including those created as extended
spanners.  So this is the way to align the dynamic marks in the
example taken from the previous section:

@cindex @code{DynamicText}, example of overriding
@cindex @code{extra-spacing-width} property, example
@cindex @code{DynamicLineSpanner}, example of overriding
@cindex @code{staff-padding} property, example

@lilypond[quote,fragment,ragged-right,verbatim]
\override DynamicLineSpanner.staff-padding = 3
\relative { a'4\f b\mf a\p b\mp }
@end lilypond


@node The self-alignment-X property
@unnumberedsubsubsec The @code{self-alignment-X} property

The following example shows how to adjust the position
of a string fingering object relative to a note's stem by aligning the
right edge with the reference point of the parent note:

@cindex @code{StringNumber}, example of overriding
@cindex @code{self-alignment-X} property, example

@lilypond[quote,fragment,ragged-right,verbatim]
\voiceOne
<a''\2>
\once \override StringNumber.self-alignment-X = #RIGHT
<a''\2>
@end lilypond

@node The staff-position property
@unnumberedsubsubsec The @code{staff-position} property

@cindex object, collision within a staff

Multi-measure rests in one voice can collide with notes in another.
Since these rests are typeset centered between the bar lines, it
would require significant effort for LilyPond to figure out which
other notes might collide with it, since all the current collision
handling between notes and between notes and rests is done only
for notes and rests that occur at the same time.  Here's an
example of a collision of this type:

@lilypond[quote,verbatim,ragged-right]
<< \relative { c'4 c c c } \\ { R1 } >>
@end lilypond

The best solution here is to move the multi-measure rest down, since the
rest is in the lower voice. The default in @code{\voiceTwo} (i.e., in the
second voice of a @code{<<@{@dots{}@} \\ @{@dots{}@}>>} construct) is
that @code{staff-position} is set to @w{-6} for @code{MultiMeasureRest}, so we need
to move it, say, four half-staff spaces down to @w{-10}.

@cindex @code{MultiMeasureRest}, example of overriding
@cindex @code{staff-position} property, example

@lilypond[quote,verbatim,ragged-right]
<<
  \relative { c'4 c c c }
  \\
  \override MultiMeasureRest.staff-position = -10
  { R1 }
>>
@end lilypond

This is better than using, for example, @code{extra-offset},
because the ledger line above the rest is inserted automatically.

Note that there is a peculiarity with this property if applied to
a @code{Tie} grob: LilyPond distinguishes between @dfn{exact}
numbers (like integers and fractions) and @dfn{inexact} numbers
(like numbers with a decimal point).  See @rnotation{Engraving
ties manually} for the difference between exact and inexact
values.

@node The extra-offset property
@unnumberedsubsubsec The @code{extra-offset} property

@cindex positioning objects
@cindex positioning grobs
@cindex object, positioning
@cindex grob, positioning

The @code{extra-offset} property provides complete control over the
positioning of an object both horizontally and vertically.

In the following example, the second fingering is moved a little to
the left, and 1.8 staff space downwards:

@cindex @code{Fingering}, example of overriding
@cindex @code{extra-offset} property, example

@lilypond[quote,fragment,verbatim]
f'4-5
\once \override Fingering.extra-offset = #'(-0.3 . -1.8)
f'4-5
@end lilypond


@node The positions property
@unnumberedsubsubsec The @code{positions} property

@cindex controlling tuplets, slurs, phrasing slurs, and beams manually
@cindex manually controlling tuplets, slurs, phrasing slurs, and beams
@cindex tuplet beam, controlling manually
@cindex slur, controlling manually
@cindex phrasing slur, controlling manually
@cindex beam, controlling manually

The @code{positions} property allows the vertical position and hence
the slope of tuplets, slurs, phrasing slurs, and beams to be controlled
manually.

Here's an example in which the phrasing slur and slur collide:

@lilypond[quote,verbatim,ragged-right]
\relative { a'8 \( ( a'16 ) a \) }
@end lilypond

@cindex @code{PhrasingSlur}, example of overriding
@cindex @code{positions} property, example

@noindent
One possibility would be to move the two ends of the phrasing slur
higher.  We can try setting the left end to 2.5 staff spaces above the
center line and the right end to 4.5 above, and LilyPond will
select the phrasing slur from the candidates it has found with its
end points closest to these:

@lilypond[quote,verbatim,fragment,ragged-right]
\once \override PhrasingSlur.positions = #'(2.5 . 4.5)
a'8 \( ( a''16 ) a'' \)
@end lilypond

This is an improvement, but why not lower the right end of the slur
a little?  If you try it you'll find it can't be done in this way.
That's because there are no candidate slurs lower than the one
already selected, and in this case the @code{positions} property has
no effect.  However, ties, slurs and phrasing slurs @emph{can} be
positioned and shaped very precisely when necessary.  To learn how to
do this, see @rnotation{Modifying ties and slurs}.

Here's a further example.  We see that the beams
collide with the ties:

@lilypond[quote,verbatim,ragged-right]
{
  \time 4/2
  <<
    \relative { c'1~ 2. e8 f }
    \\
    \relative {
      e''8 e e e
      e e e e
      f2 g
    }
  >>
  <<
    \relative { c'1~ 2. e8 f }
    \\
    \relative {
      e''8 e e e
      e e e e
      f2 g
    }
  >>
}
@end lilypond

@noindent
This can be resolved by manually moving both ends of the beam
up from their position at 1.81 staff spaces below the center line to,
say, 1:

@cindex @code{Beam}, example of overriding
@cindex @code{positions} property, example

@lilypond[quote,verbatim,ragged-right]
{
  \time 4/2
  <<
    \relative { c'1~ 2. e8 f }
    \\
    \relative {
      \override Beam.positions = #'(-1 . -1)
      e''8 e e e
      e e e e
      f2 g
    }
  >>
  <<
    \relative { c'1~ 2. e8 f }
    \\
    \relative {
      e''8 e e e
      e e e e
      f2 g
      \revert Beam.positions
    }
  >>
}
@end lilypond

@noindent
Note that the override continues to apply in the second voice of
the second measure of eighth notes, but not to any of the beams in the
first voice, even those in the later second measure.  As soon as the
override should no longer apply it should be reverted, as shown.

@node The force-hshift property
@unnumberedsubsubsec The @code{force-hshift} property

We can now see how to apply the final corrections to the Chopin
example introduced at the end of @ref{I'm hearing voices}, which
was left looking like this:

@lilypond[quote,verbatim,fragment,ragged-right]
\new Staff \relative {
  \key aes \major
  <<
    { c''2 aes4. bes8 }
    \\
    { <ees, c>2 des }
    \\
    \\
    { aes'2 f4 fes }
  >> |
  <c ees aes c>1 |
}
@end lilypond

@noindent
The inner note of the first chord (i.e., the A-flat in the fourth
Voice) need not be shifted away from the note column of the higher
note, so we use @code{\shiftOff}.

In the second chord we prefer the F to line up with the A-flat and
the lowest note to be positioned slightly right to avoid a collision
of stems.  We achieve this by setting @code{force-hshift} in the
@code{NoteColumn} of the low D-flat to move it to the right by half
a staff space, and setting @code{force-hshift} for the F to zero.
Note that we use @code{\once} to avoid the settings propagating
beyond the immediate musical moment, although in this small example
the @code{\once} and the second @code{\override} in Voice four could
be omitted.  This would not be good practice.

Here's the final result:

@cindex @code{NoteColumn}, example of overriding
@cindex @code{force-hshift} property, example

@lilypond[quote,verbatim,fragment,ragged-right]
\new Staff \relative {
  \key aes \major
  <<
    { c''2 aes4. bes8 }
    \\
    { <ees, c>2 \once \override NoteColumn.force-hshift = 0.5 des }
    \\
    \\
    { \once \shiftOff aes'2 \once \shiftOff f4 fes }
  >> |
  <c ees aes c>1 |
}
@end lilypond


@node Real music example
@subsection Real music example

We end this section on tweaks by showing the steps to be taken to
deal with a tricky example, which needs several adjustments to produce
the desired output.  The example has been deliberately chosen to
illustrate the use of the Notation Reference to resolve unusual
problems with notation.  It is not representative of the more usual
engraving process, so please do not let these difficulties put
you off!  Fortunately, difficulties like these are not very common.

The example is from Chopin's @emph{Première Ballade}, Op. 23, bars 6
to@tie{}9, the transition from the opening @q{Lento} to
@q{Moderato}.@footnote{This is taken from an older edition that
deviates from the Urtext version as shown by Henle, for example.}
Here, first, is what we want the output to look like, but to avoid
over-complicating the example too much we have left out the
dynamics, fingering and pedalling, and we aren't going to tweak the
appearance as much as would be possible.

@c The following should appear as music without code
@c This example should not be indexed
@c line-width ensures no break
@lilypond[quote,ragged-right,line-width=6\in]
rhMusic = \relative {
  \new Voice {
    r2 c''4.\( g8 |
    \once \override Tie.staff-position = 3.5
    bes1~ |
    \bar "||"
    \time 6/4
    \mergeDifferentlyHeadedOn
    \mergeDifferentlyDottedOn
    bes2.\tempo "Moderato" r8
    <<
      { c,8 d fis bes a }
      \new Voice {
        \voiceTwo
        c,8~
        % Reposition the c2 to the right of the merged note
        \once \override NoteColumn.force-hshift = 1.0
        % Move the c2 out of the main note column
        % so the merge will work
        \shiftOnn
        c2
      }
      \new Voice {
        \voiceThree
        s8
        % Stem on the d2 must be down to permit merging
        \stemDown
        % Stem on the d2 should be invisible
        \tweak Stem.transparent ##t
        d2
      }
      \new Voice {
        \voiceFour
        s4 fis4.
      }
    >> |
    \mergeDifferentlyHeadedOff
    \mergeDifferentlyDottedOff
    g2.\)
  }
}

lhMusic = \relative {
  r2 <c' g ees>2( |
  <d g, d>1)\arpeggio |
  r2. d,,4 r4 r |
  r4
}

\score {
  \new PianoStaff <<
    \new Staff = "RH"  <<
      \key g \minor
      \rhMusic
    >>
    \new Staff = "LH" <<
      \key g \minor
      \clef "bass"
      \lhMusic
    >>
  >>
}
@end lilypond

We note first that the right hand part in the third bar
requires four voices.  These are the five beamed eighth notes,
the tied@tie{}C, the half-note@tie{}D which is merged with the eighth
note@tie{}D, and the dotted quarter note F-sharp, which is also merged with
the eighth note at the same pitch.  Everything else is in a single
voice, so the easiest way is to introduce these extra three voices
temporarily at the time they are needed.  If you have forgotten
how to do this, @pxref{I'm hearing voices} and @ref{Explicitly
instantiating voices}.  Here we choose to use explicitly instantiated
voices for the polyphonic passage, as LilyPond is better able to
avoid collisions if all voices are instantiated explicitly in this
way.

So let us begin by entering the notes as two variables, setting up
the staff structure in a score block, and seeing what LilyPond
produces by default:

@c line-width ensures no break
@lilypond[quote,verbatim,ragged-right,line-width=6\in]
rhMusic = \relative {
  \new Voice {
    r2 c''4. g8 |
    bes1~ |
    \time 6/4
    bes2. r8
    % Start polyphonic section of four voices
    <<
      { c,8 d fis bes a }  % continuation of main voice
      \new Voice {
        \voiceTwo
        c,8~ 2
      }
      \new Voice {
        \voiceThree
        s8 d2
      }
      \new Voice {
        \voiceFour
        s4 fis4.
      }
    >> |
    g2.  % continuation of main voice
  }
}

lhMusic = \relative {
  r2 <c' g ees>2 |
  <d g, d>1 |
  r2. d,,4 r4 r |
  r4
}

\score {
  \new PianoStaff <<
    \new Staff = "RH"  <<
      \key g \minor
      \rhMusic
    >>
    \new Staff = "LH" <<
      \key g \minor
      \clef "bass"
      \lhMusic
    >>
  >>
}
@end lilypond

All the notes are right, but the appearance is far from
satisfactory.  The tie collides with the change in time signature,
some notes are not merged together, and several notation elements
are missing.  Let's first deal with the easier things.  We can
easily add the left hand slur and the right hand phrasing slur,
since these were all covered in the Tutorial.  Doing this gives:

@c line-width ensures no break
@lilypond[quote,verbatim,ragged-right,line-width=6\in]
rhMusic = \relative {
  \new Voice {
    r2 c''4.\( g8 |
    bes1~ |
    \time 6/4
    bes2. r8
    % Start polyphonic section of four voices
    <<
      { c,8 d fis bes a }  % continuation of main voice
      \new Voice {
        \voiceTwo
        c,8~ 2
      }
      \new Voice {
        \voiceThree
        s8 d2
      }
      \new Voice {
        \voiceFour
        s4 fis4.
      }
    >> |
    g2.\)  % continuation of main voice
  }
}

lhMusic = \relative {
  r2 <c' g ees>2( |
  <d g, d>1) |
  r2. d,,4 r4 r |
  r4
}

\score {
  \new PianoStaff <<
    \new Staff = "RH"  <<
      \key g \minor
      \rhMusic
    >>
    \new Staff = "LH" <<
      \key g \minor
      \clef "bass"
      \lhMusic
    >>
  >>
}
@end lilypond

The first bar is now correct.  The second bar contains an arpeggio and
is terminated by a double bar line.  How do we do these, as they have
not been mentioned in this Learning Manual?  This is where we need to
turn to the Notation Reference.  Looking up @q{arpeggio} and @q{bar
line} in the index quickly shows us that an arpeggio is produced by
appending @code{\arpeggio} to a chord, and a double bar line is
produced by the @code{\bar "||"} command.  That's easily done.  We
next need to correct the collision of the tie with the time signature.
This is best done by moving the tie upwards.@footnote{An
alternative would be to adjust @code{Tie.details.height-limit},
making the tie more concave.}  Moving objects was
covered earlier in @ref{Moving objects}, which says that objects
positioned relative to the staff can be moved vertically by overriding
their @code{staff-position} property, which is specified in half
staff-space units relative to the center line of the staff.  So the following
override placed just before the first tied note would move the tie up
to 3.5 half staff spaces above the center line:

@code{\once \override Tie.staff-position = 3.5}

This completes bar two, giving:

@c line-width ensures no break
@lilypond[quote,verbatim,ragged-right,line-width=6\in]
rhMusic = \relative {
  \new Voice {
    r2 c''4.\( g8 |
    \once \override Tie.staff-position = 3.5
    bes1~ |
    \bar "||"
    \time 6/4
    bes2. r8
    % Start polyphonic section of four voices
    <<
      { c,8 d fis bes a }  % continuation of main voice
      \new Voice {
        \voiceTwo
        c,8~ 2
      }
      \new Voice {
        \voiceThree
        s8 d2
      }
      \new Voice {
        \voiceFour
        s4 fis4.
      }
    >> |
    g2.\)  % continuation of main voice
  }
}

lhMusic = \relative {
  r2 <c' g ees>2( |
  <d g, d>1)\arpeggio |
  r2. d,,4 r4 r |
  r4
}

\score {
  \new PianoStaff <<
    \new Staff = "RH"  <<
      \key g \minor
      \rhMusic
    >>
    \new Staff = "LH" <<
      \key g \minor
      \clef "bass"
      \lhMusic
    >>
  >>
}
@end lilypond

On to bar three and the start of the Moderato section.  The tutorial
showed how to add a tempo indication with the @code{\tempo} command, so
adding @qq{Moderato} is easy.  But how do we merge notes in
different voices together?  This is where we need to turn again to
the Notation Reference for help.  A search for @qq{merge} in the
Notation Reference index quickly leads us to the commands for merging
differently headed and differently dotted notes in
@rnotation{Collision resolution}.  In our example we need to merge both
types of note for the duration of the polyphonic section in bar 3,
so using the information we find in the Notation Reference we add

@example
\mergeDifferentlyHeadedOn
\mergeDifferentlyDottedOn
@end example

@noindent
to the start of that section and

@example
\mergeDifferentlyHeadedOff
\mergeDifferentlyDottedOff
@end example

@noindent
to the end, giving:

@c line-width ensures no break
@lilypond[quote,ragged-right,line-width=6\in]
rhMusic = \relative {
  \new Voice {
    r2 c''4.\( g8 |
    \once \override Tie.staff-position = 3.5
    bes1~ |
    \bar "||"
    \time 6/4
    bes2.\tempo "Moderato" r8
    \mergeDifferentlyHeadedOn
    \mergeDifferentlyDottedOn
    % Start polyphonic section of four voices
    <<
      { c,8 d fis bes a }  % continuation of main voice
      \new Voice {
        \voiceTwo
        c,8~ 2
      }
      \new Voice {
        \voiceThree
        s8 d2
      }
      \new Voice {
        \voiceFour
        s4 fis4.
      }
    >> |
    \mergeDifferentlyHeadedOff
    \mergeDifferentlyDottedOff
    g2.\)  % continuation of main voice
  }
}

lhMusic = \relative {
  r2 <c' g ees>2( |
  <d g, d>1)\arpeggio |
  r2. d,,4 r4 r |
  r4
}

\score {
  \new PianoStaff <<
    \new Staff = "RH"  <<
      \key g \minor
      \rhMusic
    >>
    \new Staff = "LH" <<
      \key g \minor
      \clef "bass"
      \lhMusic
    >>
  >>
}
@end lilypond

These overrides have merged the two F-sharp notes, but not the two
on@tie{}D.  Why not?  The answer is there in the same section in the
Notation Reference -- notes being merged must have stems in
opposite directions and two notes cannot be merged successfully if
there is a third note in the same note column.  Here the two D's
both have upward stems and there is a third note -- the@tie{}C.  We know
how to change the stem direction using @code{\stemDown}, and
the Notation Reference also says how to move the@tie{}C -- apply a shift
using one of the @code{\shift} commands.  But which one?
The@tie{}C is in voice two which has shift off, and the two D's are in
voices one and three, which have shift off and shift on,
respectively.  So we have to shift the@tie{}C a further level still
using @code{\shiftOnn} to avoid it interfering with the two D's.
Applying these changes gives:

@cindex @code{Tie}, example of overriding
@cindex @code{staff-position} property, example

@c line-width ensures no break
@lilypond[quote,verbatim,ragged-right,line-width=6\in]
rhMusic = \relative {
  \new Voice {
    r2 c''4.\( g8 |
    \once \override Tie.staff-position = 3.5
    bes1~ |
    \bar "||"
    \time 6/4
    bes2.\tempo "Moderato" r8
    \mergeDifferentlyHeadedOn
    \mergeDifferentlyDottedOn
    % Start polyphonic section of four voices
    <<
      { c,8 d fis bes a }  % continuation of main voice
      \new Voice {
        \voiceTwo
        % Move the c2 out of the main note column
        % so the merge will work
        c,8~ \shiftOnn c2
      }
      \new Voice {
        \voiceThree
        % Stem on the d2 must be down to permit merging
        s8 \stemDown d2
      }
      \new Voice {
        \voiceFour
        s4 fis4.
      }
    >> |
    \mergeDifferentlyHeadedOff
    \mergeDifferentlyDottedOff
    g2.\)  % continuation of main voice
  }
}

lhMusic = \relative {
  r2 <c' g ees>2( |
  <d g, d>1)\arpeggio |
  r2. d,,4 r4 r |
  r4
}

\score {
  \new PianoStaff <<
    \new Staff = "RH"  <<
      \key g \minor
      \rhMusic
    >>
    \new Staff = "LH" <<
      \key g \minor
      \clef "bass"
      \lhMusic
    >>
  >>
}
@end lilypond

Nearly there.  Only two problems remain: The downward stem on the
merged@tie{}D should not be there, and the@tie{}C would be better positioned
to the right of the D's.  We know how to do both of these from the
earlier tweaks: we make the stem transparent, and move the@tie{}C with
the @code{force-hshift} property.  Here's the final result:

@cindex @code{NoteColumn}, example of overriding
@cindex @code{force-hshift} property, example
@cindex @code{Stem}, example of overriding
@cindex @code{transparent} property, example

@c line-width ensures no break
@lilypond[quote,verbatim,ragged-right,line-width=6\in]
rhMusic = \relative {
  \new Voice {
    r2 c''4.\( g8 |
    \once \override Tie.staff-position = 3.5
    bes1~ |
    \bar "||"
    \time 6/4
    bes2.\tempo "Moderato" r8
    \mergeDifferentlyHeadedOn
    \mergeDifferentlyDottedOn
    % Start polyphonic section of four voices
    <<
      { c,8 d fis bes a }  % continuation of main voice
      \new Voice {
        \voiceTwo
        c,8~
        % Reposition the c2 to the right of the merged note
        \once \override NoteColumn.force-hshift = 1.0
        % Move the c2 out of the main note column
        % so the merge will work
        \shiftOnn
        c2
      }
      \new Voice {
        \voiceThree
        s8
        % Stem on the d2 must be down to permit merging
        \stemDown
        % Stem on the d2 should be invisible
        \tweak Stem.transparent ##t
        d2
      }
      \new Voice {
        \voiceFour
        s4 fis4.
      }
    >> |
    \mergeDifferentlyHeadedOff
    \mergeDifferentlyDottedOff
    g2.\)  % continuation of main voice
  }
}

lhMusic = \relative {
  r2 <c' g ees>2( |
  <d g, d>1)\arpeggio |
  r2. d,,4 r4 r |
  r4
}

\score {
  \new PianoStaff <<
    \new Staff = "RH"  <<
      \key g \minor
      \rhMusic
    >>
    \new Staff = "LH" <<
      \key g \minor
      \clef "bass"
      \lhMusic
    >>
  >>
}
@end lilypond


@node Further tweaking
@section Further tweaking

We have almost completed our introduction into tweaking, and only
some topics remain that we would like to discuss.


@node Other uses for tweaks
@subsection Other uses for tweaks

Here we describe situations that occur rather frequently in
real-world scores.


@cindex removing objects
@cindex object, removing

@node Tying notes across voices
@unnumberedsubsubsec Tying notes across voices

@cindex tying notes across voices

The following example demonstrates how to connect notes in different
voices using ties.  Normally, only notes in the same voice can be
connected with ties.  By using two voices, with the tied notes in one
of them

@lilypond[quote]
<< { b'8~ 8\noBeam } \\ { b'8[ g'] } >>
@end lilypond

@noindent
and removing the first up-stem and its flag in that voice, the tie
appears to cross voices:

@funindex \omit
@cindex @code{Stem}, example of removing
@cindex @code{Flag}, example of removing
@cindex @code{\omit}, example

@lilypond[quote,verbatim]
<<
  {
    \once \omit Stem
    \once \omit Flag
    b'8~ 8\noBeam
  }
\\
  { b'8[ g'] }
>>
@end lilypond

@morerefs
Learning Manual:
@ref{The once prefix},
@ref{The stencil property}.
@endmorerefs


@node Simulating a fermata in MIDI
@unnumberedsubsubsec Simulating a fermata in MIDI

@cindex fermata, implementing in MIDI

For outside-staff objects it is usually better to override the
object's @code{stencil} property rather than its @code{transparent}
property when you wish to remove it from the printed output.
Setting the @code{stencil} property of an object to @code{#f} will
remove that object entirely from the printed output.  This means it
has no effect on the placement of other objects placed relative to
it.

For example, if we wished to change the metronome setting in order
to simulate a fermata in the MIDI output we would not want the
metronome markings to appear in the printed output, and we would
not want it to influence the spacing between the two systems or
the positions of adjacent annotations on the staff.  So setting
its @code{stencil} property to @code{#f} would be the best way.
We show here the effect of the two methods:

@cindex @code{MetronomeMark}, example of overriding
@cindex @code{transparent} property, example

@lilypond[quote,verbatim,ragged-right]
\score {
  \relative {
    % Visible tempo marking
    \tempo 4=120
    a'4 a a
    \once \hide Score.MetronomeMark
    % Invisible tempo marking to lengthen fermata in MIDI
    \tempo 4=80
    a4\fermata |
    % New tempo for next section
    \tempo 4=100
    a4 a a a |
  }
  \layout { }
  \midi { }
}
@end lilypond

@cindex @code{MetronomeMark}, example of overriding
@cindex @code{stencil} property, example

@lilypond[quote,verbatim,ragged-right]
\score {
  \relative {
    % Visible tempo marking
    \tempo 4=120
    a'4 a a
    \once \omit Score.MetronomeMark
    % Invisible tempo marking to lengthen fermata in MIDI
    \tempo 4=80
    a4\fermata |
    % New tempo for next section
    \tempo 4=100
    a4 a a a |
  }
  \layout { }
  \midi { }
}
@end lilypond

@noindent
Both methods remove the metronome mark which lengthens the fermata
from the printed output, and both affect the MIDI timing as
required, but the transparent metronome mark in the first line
forces the following tempo indication too high while the
second (with the stencil removed) does not.

@morerefs
Music Glossary:
@rglos{system}.
@endmorerefs


@node Using variables for layout adjustments
@subsection Using variables for layout adjustments

@cindex variable, using for overrides
@cindex override, using variable for
@cindex adjustment, using variable for
@cindex layout adjustment, using variable for

Override commands are often long and tedious to type, and they
have to be absolutely correct.  If the same overrides are to be
used many times it may be worth defining variables to hold them.

Suppose we wish to emphasize certain words in lyrics by printing
them in bold italics.  The @code{\italic} and @code{\bold}
commands only work within lyrics if they are embedded, together with
the word or words to be modified, within a @code{\markup} block,
which makes them tedious to enter.  The need to embed the words
themselves prevents their use in simple variables.  As an
alternative can we use @code{\override} and @code{\revert} commands?

@example
\override Lyrics.LyricText.font-shape = #'italic
\override Lyrics.LyricText.font-series = #'bold

\revert Lyrics.LyricText.font-shape
\revert Lyrics.LyricText.font-series
@end example

These would also be extremely tedious to enter if there were many
words requiring emphasis.  But we @emph{can} define these as two
variables and use those to bracket the words to be emphasized.
Another advantage of using variables for these overrides is that
the spaces around the dot are not necessary, since they are not
being interpreted in @code{\lyricmode} directly.  Here's an example
of this, although in practice  we would choose shorter names
for the variables to make them quicker to type:

@cindex @code{LyricText}, example of overriding
@cindex @code{font-shape} property, example
@cindex @code{font-series} property, example

@lilypond[quote,verbatim]
emphasize = {
  \override Lyrics.LyricText.font-shape = #'italic
  \override Lyrics.LyricText.font-series = #'bold
}

normal = {
  \revert Lyrics.LyricText.font-shape
  \revert Lyrics.LyricText.font-series
}

global = { \key c \major \time 4/4 \partial 4 }

SopranoMusic = \relative { c'4 | e4. e8 g4 g    | a4   a   g  }
AltoMusic    = \relative { c'4 | c4. c8 e4 e    | f4   f   e  }
TenorMusic   = \relative  { e4 | g4. g8 c4.  b8 | a8 b c d e4 }
BassMusic    = \relative  { c4 | c4. c8 c4 c    | f8 g a b c4 }

VerseOne = \lyricmode {
  E -- | ter -- nal \emphasize Fa -- ther, | \normal strong to save,
}

VerseTwo = \lyricmode {
  O | \once \emphasize Christ, whose voice the | wa -- ters heard,
}

VerseThree = \lyricmode {
  O | \emphasize Ho -- ly Spi -- rit, | \normal who didst brood
}

VerseFour = \lyricmode {
  O | \emphasize Tri -- ni -- ty \normal of | love and pow'r
}

\score {
  \new ChoirStaff <<
    \new Staff <<
      \clef "treble"
      \new Voice = "Soprano"  { \voiceOne \global \SopranoMusic }
      \new Voice = "Alto" { \voiceTwo \AltoMusic }
      \new Lyrics \lyricsto "Soprano" { \VerseOne }
      \new Lyrics \lyricsto "Soprano" { \VerseTwo }
      \new Lyrics \lyricsto "Soprano" { \VerseThree }
      \new Lyrics \lyricsto "Soprano" { \VerseFour }
    >>
    \new Staff <<
      \clef "bass"
      \new Voice = "Tenor" { \voiceOne \TenorMusic }
      \new Voice = "Bass"  { \voiceTwo \BassMusic }
    >>
  >>
}
@end lilypond


@node Style sheets
@subsection Style sheets

The output that LilyPond produces can be heavily modified; see
@ref{Tweaking output}, for details.  But what if you have many
input files that you want to apply your tweaks to?  Or what if you
simply want to separate your tweaks from the actual music?  This
is quite easy to do.

Let's look at an example.  Don't worry if you don't understand
the parts with all the @code{#(@dots{})}.  This is explained in
@ref{Advanced tweaks with Scheme}.

@lilypond[quote,verbatim,ragged-right]
mpdolce =
  \tweak self-alignment-X -0.6
  #(make-dynamic-script
    #{ \markup { \dynamic mp \normal-text \italic \bold dolce } #})

inst =
#(define-music-function
     (string)
     (string?)
   #{ <>^\markup \bold \box #string #})

\relative {
  \tempo 4=50
  a'4.\mpdolce d8 cis4--\glissando a |
  b4 bes a2 |
  \inst "Clarinet"
  cis4.\< d8 e4 fis |
  g8(\! fis)-. e( d)-. cis2 |
}
@end lilypond

Let's do something about the @code{mpdolce} and @code{inst} definitions.
They produce the output we desire, but we might want to use them in
another piece.  We could simply copy-and-paste them at the top of every
file, but that's an annoyance.  It also leaves those definitions in our
input files, and I personally find all the @code{#()} somewhat ugly.
Let's hide them in another file:

@example
%%% save this to a file called "definitions.ily"
mpdolce =
  \tweak self-alignment-X -0.6
  #(make-dynamic-script
    #@{ \markup @{ \dynamic mp \normal-text \italic \bold dolce @} #@})

inst =
#(define-music-function
     (string)
     (string?)
   #@{ <>^\markup \bold \box #string #@})
@end example

We will refer to this file using the @code{\include} command near
the top of the music file.  (The extension @file{.ily} is used to
distinguish this included file, which is not meant to be compiled
on its own, from the main file.)
Now let's modify our music (let's save this file as @file{music.ly}).

@c  We have to do this awkward example/lilypond-non-verbatim
@c  because we can't do the \include stuff in the manual.

@example
\include "definitions.ily"

\relative @{
  \tempo 4=50
  a'4.\mpdolce d8 cis4--\glissando a |
  b4 bes a2 |
  \inst "Clarinet"
  cis4.\< d8 e4 fis |
  g8(\! fis)-. e( d)-. cis2 |
@}
@end example

@lilypond[quote,ragged-right]
mpdolce =
  \tweak self-alignment-X -0.6
  #(make-dynamic-script
    #{ \markup { \dynamic mp \normal-text \italic \bold dolce } #})

inst =
#(define-music-function
     (string)
     (string?)
   #{ <>^\markup \bold \box #string #})

\relative {
  \tempo 4=50
  a'4.\mpdolce d8 cis4--\glissando a |
  b4 bes a2 |
  \inst "Clarinet"
  cis4.\< d8 e4 fis |
  g8(\! fis)-. e( d)-. cis2 |
}
@end lilypond

That looks better, but let's make a few changes.  The glissando is hard
to see, so let's make it thicker and closer to the note heads.  Let's
put the metronome marking above the clef, instead of over the first
note.  And finally, my composition professor hates @q{C} time signatures,
so we'd better make that @q{4/4} instead.

Don't change @file{music.ly}, though.  Replace our @file{definitions.ily}
with this:

@example
%%%  definitions.ily
mpdolce =
  \tweak self-alignment-X -0.6
  #(make-dynamic-script
    #@{ \markup @{ \dynamic mp \normal-text \italic \bold dolce @} #@})

inst =
#(define-music-function
     (string)
     (string?)
   #@{ <>^\markup \bold \box #string #@})

\layout@{
  \context @{
    \Score
    \override MetronomeMark.extra-offset = #'(-5 . 0)
    \override MetronomeMark.padding = 3
  @}
  \context @{
    \Staff
    \numericTimeSignature
  @}
  \context @{
    \Voice
    \override Glissando.thickness = 3
    \override Glissando.gap = 0.1
  @}
@}
@end example

@lilypond[quote,ragged-right]
mpdolce =
  \tweak self-alignment-X -0.6
  #(make-dynamic-script
    #{ \markup { \dynamic mp \normal-text \italic \bold dolce } #})

inst =
#(define-music-function
     (string)
     (string?)
   #{ <>^\markup \bold \box #string #})

\layout{
  \context {
    \Score
    \override MetronomeMark.extra-offset = #'(-5 . 0)
    \override MetronomeMark.padding = 3
  }
  \context {
    \Staff
    \numericTimeSignature
  }
  \context {
    \Voice
    \override Glissando.thickness = 3
    \override Glissando.gap = 0.1
  }
}

\relative {
  \tempo 4=50
  a'4.\mpdolce d8 cis4--\glissando a |
  b4 bes a2 |
  \inst "Clarinet"
  cis4.\< d8 e4 fis |
  g8(\! fis)-. e( d)-. cis2 |
}
@end lilypond

That looks nicer!  But now suppose that I want to publish this
piece.  My composition professor doesn't like @q{C} time
signatures, but I'm somewhat fond of them.  Let's copy the
current @file{definitions.ily} to @file{web-publish.ily} and
modify that.  Since this music is aimed at producing a pdf which
will be displayed on the screen, we'll also increase the
overall size of the output.

@example
%%%  web-publish.ily
mpdolce =
  \tweak self-alignment-X -0.6
  #(make-dynamic-script
    #@{ \markup @{ \dynamic mp \normal-text \italic \bold dolce @} #@})

inst =
#(define-music-function
     (string)
     (string?)
   #@{ <>^\markup \bold \box #string #@})

#(set-global-staff-size 23)

\layout@{
  \context @{
    \Score
    \override MetronomeMark.extra-offset = #'(-5 . 0)
    \override MetronomeMark.padding = 3
  @}
  \context @{
    \Staff
  @}
  \context @{
    \Voice
    \override Glissando.thickness = 3
    \override Glissando.gap = 0.1
  @}
@}
@end example

@lilypond[quote,ragged-right]
mpdolce =
  \tweak self-alignment-X -0.6
  #(make-dynamic-script
    #{ \markup { \dynamic mp \normal-text \italic \bold dolce } #})

inst =
#(define-music-function
     (string)
     (string?)
   #{ <>^\markup \bold \box #string #})

#(set-global-staff-size 23)

\layout{
  \context { \Score
    \override MetronomeMark.extra-offset = #'(-5 . 0)
    \override MetronomeMark.padding = 3
  }
  \context { \Voice
    \override Glissando.thickness = 3
    \override Glissando.gap = 0.1
  }
}

\relative {
  \tempo 4=50
  a'4.\mpdolce d8 cis4--\glissando a |
  b4 bes a2 |
  \inst "Clarinet"
  cis4.\< d8 e4 fis |
  g8(\! fis)-. e( d)-. cis2 |
}
@end lilypond

Now in our music, I simply replace
@code{\include "definitions.ily"} with
@code{\include "web-publish.ily"}.  Of course, we could make this
even more convenient.  We could make a @file{definitions.ily} file that
contains only the definitions of @code{\mpdolce} and @code{\inst}, a
@file{web-publish.ily} file that contains only the @code{\layout}
section listed above, and a @file{university.ily} file that
contains only the tweaks to produce the output that my professor
prefers.  The top of @file{music.ly} would then look like this:

@example
\include "definitions.ily"

%%%  Only uncomment one of these two lines!
\include "web-publish.ily"
%\include "university.ily"
@end example

This approach can be useful even if you are only producing
one set of parts.  I use half a dozen different
@q{style sheet} files for my projects.  I begin every music
file with @code{\include "../global.ily"}, which contains

@example
%%%   global.ily
\version "@version{}"

#(ly:set-option 'point-and-click #f)

\include "../init/init-defs.ly"
\include "../init/init-layout.ly"
\include "../init/init-headers.ly"
\include "../init/init-paper.ly"
@end example


@node Other sources of information
@subsection Other sources of information

The Internals Reference documentation contains a lot of information
about LilyPond, but even more information can be gathered by
looking at the internal LilyPond files.  To explore these, you must
first find the directory appropriate to your system.  If you say

@example
lilypond --help
@end example

@noindent
on the command line, the top-level directory of LilyPond's data
tree is shown near the end of the help output, for example

@example
Top-level directory of LilyPond's data tree:
  '/usr/local/share/lilypond/@version{}'
@end example

Within this directory the two interesting subdirectories are

@indentedblock
@table @file
@item ly/
contains files in LilyPond format
@item scm/
contains files in Scheme format
@end table
@end indentedblock

Let's begin by looking at some files in @file{ly/}.
Open @file{ly/property-init.ly} in a text editor -- the one
you normally use for @code{.ly} files will be fine.  This file
contains the definitions of all the standard LilyPond predefined
commands, such as @code{\tieUp} and @code{\slurDotted}.  You will
see that these are nothing more than definitions of variables
containing one or a group of @code{\override} commands.  For
example, @code{\textLengthOn} is defined to be:

@example
textLengthOn = @{
  % 0.4 staff-space between adjacent texts
  \override TextScript.extra-spacing-width = #'(-0.0 . 0.4)
  \override TextScript.extra-spacing-height = #'(-inf.0 . +inf.0)
@}
@end example

If you do not like the default values these predefined commands can
be redefined easily, just like any other variable, at the
head of your input file.

The following are the most useful files to be found in
@file{ly/}:

@indentedblock
@raggedright
@multitable @columnfractions .42 .53
@headitem Filename
  @tab Contents
@item @file{ly/engraver-init.ly}
  @tab Definitions of engraver contexts
@item @file{ly/paper-defaults-init.ly}
  @tab Specifications of paper-related defaults
@item @file{ly/performer-init.ly}
  @tab Definitions of performer contexts
@item @file{ly/property-init.ly}
  @tab Definitions of all common predefined commands
@item @file{ly/spanner-init.ly}
  @tab Definitions of spanner-related predefined commands
@end multitable
@end raggedright
@end indentedblock

Other settings (such as the definitions of markup commands) are
stored as @file{.scm} (Scheme) files.  The Scheme programming
language is used to provide a programmable interface into
LilyPond's internal operation.  Further explanation of these files
is outside the scope of this manual, as some knowledge of
the Scheme language is required.  Users should be warned that
a substantial amount of technical knowledge or time is required
to understand Scheme and these files (see @rextend{Scheme tutorial}).

If you have this knowledge, the Scheme files that may be of
interest are:

@indentedblock
@raggedright
@multitable @columnfractions .42 .53
@headitem Filename
  @tab Contents
@item @file{scm/auto-beam.scm}
  @tab Sub-beaming defaults
@item @file{scm/define-grobs.scm}
  @tab Default settings for grob properties
@item @file{scm/define-markup-commands.scm}
  @tab Almost all markup commands
@item @file{scm/midi.scm}
  @tab Default settings for MIDI output
@item @file{scm/output-lib.scm}
  @tab Settings that affect appearance of frets, colors,
       accidentals, bar lines, etc.
@item @file{scm/parser-clef.scm}
  @tab Definitions of supported clefs
@item @file{scm/script.scm}
  @tab Default settings for articulations
@end multitable
@end raggedright
@end indentedblock


@node Advanced tweaks with Scheme
@subsection Advanced tweaks with Scheme

Although many things are possible with the @code{\override} and
@code{\tweak} commands, an even more powerful way of modifying
the action of LilyPond is available through a programmable
interface to the LilyPond internal operation.  Code written in
the Scheme programming language can be incorporated directly in
the internal operation of LilyPond.  Of course, at least a basic
knowledge of programming in Scheme is required to do this, and an
introduction is provided in the @rextend{Scheme tutorial}.

As an illustration of one of the many possibilities, instead of
setting a property to a constant it can be set to a Scheme
procedure, which is then called whenever that property is accessed
by LilyPond.  The property can then be set dynamically to a value
determined by the procedure at the time it is called.  In this
example we color the note head in accordance with its position on
the staff.

@cindex @code{x11-color}, example of using
@cindex @code{NoteHead}, example of overriding
@cindex @code{color} property, setting to Scheme procedure

@lilypond[quote,verbatim,ragged-right]
#(define (color-notehead grob)
   "Color the notehead according to its position on the staff."
   (let ((mod-position (modulo (ly:grob-property grob 'staff-position)
                               7)))
     (case mod-position
       ;;   Return rainbow colors
       ((1) (x11-color 'red    ))  ; for C
       ((2) (x11-color 'orange ))  ; for D
       ((3) (x11-color 'yellow ))  ; for E
       ((4) (x11-color 'green  ))  ; for F
       ((5) (x11-color 'blue   ))  ; for G
       ((6) (x11-color 'purple ))  ; for A
       ((0) (x11-color 'violet ))  ; for B
       )))

\relative {
  % Arrange to obtain color from color-notehead procedure
  \override NoteHead.color = #color-notehead
  a2 b | c2 d | e2 f | g2 a |
}
@end lilypond

Further examples showing the use of these programmable interfaces
can be found in @rextend{Callback functions}.
