Here is a cleaner, updated version of the snippet. It now gives the stencil the extent of the longest table of contents entry. Not ideal, but close enough for practical use. Also added comments and cleaned up variable names.
\version "2.18.2" #(define-markup-command (current-toc-section layout props) () (ly:stencil-add (let* ((titles (map cddr (toc-items))) (sorted-titles (stable-sort titles (lambda (item1 item2) (<= (length item1) (length item2))))) (biggest-title (interpret-markup layout props (caar sorted-titles))) (x-extent (ly:stencil-extent biggest-title X)) (y-extent (ly:stencil-extent biggest-title Y)) ) (ly:make-stencil `(delay-stencil-evaluation ,(delay (ly:stencil-expr (let* ((table (ly:output-def-lookup layout 'label-page-table)) (curr-page (chain-assoc-get 'page:page-number props)) ;; TOC entries should be at the beginning of sections/movements. ;; If a section starts mid-page, the page header will show the new section. ;; To make it show the title belonging to the first system of the page, ;; Change >= to <= and put TOC entries at the END of sections. ;; That breaks the actual TOC, however. (labels-up-to-curr (filter (lambda (item) (>= curr-page (cdr item))) table)) (most-recent-toc-page (apply max (map cdr labels-up-to-curr))) ;; If there are multiple toc items on the same page, behavior may be undefined. (most-recent-label (filter (lambda (item) (eq? (cdr item) most-recent-toc-page)) labels-up-to-curr)) (title-of-curr-label (cadr (assoc-get (caar most-recent-label) (toc-items)))) (curr-markup (interpret-markup layout props title-of-curr-label)) ;; How do we get the true extent outside of the delayed evaluation? ; (x-ext (ly:stencil-extent curr-markup X)) ; (y-ext (ly:stencil-extent curr-markup Y)) ) ; (set! x-extent x-ext) ; (set! y-extent y-ext) curr-markup )))) ;; Currently, we just use the extent of the longest toc entry for all of them x-extent y-extent )) )) %% Uncomment below to test % \paper { % evenHeaderMarkup = \markup \current-toc-section % oddHeaderMarkup = \markup { "foo" \current-toc-section "bar" } % } % % \score { % \new Staff { % \tocItem "Title 1" % s1 % \pageBreak % s1 % % } % } % % \score { % \new Staff { % \tocItem "Title 2 is long" % s1 % \pageBreak % s1 % % } % } -- Sent from: http://lilypond.1069038.n5.nabble.com/User-f3.html _______________________________________________ lilypond-user mailing list lilypond-user@gnu.org https://lists.gnu.org/mailman/listinfo/lilypond-user