# Defining new commands in Lilypond (for koron symbol)

I am trying to write a simple, minimalist script in LilyPond to create the koron symbol (used in Persian music).

I came up with the following solution, with satisfactory results, but my code is not well-organized:

koron = {
\once\override Accidental.stencil = #ly:text-interface::print
\once\override Accidental.text = \markup {
\overlay {
\override #'(thickness . 1.5) {
\draw-line #'(-.7 . .4)
\draw-line #'(-.7 . -.4)
\translate #'(-.7 . -1.4)
\draw-line #'(0 . 1.8)
}
}
}
}

\markup {Mode of Dastgah}

\score {
{
\omit Staff.TimeSignature
\cadenzaOn
\key c \major % not interested in automation of accidentals!
\override Staff.KeySignature.stencil = #ly:text-interface::print
\override Staff.KeySignature.text = \markup {
\musicglyph #"accidentals.flat" % standard symbol added to key signature like this
\overlay {
\raise #1.5 {
\override #'(thickness . 1.5) {
\draw-line #'(-.7 . .4)
\draw-line #'(-.7 . -.4)
\translate #'(-.7 . -1.4)
\draw-line #'(0 . 1.8)
}
}
} % koron symbol added to key signature like this
}
\relative c' {
b!1 c \koron % replaces accidental of next note by koron
deh e f g
a^\markup {
\overlay {
\override #'(thickness . 1.5) {
\translate #'(1.5 . .7) {
\draw-line #'(-.7 . .4)
\draw-line #'(-.7 . -.4)
\translate #'(-.7 . -1.4)
\draw-line #'(0 . 1.8)
}
}
} (var)
} % koron in a text
b
\bar "|."
}
}
}


My naive difficulty is with defining new commands. I wish LilyPond would accept a LaTeX tool like \newcommand, but apparently it does not. The variable "koron" in my code seems to be working fine, but when I try to define, for example,

koronsymboldraw = {
\draw-line #'(-.7 . .4)
\draw-line #'(-.7 . -.4)
\translate #'(-.7 . -1.4)
\draw-line #'(0 . 1.8)
}


to make things more modular, LilyPond gives me errors:

error: unknown escaped string: \draw-line'
...


With the manual of LilyPond not being sufficiently helpful really, I hope I can get some thoughts here on how we can define efficient commands and functions in LilyPond, particularly for this code.

• You should submit this as a feature request to lilypond. May 8, 2022 at 12:49

## 1 Answer

When you are defining a non-musical piece of script you often have to wrap it in a markup. Nesting markups is fine.

I don't know what version you are running, but this seems to work in version 2.19:

\version "2.19.48"

koronsymboldraw = \markup {
\override #'(thickness . 1.5) {
\overlay {
\draw-line #'(-.7 . .4)
\draw-line #'(-.7 . -.4)
\translate #'(-.7 . -1.4)
\draw-line #'(0 . 1.8)
}
}
}

koron = {
\once\override Accidental.stencil = #ly:text-interface::print
\once\override Accidental.text = \koronsymboldraw
}

\markup { Mode of Dastgah }

\score {
{
\omit Staff.TimeSignature
\cadenzaOn
\key c \major
\override Staff.KeySignature.stencil = #ly:text-interface::print
\override Staff.KeySignature.text = \markup {
\musicglyph #"accidentals.flat"
\raise #1.5
\koronsymboldraw
}
\relative c' {
b!1 c \koron
deh e f g
a^\markup {
\koronsymboldraw
\lower #0.5
(var)
}
b
\bar "|."
}
}
}

• It seems like a solution to wrap the definition of the commands in a \markup, but since these commands are mainly to be used inside other markups in the code, I'm not sure whether the approach of nested markups is not going to cause other problems. I sort of hoped to have a way of "simply" bundling a bunch of things into a placeholder, similar to what \newcommand provides in LaTeX. Also, why didn't you use \overlay instead of those \hspace's that you have added to \koronsymboldraw`? The resulting koron symbol with your combination of commands has misaligned lines and corners. May 1, 2021 at 4:08
• I didn't know what version you were running, so I first guessed that it was 2.18 (which is now quite clearly incorrect). 2.18 doesn't have the overlay command - so there were misalignments. So I replaced some of the overlays with the extra spacing. Then I changed to version 2.19, but didn't but the overlay command back in. Sorry about that – see edited response. May 8, 2021 at 4:26