Jump to content

Integrating LilyPond expressions inside Scheme functions: Difference between revisions

From LilyPond wiki
m Replace version="2.24.0" with version="2.24" now that the LilyWiki extension supports auto-selecting the latest release in a stable series
m New category
Tags: Mobile edit Mobile web edit
 
(One intermediate revision by the same user not shown)
Line 37: Line 37:
[[Category:Syntax and expressions]]
[[Category:Syntax and expressions]]
[[Category:Really simple]]
[[Category:Really simple]]
[[Category:Snippet]]

Latest revision as of 23:37, 21 November 2025

It is possible to use fragments of LilyPond syntax inside Scheme, by putting them between #{ and #}. In this example, three functions are defined to apply different paddings on the TextScript markups, using native LilyPond commands such as \override TextScript #'padding.

\version "2.24"

%% http://lsr.di.unimi.it/LSR/Item?id=251

withPaddingA = #(define-music-function (padding music) (number? ly:music?)
		 #{ \override TextScript.padding = #padding
		 $music 
		 \revert TextScript.padding #})

withPaddingB = #(define-music-function (padding music) (number? ly:music?)
		 #{ \override TextScript.padding = #(* padding 2)
		 $music 
		 \revert TextScript.padding #})

withPaddingC = #(define-music-function (padding music) (number? ly:music?)
		 #{ \override TextScript.padding = #(+ 1 (* padding 2))
		    $music 
		    \revert TextScript.padding #})

{
  c'^"1"
  \withPaddingA #2
  { c'^"2" c'^"3" }
  c'^"4"
  \withPaddingB #2
  { c'^"5" c'^"6" }
  c'^"7"
  \withPaddingC #2
  { c'^"8" c'^"9" }
  c'^"10"
}