Jump to content

Colored arrows (2)

From LilyPond wiki
Revision as of 04:54, 9 July 2026 by Lemzwerg (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This snippet demonstrates drawing colored arrows in a score, e.g., for analysis purposes. These arrows adapt to layout changes, because they are made of spanners between (invisible) note heads. If you need arrows with a fixed length and angle, you might prefer the snippet Colored arrows.

\version "2.24"

forwardArrow =
#(define-music-function (color) (color?)
   #{
     % Cross-staff arrows are made using the VoiceFollower.  To have
     % the arrow behind the staff, choose a value below 0 for the
     % layer.  If you want the arrows to cover the notes, choose a
     % value of 2 or more.
     \once \override VoiceFollower.layer = -5

     \once \override VoiceFollower.thickness = 5   % line thickness
     \once \override VoiceFollower.color = #color

     % Padding can be adjusted to move arrow ends closer to the
     % notes.
     \once \override VoiceFollower.bound-details.left.padding = 2
     \once \override VoiceFollower.bound-details.right.padding = 2

     \once \override VoiceFollower.bound-details.right.arrow = ##t
     \once \override VoiceFollower.arrow-width = 1
     \once \override VoiceFollower.arrow-length = 2
     % ##f prevents line breaks within an arrow.
     \once \override VoiceFollower.breakable = ##t

     % Arrows within the same staff use the Glissando spanner, so
     % we repeat setting the above properties for this grob.
     \once \override Glissando.layer = -5
     \once \override Glissando.thickness = 5
     \once \override Glissando.color = #color
     \once \override Glissando.bound-details.left.padding = 2
     \once \override Glissando.bound-details.right.padding = 2
     \once \override Glissando.bound-details.right.arrow = ##t
     \once \override Glissando.arrow-width = 1
     \once \override Glissando.arrow-length = 2
     \once \override Glissando.breakable = ##t
   #})

backwardArrow =
#(define-music-function (color) (color?)
   #{
     \once \override VoiceFollower.layer = -5
     \once \override VoiceFollower.thickness = 5
     \once \override VoiceFollower.color = #color
     \once \override VoiceFollower.bound-details.left.padding = 2
     \once \override VoiceFollower.bound-details.right.padding = 2
     \once \override VoiceFollower.bound-details.left.arrow = ##t
     \once \override VoiceFollower.arrow-width = 1
     \once \override VoiceFollower.arrow-length = 2
     \once \override VoiceFollower.breakable = ##t

     \once \override Glissando.layer = -5
     \once \override Glissando.thickness = 5
     \once \override Glissando.color = #color
     \once \override Glissando.bound-details.left.padding = 2
     \once \override Glissando.bound-details.right.padding = 2
     \once \override Glissando.bound-details.left.arrow = ##t
     \once \override Glissando.arrow-width = 1
     \once \override Glissando.arrow-length = 2
     \once \override Glissando.breakable = ##t
   #})


\relative c' {
  <<
    % Usage: Place the arrow function call before the note, and the
    % glissando statement after the note.
    \new Staff = upper {
      c4 d e \backwardArrow #blue c \glissando |
      R1*5 |
      c4 d e c |
      e1 |
      d1 |
      c1 |
    }
    \new Staff = middle {
      R1 |
      c4 d e c |
      e8 d e f g f e c |
      d8 c d e f e f d |
      c1 |
      R1*5
    }
    \new Staff = lower <<
      \new Voice {
        \oneVoice
        R1*2 |
        c4 d e \forwardArrow #blue c\glissando |
        R1 |
        c4 d e c |
        d4 c b2 |
        R1*4
      }
      \new Voice {
        \override NoteColumn.ignore-collision = ##t 

        % For cross-staff arrows we use an additional voice with
        % hidden notes.  To make these notes visible, uncomment the
        % following lines
        %
        %   \override NoteHead.color = #cyan
        %   \override NoteHead.layer = #2
        %
        % and remove the following "\hideNotes" line.
        \hideNotes

        \set Voice.followVoice = ##t
        \change Staff = "upper" c4 s2. |
        % Place the arrow function call immediately before the staff
        % change.
        \backwardArrow #green \change Staff = middle c4 s2. |
        \forwardArrow #red \change Staff = lower c4 s2. |
        s1 |
        c4 s2. | \break
        s1 |
        \forwardArrow #red \change Staff = upper c4 s2. |
      }
    >>
  >>
}