Hello Dav, hello Lukas,

Instead of overriding the stencil like this one can also set the stencil to 
ly:text-interface::print and override the text property. 

Or if one need this multiple times one could do something like the appended 
version, which automatically chooses the stencil depending on whether the text 
property is set.

Cheers,
Valentin

Am Mittwoch, 8. Dezember 2021, 20:29:28 CET schrieb Lukas-Fabian Moser:
> Hi Dav,
> 
> Am 08.12.21 um 19:50 schrieb Rajesh Baskar:
> > Can you hide and replace a note with a question mark? I kind of
> > achieved this with the below example. But the question mark is not in
> > place of the note, it appears below the staff. I can use \override
> > TextScript.extra-offset = #'(0 . 4.275) to achieve this but I don't
> > want to hard code the offset as the note to hide can appear anywhere
> > on the staff.
> 
> You can exchange the note head by a markup:
> 
> \version "2.22.0"
> 
> \language english
> 
> questionMark = \markup \general-align #Y #CENTER {
>    \fontsize #7 \with-color "#6A00F4" "?"
> }
> 
> \new Staff \with {
>    \omit TimeSignature
> }
> {
>    \cadenzaOn
>    \key c \major
>    \clef bass
>    f1 g
>    % comment to center the question mark
>    % on the actual notehead position:
>    \once \override NoteHead.Y-offset = 0
>    \once \override NoteHead.stencil =
>    #(lambda (grob) (grob-interpret-markup grob questionMark))
>    a
>    bf c'
>    \bar "||"
> }
> 
> If you want to do this more often, you might define a shorthand (that
> then should also contain a "\once \omit Stem" etc.)
> 
> Lukas
\version "2.22.0"

\language english

questionMark = \markup \general-align #Y #CENTER {
   \fontsize #7 \with-color "#6A00F4" "?"
}

#(define (get-text grob)
   (ly:grob-property grob 'text))

#(define (has-text? grob)
   (not (null? (get-text grob))))

#(define (alternate-if-text sym alt)
   (grob-transformer sym
      (lambda (grob orig)
        (if (has-text? grob)
            (if (procedure? alt) (alt grob) alt)
            orig))))

\new Staff \with {
   \omit TimeSignature
   \override NoteHead.stencil = #(alternate-if-text 'stencil ly:text-interface::print)
   \override NoteHead.Y-offset = #(alternate-if-text 'Y-offset 0)
}
{
   \cadenzaOn
   \key c \major
   \clef bass
   f1 g
   \once \override NoteHead.text = \questionMark
   a
   bf c'
   \once \override NoteHead.text = \questionMark
   d'
   \bar "||"
}

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

Reply via email to