4

My Lilypond score consist of repeat snippets a few bars each. I need to show the snippet number as markup. The score is being updated regularly by inserting new snippets, hence the snippet number should increment automatically, rather than being hard-coded:

snippetNumber = 0

snippet = #(define-music-function)
  (music)
  (ly:music?)
  #{
    % How do I increment the snippetNumber variable here?
    \textMark #(number->string snippetNumber)
    \repeat volta 2 {
      #music
    }
  #})

\snippet { c8 e fis e d fis gis fis }
\snippet { c a f cis d b g dis }

Snippets

New contributor
Stelian Dumitrascu is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • 1
    There should be no closing parenthesis at the end of the line snippet = #(define-music-function.
    – phoog
    Commented 10 hours ago

1 Answer 1

3

Add this where the comment is:

#(set! snippetNumber (+ 1 snippetNumber))

With the syntax correction, this gives

snippetNumber = 0

snippet = #(define-music-function
  (music)
  (ly:music?)
  #{
    #(set! snippetNumber (+ 1 snippetNumber))
    \textMark #(number->string snippetNumber)
    \repeat volta 2 {
      #music
    }
  #})

\snippet { c8 e fis e d fis gis fis }
\snippet { c a f cis d b g dis }
1
  • Or even (set! snippetNumber (1+ snippetNumber)).
    – Lazy
    Commented 7 hours ago

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.