I've been avoiding using variables in Lilypond as the pieces I've been scoring have been short but I'm now beginning to use bars (phrases?) repeatedly.

What is the best approach to the problem of having a variable repeat but in a \relative context which results in the variable not being scored in the correct octave (as it deduces position from the prior notes)?

See below as an example, where the bebtuplet variable is produced at a different octave on second use.

\version "2.23"

global = {
  \key c \major
  \time 4/4
  \tempo 4 = 60

bebtuplet = {
    <b e b'>2~ <b e b'>8 a' e c  | << { e1 } \\ { \tuplet 3/2 {b16 c b} a8~ a2. }  >>

rh =  {
  \relative c'' {

    g8 g g g << { d'4.( c8) } \\ { f,2 } >> | \bebtuplet |  \break

    << { e2 <d~ f>4 <d e>4 } >> | \bebtuplet | \break

\score {
   \context PianoStaff <<
   \context Staff = "rh" <<
     \clef "treble"
  \layout { }
  \midi { }

result of posted code: repeated material in two different octaves

2 Answers 2


There are two (related) options.

  1. Enclose the repeated block within its own \relative block.
bebtuplet = { \relative c'' {
    <b e b'>2~ <b e b'>8 a' e c  | << { e1 } \\ { \tuplet 3/2 {b16 c b} a8~ a2. }  >>

Result of above code inserted into OP code

  1. You could also use \fixed in a similar way. See the LilyPond manual for Absolute octave entry.

Frame challenge. You ask for "the best approach". A better approach than using \relative is not using it. When you specify each note's pitch absolutely, it's much easier for you to be sure that each note is in the octave you intend. If you find that that means typing more ticks than you like to, use \transpose.

Your example indicates that you'd find \transpose useful anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.