4.4.3 Vertical collision avoidance

Intuitively, there are some objects in musical notation that belong
to the staff and there are other objects that should be placed outside
the staff. Objects belonging outside the staff include things such as
rehearsal marks, text and dynamic markings (from now on, these will
be called outside-staff objects). 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
another object.

LilyPond uses the outside-staff-priority property to determine
whether a grob is an outside-staff object: if outside-staff-priority
is a number, the grob is an outside-staff object. In addition,
outside-staff-priority tells LilyPond in which order the objects
should be placed.

First, LilyPond places all the objects that do not belong outside
the staff. Then it sorts the outside-staff objects according to their
outside-staff-priority (in increasing order). One by one, LilyPond
takes the outside-staff objects and places them 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 outside-staff-priority will be placed closer to
the staff.

By default, outside-staff objects are placed so they avoid
a horizontal collision with previously-positioned grobs. This
can lead to situations in which objects are placed close to each
other horizontally.
As shown in the example below, setting outside-staff-horizontal-padding
increases the horizontal spacing required, and in this case moves the text up
to prevent it from getting too close to the ledger lines.