Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Using abjad to create Lilypond templates

9 views
Skip to first unread message

Pedro Faria

unread,
Sep 13, 2024, 10:31:14 AM9/13/24
to abjad-user
I'm looking to use Abjad to help creating Lilypond templates (a simple use I know, but I'm still a beginner)

Is there a way to, in abjad, define variables to be used in lilypond?

For example, let's say that in my score block, instead of the music being typed out. I have only a reference to a variable, and that variable is defined elsewhere with the music to be engraved.

Something like this:

global = (...)

music = (...)

} \score { \new Staff << \global \music >> \layout { } } }

Davi Raubach

unread,
Sep 14, 2024, 3:51:23 PM9/14/24
to abjad...@googlegroups.com

Hi Pedro,

To insert a variable in a lilypond file:

import abjad
score = abjad.Score()
var = r"global = {}"
ly = abjad.LilyPondFile(items=[var, score])
print(abjad.lilypond(ly))
\version "2.25.13"
\language "english"
global = {}
\new Score
<<

To reference it is complicated. Abjad LilyPond parser understands variable assigns (see: https://abjad.github.io/api/abjad/parsers/parser.html#abjad.parsers.parser.LilyPondParser) but you need to pass it together and the result is not what you want:

score = abjad.Score()
parser = abjad.parser.LilyPondParser()
string = r"global = {} \new Staff {\global}"
staff = parser(string)
score.append(staff)
ly = abjad.LilyPondFile(items=[score])
print(abjad.lilypond(ly))
\version "2.25.13"
\language "english"
\new Score
<<
    \new Staff
    {
        {
        }
    }

I manage to get your expected result adding an empty container and attaching the variable as a LilyPondLiteral:

import abjad
score = abjad.Score()
var = r"global = {}"
staff = abjad.Staff()
cont = abjad.Container()
staff.append(cont)
abjad.attach(abjad.LilyPondLiteral(r"\global"), cont)
score.append(staff)
ly = abjad.LilyPondFile(items=[var, score])
print(abjad.lilypond(ly))
\version "2.25.13"
\language "english"
global = {}
\new Score
<<
    \new Staff
    {
        \global
        {
        }
    }

Feels like hacking.

Best regards,

Davi

Pedro Faria <pedrofa...@gmail.com> writes:

I'm looking to use Abjad to help creating Lilypond templates (a simple use I know, but I'm still a beginner)

Is there a way to, in abjad, define variables to be used in lilypond?

For example, let's say that in my score block, instead of the music being typed out. I have only a reference to a variable, and that variable is defined elsewhere with the music to be engraved.

Something like this:

global = (…)

music = (…)

} { Staff << >> { } } }

Pedro Faria

unread,
Sep 16, 2024, 7:03:07 AM9/16/24
to abjad-user
Oh wow, thank you so much! That's exactly what I needed
Reply all
Reply to author
Forward
0 new messages