]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/snippets/centering-markup-on-note-heads-automatically.ly
c599be1556626599091071c071c26ed39ec99af9
[lilypond.git] / Documentation / snippets / centering-markup-on-note-heads-automatically.ly
1 % Do not edit this file; it is automatically
2 % generated from Documentation/snippets/new
3 % This file is in the public domain.
4 %% Note: this file works from version 2.13.36
5 \version "2.13.40"
6
7 \header {
8   lsrtags = "text, tweaks-and-overrides, contexts-and-engravers"
9   texidoc = "
10 For technical reasons, text scripts attached to note heads cannot
11 easily be centered on a note head's width, unlike articulations.
12
13 Instead of using trial-and-error offset tweaks, this snippet uses a
14 Scheme engraver to reset the horizontal parent of each markup to a
15 @code{NoteColumn}.  This also allows text to follow note heads which have
16 been shifted via @code{force-hshift}.
17 "
18   doctitle = "Centering markup on note heads automatically"
19 } % begin verbatim
20
21
22 #(define (Text_align_engraver ctx)
23   (let ((scripts '())
24         (note-column #f))
25
26     `((acknowledgers
27        (note-column-interface
28         . ,(lambda (trans grob source)
29              ;; cache NoteColumn in this Voice context
30              (set! note-column grob)))
31
32        (text-script-interface
33         . ,(lambda (trans grob source)
34              ;; whenever a TextScript is acknowledged,
35              ;; add it to `scripts' list
36              (set! scripts (cons grob scripts)))))
37
38       (stop-translation-timestep
39        . ,(lambda (trans)
40             ;; if any TextScript grobs exist,
41             ;; set NoteColumn as X-parent
42             (and (pair? scripts)
43                  (for-each (lambda (script)
44                              (set! (ly:grob-parent script X) note-column))
45                            scripts))
46             ;; clear scripts ready for next timestep
47             (set! scripts '()))))))
48
49 \layout {
50   \context {
51     \Voice
52     \consists #Text_align_engraver
53     \override TextScript #'X-offset =
54       #ly:self-alignment-interface::aligned-on-x-parent
55     \override TextScript #'self-alignment-X = #CENTER
56   }
57 }
58
59 \new Staff <<
60   \relative c'' {
61     \override NoteColumn #'force-hshift = #3
62     c1-\markup { \arrow-head #Y #DOWN ##t }
63   }
64   \\
65   \relative c' {
66     a4 a-\markup { \huge ^ } a a
67   }
68 >>