3.3.6 Reference to page numbers

A particular place of a score can be marked using the \label command, either at top level or inside music. This label can then be referred to in a markup, to get the number of the page where the marked point is placed, using the \page-ref markup command.

\header { tagline = ##f }
\book {
  \label #'firstScore
  \score {
    {
      c'1
      \pageBreak \mark A \label #'markA
      c'1
    }
  }
  \markup { The first score begins on page \page-ref #'firstScore "0" "?" }
  \markup { Mark A is on page \page-ref #'markA "0" "?" }
}
[image of music]

The \page-ref markup command takes three arguments:

  1. the label, a Scheme symbol, for example #'firstScore;
  2. a markup that will be used as a gauge to estimate the dimensions of the markup;
  3. a markup that will be used in place of the page number if the label is not known.

The reason why a gauge is needed is that, at the time markups are interpreted, the page breaking has not yet occurred, so the page numbers are not yet known. To work around this issue, the actual markup interpretation is delayed to a later time; however, the dimensions of the markup have to be known before, so a gauge is used to decide these dimensions. If the book has between 10 and 99 pages, it may be “00”, i.e., a two digit number.

If the size of the final text is different from the gauge, it could be useful to configure the horizontal alignment relative to the gauge with the property x-align. The default is right-aligned.

\markup {
  \box
    \page-ref #'foo "???" "?" " right-aligned (default)"
}
\markup {
  \box
    \override #`(x-align . ,LEFT)
    \page-ref #'foo "???" "?" " left-aligned"
}
\markup {
  \box
    \override #'(x-align . -2.5)
    \page-ref #'foo "???" "?" " left outside"
}
[image of music]

In the example the gauge ‘???’ is wider than the replacement text ‘?’. The replacement is used because the label #'foo does not exist. The property x-align can be set with any numbers. The predefined symbols LEFT, CENTER or RIGHT can also be used to set the alignment to left, center or right relative to the gauge.

Predefined commands

\label, \page-ref.


LilyPond Notation Reference v2.25.24 (development-branch).