lilypond-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Defining variables mid-stream in a music expression using tags.


From: Valentin Petzel
Subject: Re: Defining variables mid-stream in a music expression using tags.
Date: Thu, 18 May 2023 12:31:48 +0200

Am Donnerstag, 18. Mai 2023, 05:32:28 CEST schrieb dfro:
> \version "2.24.1"
> 
> #(define-markup-command
>    (symbol
>     layout props
>     var staff-variable)
>    (markup? markup?)
>    (interpret-markup
>     layout props
>     #{ \markup \concat { "Symbol " #var #staff-variable } #}
>     )
>    )
> 
> symbol =
> #(define-music-function
>    ()
>    ()
>    #{ -\tag #'staff-one ^\markup \symbol #var "staff-one-var"
>       -\tag #'staff-two ^\markup \symbol #var "staff-two-var"
>    #})
> 
> 
> music =
> {
> 
>    #(define var "var-1 ")
>    c1 \symbol
>    #(define var "var-2 ")
>    c1 \symbol
> 
> }
> 
> <<
>    \new Staff = "one"
>    {
>      \keepWithTag #'staff-one
>      \relative c' {
> 
>        \textLengthOn
>        \music
> 
>      }
>    }
>    \new Staff = "two"
>    {
>      \keepWithTag #'staff-two
>      \relative c' {
> 
>        \textLengthOn
>        \music
> 
>      }
>    }

Hello David,

This is indeed a better way, Essentially instead of creating a single markup 
expression relying on a global state you are creating two different markups and 
selecting the one you want to use. But really instead of defining a global 
variable #var you should simply pass that as argument to your function:

symbol =
#(define-music-function
   (var)
   (markup?)
   #{ -\tag #'staff-one ^\markup \symbol #var "staff-one-var"
      -\tag #'staff-two ^\markup \symbol #var "staff-two-var"
   #})


music =
{
   c1 \symbol "var-1"
   c1 \symbol "var-2"
}


This way you’ll completely avoid having to handle global states. More 
generally if you want have parts of your score behave in a conditionally 
different manner it may also be an option to wrap your whole music expression 
into a markup function which behaves differently based on some parameter.

Generally we want to avoid dependency on global states (unless they are flags 
or options that are not supposed to change) as this may cause weird behaviour 
if thing do not evaluate at the time we think they do. For example if we move 
the scoping of #var to the markup function:

\version "2.24.1"

#(define-markup-command
   (symbol
    layout props
    staff-variable)
   (markup?)
   (interpret-markup
    layout props
    #{ \markup \concat { "Symbol " #var #staff-variable } #}
    )
   )

symbol =
#(define-music-function
   ()
   ()
   #{ -\tag #'staff-one ^\markup \symbol "staff-one-var"
      -\tag #'staff-two ^\markup \symbol "staff-two-var"
   #})


music =
{

   #(define var "var-1 ")
   c1 \symbol
   #(define var "var-2 ")
   c1 \symbol

}

<<
   \new Staff = "one"
   {
     \keepWithTag #'staff-one
     \relative c' {

       \textLengthOn
       \music

     }
   }
   \new Staff = "two"
   {
     \keepWithTag #'staff-two
     \relative c' {

       \textLengthOn
       \music

     }
   }
 >>

This will cause trouble, as the markup function is only evaluated later when 
it is rendered.

Cheers,
Valentin

Attachment: signature.asc
Description: This is a digitally signed message part.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]