LilyPond snippets: Spacing

# Spacing

Spacing issues

## Adjusting lyrics vertical spacing

This snippet shows how to bring the lyrics line closer to the staff.

    % Default layout:
    <<
      \new Staff \new Voice = melody \relative c' {
        c4 d e f
        g4 f e d
        c1
      }
      \new Lyrics \lyricsto melody { aa aa aa aa aa aa aa aa aa }

      \new Staff {
        \new Voice = melody \relative c' {
          c4 d e f
          g4 f e d
          c1
        }
      }
      % Reducing the minimum space below the staff and above the lyrics:
      \new Lyrics \with {
        \override VerticalAxisGroup.nonstaff-relatedstaff-spacing =
          #'((basic-distance . 1))
      }
      \lyricsto melody { aa aa aa aa aa aa aa aa aa }
    >>

## Allowing fingerings to be printed inside the staff

By default, vertically oriented fingerings are positioned outside the staff; that behavior, however, may be disabled. Attention needs to be paid to situations where fingerings and stems are in the same direction: by default, fingerings will avoid only beamed stems. That setting can be changed to avoid no stems or all stems; the following example demonstrates these two options, as well as how to go back to the default behavior.

    \relative c' {
      <c-1 e-2 g-3 b-5>2
      \override Fingering.staff-padding = #'()
      <c-1 e-2 g-3 b-5>4 g'-0
      a8[-1 b]-2 g-0 r
      \override Fingering.add-stem-support = ##f
      a[-1 b]-2 g-0 r
      \override Fingering.add-stem-support = ##t
      a[-1 b]-2 g-0 r
      \override Fingering.add-stem-support = #only-if-beamed
      a[-1 b]-2 g-0 r
    }

## Page label

Page labels may be placed inside music or at top-level, and referred to in markups.

    #(set-default-paper-size "a6")

    #(define-markup-command (toc-line layout props label text)
      (symbol? markup?)
      (interpret-markup layout props
       (markup #:fill-line (text #:page-ref label "8" "?"))))

    \book {
      \markup \huge \fill-line { \null Title Page \null }

      \pageBreak

      \label #'toc
      \markup \column {
        \large \fill-line { \null Table of contents \null }
        \toc-line #'toc "Table of contents"
        \toc-line #'firstScore "First Score"
        \toc-line #'markA "Mark A"
        \toc-line #'markB "Mark B"
        \toc-line #'markC "Mark C"
        \toc-line #'unknown "Unknown label"
      }

      \pageBreak

      \label #'firstScore
      \score {
        \new Staff \relative c' {
          c2 c
          \mark \markup {
            A (page \concat { \page-ref #'markA "0" "?" ) }
          } \label #'markA
          c2 c
          \pageBreak
          \mark "B" \label #'markB
          d2 d
          d2 d
          \once \override Score.RehearsalMark.break-visibility =
            #begin-of-line-invisible
          \mark "C" \label #'markC
        }
        \header { piece = "First score" }
      }
    }

## Proportional strict notespacing

If `strict-note-spacing` is set spacing of notes is not influenced by bars or clefs within a system. Rather, they are placed just before the note that occurs at the same time. This may cause collisions.

    \relative c'' <<
      \override Score.SpacingSpanner.strict-note-spacing = ##t
      \set Score.proportionalNotationDuration = #(ly:make-moment 1/16)
      \new Staff {
        c8[ c \clef alto c c \grace { d16 } c8 c] c4
        c2 \grace { c16[ c16] } c2
      }
      \new Staff {
        c2 \tuplet 3/2 { c8 \clef bass cis,, c } c4
        c1
      }
    >>

## Vertically aligned dynamics and textscripts

All `DynamicLineSpanner` objects (hairpins and dynamic texts) are placed with their reference line at least `'staff-padding` from the staff, unless other notation forces them to be farther. Setting `'staff-padding` to a sufficiently large value aligns the dynamics.

The same idea, together with `\textLengthOn`, is used to align the text scripts along their baseline.

    music = \relative c' {
      a'2\p b\f
      e4\p f\f\> g, b\p
      c2^\markup { \huge gorgeous } c^\markup { \huge fantastic }
    }

    {
      \music
      \break
      \override DynamicLineSpanner.staff-padding = #3
      \textLengthOn
      \override TextScript.staff-padding = #1
      \music
    }

## Vertically aligning ossias and lyrics

This snippet demonstrates the use of the context properties `alignBelowContext` and `alignAboveContext` to control the positioning of lyrics and ossias.

    \paper {
      ragged-right = ##t
    }

    \relative c' <<
      \new Staff = "1" { c4 c s2 }
      \new Staff = "2" { c4 c s2 }
      \new Staff = "3" { c4 c s2 }
      { \skip 2
        <<
          \lyrics {
            \set alignBelowContext = #"1"
            lyrics4 below
          }
          \new Staff \with {
            alignAboveContext = #"3"
            fontSize = #-2
            \override StaffSymbol.staff-space = #(magstep -2)
            \remove "Time_signature_engraver"
          } {
            \tuplet 6/4 {
              \override TextScript.padding = #3
              c8[^"ossia above" d e d e f]
            }
          }
        >>
      }
    >>

#### LilyPond — Snippets v2.22.2 (stable-branch).