Hello Stefano,

Here’s a slightly improved example, offering more control.

Cheers,
Valentin

Am Mittwoch, 10. März 2021, 05:31:07 CET schrieben Sie:
> Thank you for the example Valentin,
> 
> a very interesting approach.  I think I was headed in that direction, in
> fact, although you code will make me jump ahead quite a bit.
> 
> Cheers,
> 
> Stefano
> 
> On Fri, Mar 5, 2021 at 2:49 PM Valentin Petzel <[email protected]> wrote:
> > Hello Stefano,
> > 
> > You might try to keep the whole thing dynamic and leave the numbering and
> > stuff to Lilypond. For example you could have just a list of all Examples
> > in
> > one Category and then have a function to print that out. You could even do
> > a
> > list of Categories and print them all at once. This way you could even
> > manage
> > your Scores anyway you want, and adding Examples to one Group requires
> > just
> > adding it to that list. See the appended File for an example.
> > 
> > Cheers,
> > Valentin
#(define (string-replace-substring s substring replacement)
  "Replace every instance of substring in s by replacement."
  (let ((sublen (string-length substring)))
    (with-output-to-string
      (lambda ()
        (let lp ((start 0))
          (cond
           ((string-contains s substring start)
            => (lambda (end)
                 (display (substring/shared s start end))
                 (display replacement)
                 (lp (+ end sublen))))
           (else
            (display (substring/shared s start)))))))))

catScore=
#(define-scheme-function (music title) (ly:music? string?)
   #{
     \score {
       \header {
         piece = \markup { #title }
       }
       #music
       \layout { }
     }
   #})

addScore = #(define-void-function (score) (ly:score?) (add-score score)) 


% List contains eighter scores, or lists (score title="" count?=#t append?=#t sep="")
% Set title to a string, {idx} is replaced by current count. Set count?=#f to not count
% the current score, e.g. Ex. 2 and Ex. 2b. Set append?=#f to not append title to Ex. {idx},
% but to replace it. If append?=#t you may sep sep as Separator between Ex. {idx} and title
% can also take nested pairs (score title count append . sep)
catScores=
#(define-void-function (listOfScores i) (list? number?)
   (if (not (null? listOfScores))
       (let* (
               (current (car listOfScores))
               (score (if (pair? current) (car current) current))
               (rest1 (if (pair? current) (cdr current) '()))
               (fmt   (if (null? rest1) ""
                          (if (pair? rest1) (car rest1)
                              rest1)))
               (rest2 (if (pair? rest1) (cdr rest1) '()))
               (count? (if (null? rest2) #t
                            (if (pair? rest2) (car rest2)
                                rest2)))
               (rest3 (if (pair? rest2) (cdr rest2) '()))
               (append?  (if (null? rest3) #t
                            (if (pair? rest3) (car rest3)
                                rest3)))
               (rest4 (if (pair? rest3) (cdr rest3) '()))
               (sep     (if (null? rest4) ""
                            (if (pair? rest4) (car rest4)
                                rest4)))
               (fmt-real (if append? (string-join (list "Ex. {idx}" fmt) sep) fmt))
               (title (string-replace-substring fmt-real "{idx}" (if count? (+ 1 i) i)))
               )
         (add-score (catScore score title))
         (catScores (cdr listOfScores) (if count? (+ 1 i) i)))))
     

#(define catA (list
               #{ c'4 e'8 e' g' f' e'4 #}
               #{ d'4 4 16 16 e'8 g' d' #}
               #{ e'8 8 f' f' d' d' g4 #}
               (list #{ c'4 e'8 e' g' f' e'4 #} "b" #f) ; do not count and append b
               (list #{ d'4 4 16 16 e'8 g' d' #} "Example {idx}" #t #f) ; count and replace
               (list #{ e'8 8 f' f' d' d' g4 #} "The Title" #t #t ": ") ; count, append, use : as sep
   ))


\markup "Only printing one category this time"

\catScores #catA 0

#(define catB (list
               #{ \time 2/3 \times 2/3 { f'4 g' a' e' } #}
               #{ \new StaffGroup <<
                 \new Staff { c'8 d' e' f' e' d' c'4 } 
                 \new Staff {\clef bass c4 g f e }
               >> #}
               #{  \new TabStaff { e, g b e' } #}))

printCats=
#(define-void-function (listOfCategories) (list?)
   (if (not (null? listOfCategories))
       (begin
         (add-text
          #{
           \markup\larger\larger\larger { Category #(car (car listOfCategories)) }
          #})
         (catScores (cdr (car listOfCategories)) 0)
         (printCats (cdr listOfCategories)))))

#(define cats (list (cons "A" catA) (cons "B" catB)))

\bookpart {
\markup "Printing all categories"
\printCats #cats
}

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

Reply via email to