It is possible by adapting the implementation of \page-ref
.
This is a bit awkward to use since you have to put a uniquely named \label
before each markup block that uses \odd-even-markup
, and the page number of the label, not the markup, is used for the decision. There may be a way to get the page number without a label, but I didn't find one.
Since the size of the markup is calculated before the page number is known, the odd and even markup have to be the same size (or they're padded to the same size). That probably won't be a problem in this particular use case.
\version "2.24.3"
#(define-markup-command (delayed-markup layout props factory) (procedure?)
"Use the dimensions of the markup returned by (factory #t) for layout,
and the markup returned by (factory #f), called later, for rendering."
(let* ((gauge-stencil (interpret-markup layout props (factory #t)))
(x-ext (ly:stencil-extent gauge-stencil X))
(y-ext (ly:stencil-extent gauge-stencil Y)))
(ly:stencil-outline
(ly:make-stencil
`(delay-stencil-evaluation
,(delay (ly:stencil-expr
(interpret-markup layout props
(make-with-dimensions-markup x-ext y-ext (factory #f))))))
x-ext y-ext)
(make-filled-box-stencil x-ext y-ext))))
#(define (layout-lookup-label layout label)
"The weird label-lookup logic from page-ref."
(define (assoc-name-get name ls)
(do ((ls ls (cdr ls)) (result '() result))
((null? ls) result)
(if (and (car ls) (eq? name (assoc-get 'name (cdar ls))))
(set! result (cons (car ls) result)))))
(let* ((table (ly:output-def-lookup layout 'label-page-table))
(alist-table (ly:output-def-lookup layout 'label-alist-table))
(retrieve-id (and (list? alist-table)
(let ((entry (assoc-name-get label alist-table)))
(and (pair? entry) (caar entry))))))
(and (list? table)
(assoc-get (or retrieve-id label) table))))
#(define-markup-command (page-ref-generic layout props label factory) (symbol? procedure?)
"Use the dimensions of the markup returned by (factory #t) for layout, and
the markup returned by (factory pagenum) for rendering, where pagenum is
the page number of the given label or #f if the label wasn't found."
(interpret-markup layout props
(make-delayed-markup-markup
(lambda (dry-run)
(factory (or dry-run (layout-lookup-label layout label)))))))
%#(define-markup-command (page-ref layout props label gauge default) (symbol? markup? markup?)
%
% "A reimplementation of page-ref in terms of page-ref-generic."
%
% (interpret-markup layout props
% (make-page-ref-generic-markup label
% (lambda (page-number)
% (if (boolean? page-number)
% (if page-number gauge default)
% (number-format (ly:output-def-lookup layout 'page-number-type) page-number))))))
#(define-markup-command (odd-even-markup layout props label ifodd ifeven) (symbol? markup? markup?)
"Select ifodd or ifeven markup depending on the page number of the label.
If label lookup fails, display ifodd and ifeven superimposed."
(interpret-markup layout props
(make-page-ref-generic-markup
label
(lambda (page)
(if (boolean? page)
(make-combine-markup ifodd ifeven)
(if (odd? page) ifodd ifeven))))))
\label #'a \markup \odd-even-markup #'a "odd" "even" \pageBreak
\label #'b \markup \odd-even-markup #'b "odd" "even" \pageBreak
\label #'c \markup \odd-even-markup #'c "odd" "even" \pageBreak
To use common title markup for all scores, you could do something like this. (Edit: see Lazy's answer for a way to avoid the \remember-page-number
and score-label = #last-remembered-page-label
boilerplate.)
last-remembered-page-label = ##f
remember-page-number =
#(define-music-function () ()
(set! last-remembered-page-label (gensym))
#{ \label #last-remembered-page-label #} )
#(define-markup-command (title-odd-even-markup layout props ifodd ifeven) (markup? markup?)
"like \\odd-even-markup, but gets the label from the 'score-label' header property."
(interpret-markup layout props
(make-odd-even-markup-markup (chain-assoc-get 'header:score-label props) ifodd ifeven)))
\paper {
scoreTitleMarkup = \markup \title-odd-even-markup "odd" "even"
}
\remember-page-number
\score {
\header { score-label = #last-remembered-page-label }
{ c'1 }
}
\pageBreak
\remember-page-number
\score {
\header { score-label = #last-remembered-page-label }
{ d'1 }
}