Please excuse the vague question title. I don't know how what I need is called in Lilypond - the two words (macros/templates) are what I'd call it in programming context.
I have a Lilypond document like this:
title = "title1"
music = { \relative { c c c c } \addlyrics { this is some text } }
% song on one page
\bookpart { \header { title = \title } \score { \music \layout { } } }
% midi file creation
\book { \bookOutputName \title \score { \unfoldRepeats \music \midi { } } }
title = "title2"
music = { \relative { d d d d } \addlyrics { here o -- ther text } }
% song on one page
\bookpart { \header { title = \title } \score { \music \layout { } } }
% midi file creation
\book { \bookOutputName \title \score { \unfoldRepeats \music \midi { } } }
title = "title3"
music = { \relative { e e e e } \addlyrics { what -- e -- ver text } }
% song on one page
\bookpart { \header { title = \title } \score { \music \layout { } } }
% midi file creation
\book { \bookOutputName \title \score { \unfoldRepeats \music \midi { } } }
There's a lot of code repetition: For each title
and musicAndLyrics
I have the same code. I want to put that code into a macro/template and reuse that for each pair of title
and musicAndLyrics
. Like this:
% define macro. NOTE THAT I DO NOT KNOW THE SYNTAX. THIS IS PSEUDOCODE!
macro(foo, bar) =
{
% song on one page
\bookpart { \header { title = \foo } \score { \bar \layout { } } }
% midi file creation
\book { \bookOutputName \foo \score { \unfoldRepeats \bar \midi { } } }
}
title = "title1"
music = { \relative { c c c c } \addlyrics { this is some text } }
% call macro with above variables as content
macro(title, music)
title = "title2"
music = { \relative { d d d d } \addlyrics { here o -- ther text } }
% call macro with above variables as content
macro(title, music)
title = "title3"
music = { \relative { e e e e } \addlyrics { what -- e -- ver text } }
% call macro with above variables as content
macro(title, music)
Is there a way to achieve this? If yes, how? How is that concept called in Lilypond? I searched Lilypond snippets that contained "template", "macro", "variable" and "function" in their content or comments, but could find no example where something like \bookpart
, \header
etc. were used in the way I intend.
Bonus question:
Can we write a macro based on arrays in order to remove the last bit of code repetition? Like this:
% define macro. NOTE THAT I DO NOT KNOW THE SYNTAX.
% THIS IS (C#/JAVA-based) PSEUDOCODE!
% assumption: the two arrays have the same length.
macro(foo[], bar[]) =
{
for (int i = 0; i < foo.length; i++)
{
% song on one page
\bookpart { \header { title = \foo[i] } \score { \bar[i] \layout { } } }
% midi file creation
\book { \bookOutputName \foo[i] \score { \unfoldRepeats \bar[i] \midi { } } }
}
}
title[1] = "title1"
music[1] = { \relative { c c c c } \addlyrics { this is some text } }
title[2] = "title2"
music[2] = { \relative { d d d d } \addlyrics { here o -- ther text } }
title[3] = "title3"
music[3] = { \relative { e e e e } \addlyrics { what -- e -- ver text } }
% call macro with above variables as content
macro(title, music)
Side question: Would you put that question somewhere else, like StackOverflow or Tex Stackexchange? Where do you think the chance for a good answer is highest?