]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/notation/input.itely
946eb170af2fc19f0f269c6952e149ad556eb007
[lilypond.git] / Documentation / notation / input.itely
1 @c -*- coding: utf-8; mode: texinfo; -*-
2
3 @ignore
4     Translation of GIT committish: FILL-IN-HEAD-COMMITTISH
5
6     When revising a translation, copy the HEAD committish of the
7     version that you are working on.  For details, see the Contributors'
8     Guide, node Updating translation committishes..
9 @end ignore
10
11 @c \version "2.19.22"
12
13 @node General input and output
14 @chapter General input and output
15
16 This section deals with general LilyPond input and output issues,
17 rather than specific notation.
18
19 @menu
20 * Input structure::
21 * Titles and headers::
22 * Working with input files::
23 * Controlling output::
24 * Creating MIDI output::
25 * Extracting musical information::
26 @end menu
27
28
29 @node Input structure
30 @section Input structure
31
32 The main format of input for LilyPond are text files.  By convention,
33 these files end with @file{.ly}.
34
35 @menu
36 * Structure of a score::
37 * Multiple scores in a book::
38 * Multiple output files from one input file::
39 * Output file names::
40 * File structure::
41 @end menu
42
43
44 @node Structure of a score
45 @subsection Structure of a score
46
47 @funindex \score
48
49 A @code{\score} block must contain a single music expression
50 delimited by curly brackets:
51
52 @example
53 \score @{
54   @dots{}
55 @}
56 @end example
57
58 @warning{There must be @strong{only one} outer music expression in
59 a @code{\score} block, and it @strong{must} be surrounded by
60 curly brackets.}
61
62 This single music expression may be of any size, and may contain
63 other music expressions to any complexity.  All of these examples
64 are music expressions:
65
66 @example
67 @{ c'4 c' c' c' @}
68 @end example
69
70 @lilypond[verbatim,quote]
71 {
72   { c'4 c' c' c' }
73   { d'4 d' d' d' }
74 }
75 @end lilypond
76
77 @lilypond[verbatim,quote]
78 <<
79   \new Staff { c'4 c' c' c' }
80   \new Staff { d'4 d' d' d' }
81 >>
82 @end lilypond
83
84 @example
85 @{
86   \new GrandStaff <<
87     \new StaffGroup <<
88       \new Staff @{ \flute @}
89       \new Staff @{ \oboe @}
90     >>
91     \new StaffGroup <<
92       \new Staff @{ \violinI @}
93       \new Staff @{ \violinII @}
94     >>
95   >>
96 @}
97 @end example
98
99 Comments are one exception to this general rule.  (For others see
100 @ref{File structure}.)  Both single-line comments and comments
101 delimited by @code{%@{ @dots{} %@}} may be placed anywhere within an
102 input file.  They may be placed inside or outside a @code{\score}
103 block, and inside or outside the single music expression within a
104 @code{\score} block.
105
106 Remember that even in a file containing only a @code{\score} block, it
107 is implicitly enclosed in a \book block.  A \book block in a source
108 file produces at least one output file, and by default the name of the
109 output file produced is derived from the name of the input file, so
110 @file{fandangoforelephants.ly} will produce
111 @file{fandangoforelephants.pdf}.
112
113 (For more details  about @code{\book} blocks, see
114 @ref{Multiple scores in a book},
115 @ref{Multiple output files from one input file} @ref{File structure}.)
116
117 @seealso
118 Learning Manual:
119 @rlearning{Working on input files},
120 @rlearning{Music expressions explained},
121 @rlearning{Score is a (single) compound musical expression}.
122
123
124 @node Multiple scores in a book
125 @subsection Multiple scores in a book
126
127 @funindex \book
128 @cindex movements, multiple
129
130 A document may contain multiple pieces of music and text.  Examples
131 of these are an etude book, or an orchestral part with multiple
132 movements.  Each movement is entered with a @code{\score} block,
133
134 @example
135 \score @{
136   @var{@dots{}music@dots{}}
137 @}
138 @end example
139
140 and texts are entered with a @code{\markup} block,
141
142 @example
143 \markup @{
144   @var{@dots{}text@dots{}}
145 @}
146 @end example
147
148 @funindex \book
149
150 All the movements and texts which appear in the same @file{.ly} file
151 will normally be typeset in the form of a single output file.
152
153 @example
154 \score @{
155   @var{@dots{}}
156 @}
157 \markup @{
158   @var{@dots{}}
159 @}
160 \score @{
161   @var{@dots{}}
162 @}
163 @end example
164
165 One important exception is within lilypond-book documents,
166 where you explicitly have to add a @code{\book} block, otherwise only
167 the first @code{\score} or @code{\markup} will appear in the output.
168
169 The header for each piece of music can be put inside the @code{\score}
170 block.  The @code{piece} name from the header will be printed before
171 each movement.  The title for the entire book can be put inside the
172 @code{\book}, but if it is not present, the @code{\header} which is at
173 the top of the file is inserted.
174
175 @example
176 \header @{
177   title = "Eight miniatures"
178   composer = "Igor Stravinsky"
179 @}
180 \score @{
181   @dots{}
182   \header @{ piece = "Romanze" @}
183 @}
184 \markup @{
185    @dots{}text of second verse@dots{}
186 @}
187 \markup @{
188    @dots{}text of third verse@dots{}
189 @}
190 \score @{
191   @dots{}
192   \header @{ piece = "Menuetto" @}
193 @}
194 @end example
195
196 @funindex \bookpart
197
198 Pieces of music may be grouped into book parts using @code{\bookpart}
199 blocks.  Book parts are separated by a page break, and can start with a
200 title, like the book itself, by specifying a @code{\header} block.
201
202 @example
203 \bookpart @{
204   \header @{
205     title = "Book title"
206     subtitle = "First part"
207   @}
208   \score @{ @dots{} @}
209   @dots{}
210 @}
211 \bookpart @{
212   \header @{
213     subtitle = "Second part"
214   @}
215   \score @{ @dots{} @}
216   @dots{}
217 @}
218 @end example
219
220 @node Multiple output files from one input file
221 @subsection Multiple output files from one input file
222
223 If you want multiple output files from the same @file{.ly} file,
224 then you can add multiple @code{\book} blocks, where each
225 such \book block will result in a separate output file.
226 If you do not specify any @code{\book} block in the
227 input file, LilyPond will implicitly treat the whole
228 file as a single \book block, see
229 @ref{File structure}.
230
231 When producing multiple files from a single source file, Lilypond
232 ensures that none of the output files from any @code{\book} block
233 overwrites the output file produced by a preceding @code{\book} from
234 the same input file.
235
236 It does this by adding a suffix to the output name for each
237 @code{\book} which uses the default output file name derived from the
238 input source file.
239
240 The default behaviour is to append a version-number suffix for each
241 name which may clash, so
242
243 @example
244 \book @{
245   \score @{ @dots{} @}
246   \paper @{ @dots{} @}
247 @}
248 \book @{
249   \score @{ @dots{} @}
250   \paper @{ @dots{} @}
251 @}
252 \book @{
253   \score @{ @dots{} @}
254   \paper @{ @dots{} @}
255 @}
256 @end example
257
258 in source file @file{eightminiatures.ly}
259 will produce
260
261 @itemize
262 @item
263 @file{eightminiatures.pdf},
264 @item
265 @file{eightminiatures-1.pdf} and
266 @item
267 @file{eightminiatures-2.pdf}.
268 @end itemize
269
270 @node Output file names
271 @subsection Output file names
272
273 @funindex \bookOutputSuffix
274 @funindex \bookOutputName
275
276 Lilypond provides facilities to allow you to control what file names
277 are used by the various back-ends when producing output files.
278
279 In the previous section, we saw how Lilypond prevents name-clashes when
280 producing several ouputs from a single source file.  You also have the
281 ability to specify your own suffixes for each @code{\book} block, so
282 for example you can produce files called
283 @file{eightminiatures-Romanze.pdf}, @file{eightminiatures-Menuetto.pdf}
284 and @file{eightminiatures-Nocturne.pdf} by adding a
285 @code{\bookOutputSuffix} declaration inside each @code{\book} block.
286
287 @example
288 \book @{
289   \bookOutputSuffix "Romanze"
290   \score @{ @dots{} @}
291   \paper @{ @dots{} @}
292 @}
293 \book @{
294   \bookOutputSuffix "Menuetto"
295   \score @{ @dots{} @}
296   \paper @{ @dots{} @}
297 @}
298 \book @{
299   \bookOutputSuffix "Nocturne"
300   \score @{ @dots{} @}
301   \paper @{ @dots{} @}
302 @}
303 @end example
304
305 You can also specify a different output filename for @code{book} block,
306 by using @code{\bookOutputName} declarations
307
308 @example
309 \book @{
310   \bookOutputName "Romanze"
311   \score @{ @dots{} @}
312   \paper @{ @dots{} @}
313 @}
314 \book @{
315   \bookOutputName "Menuetto"
316   \score @{ @dots{} @}
317   \paper @{ @dots{} @}
318 @}
319 \book @{
320   \bookOutputName "Nocturne"
321   \score @{ @dots{} @}
322   \paper @{ @dots{} @}
323 @}
324 @end example
325
326 The file above will produce these output files:
327
328 @itemize
329 @item
330 @file{Romanze.pdf},
331 @item
332 @file{Menuetto.pdf} and
333 @item
334 @file{Nocturne.pdf}.
335 @end itemize
336
337
338 @node File structure
339 @subsection File structure
340
341 @funindex \paper
342 @funindex \midi
343 @funindex \layout
344 @funindex \header
345 @funindex \score
346 @funindex \book
347 @funindex \bookpart
348
349 A @file{.ly} file may contain any number of toplevel expressions, where a
350 toplevel expression is one of the following:
351
352 @itemize
353 @item
354 An output definition, such as @code{\paper}, @code{\midi}, and
355 @code{\layout}.  Such a definition at the toplevel changes the default
356 book-wide settings.  If more than one such definition of the same type
357 is entered at the top level the definitions are combined, but in
358 conflicting situations the later definitions take precedence.  For
359 details of how this affects the @code{\layout} block see
360 @ref{The layout block,,The @code{@bs{}layout} block}.
361
362 @item
363 A direct scheme expression, such as
364 @code{#(set-default-paper-size "a7" 'landscape)} or
365 @code{#(ly:set-option 'point-and-click #f)}.
366
367 @item
368 A @code{\header} block.  This sets the global (i.e. the top of
369 file) header block.  This is the block containing the default
370 settings of titling fields like composer, title, etc. for all
371 books within the file (see @ref{Titles explained}).
372
373 @item
374 A @code{\score} block.  This score will be collected with other
375 toplevel scores, and combined as a single @code{\book}.
376 This behavior can be changed by setting the variable
377 @code{toplevel-score-handler} at toplevel.  The default handler is
378 defined in the init file @file{../scm/lily.scm}.
379
380 @item
381 A @code{\book} block logically combines multiple movements
382 (i.e., multiple @code{\score} blocks) in one document.  If there
383 are a number of @code{\score}s, one output file will be created
384 for each @code{\book} block, in which all corresponding movements
385 are concatenated.  The only reason to explicitly specify
386 @code{\book} blocks in a @file{.ly} file is if you wish to create
387 multiple output files from a single input file.  One exception is
388 within lilypond-book documents, where you explicitly have to add
389 a @code{\book} block if you want more than a single @code{\score}
390 or @code{\markup} in the same example.  This behavior can be
391 changed by setting the variable @code{toplevel-book-handler} at
392 toplevel.  The default handler is defined in the init file
393 @file{../scm/lily.scm}.
394
395 @item
396 A @code{\bookpart} block.  A book may be divided into several parts,
397 using @code{\bookpart} blocks, in order to ease the page breaking,
398 or to use different @code{\paper} settings in different parts.
399
400 @item
401 A compound music expression, such as
402 @example
403 @{ c'4 d' e'2 @}
404 @end example
405
406 This will add the piece in a @code{\score} and format it in a
407 single book together with all other toplevel @code{\score}s and music
408 expressions.  In other words, a file containing only the above
409 music expression will be translated into
410
411 @example
412 \book @{
413   \score @{
414     \new Staff @{
415       \new Voice @{
416         @{ c'4 d' e'2 @}
417       @}
418     @}
419     \layout @{ @}
420   @}
421   \paper @{ @}
422   \header @{ @}
423 @}
424 @end example
425
426 This behavior can be changed by setting the variable
427 @code{toplevel-music-handler} at toplevel.  The default handler is
428 defined in the init file @file{../scm/lily.scm}.
429
430 @item
431 A markup text, a verse for example
432 @example
433 \markup @{
434    2.  The first line verse two.
435 @}
436 @end example
437
438 Markup texts are rendered above, between or below the scores or music
439 expressions, wherever they appear.
440
441 @cindex variables
442
443 @item
444 A variable, such as
445 @example
446 foo = @{ c4 d e d @}
447 @end example
448
449 This can be used later on in the file by entering @code{\foo}.  The
450 name of a variable should have alphabetic characters only; no
451 numbers, underscores or dashes.
452
453 @end itemize
454
455 The following example shows three things that may be entered at
456 toplevel
457
458 @example
459 \layout @{
460   % Don't justify the output
461   ragged-right = ##t
462 @}
463
464 \header @{
465    title = "Do-re-mi"
466 @}
467
468 @{ c'4 d' e2 @}
469 @end example
470
471
472 At any point in a file, any of the following lexical instructions can
473 be entered:
474
475 @itemize
476 @item @code{\version}
477 @item @code{\include}
478 @item @code{\sourcefilename}
479 @item @code{\sourcefileline}
480 @item
481 A single-line comment, introduced by a leading @code{%} sign.
482
483 @item
484 A multi-line comment delimited by @code{%@{ @dots{} %@}}.
485
486 @end itemize
487
488 @cindex whitespace
489
490 Whitespace between items in the input stream is generally ignored,
491 and may be freely omitted or extended to enhance readability.
492 However, whitespace should always be used in the following
493 circumstances to avoid errors:
494
495 @itemize
496
497 @item Around every opening and closing curly bracket.
498
499 @item After every command or variable, i.e. every item that
500 begins with a @code{\} sign.
501
502 @item After every item that is to be interpreted as a Scheme
503 expression, i.e. every item that begins with a @code{#}@tie{}sign.
504
505 @item To separate all elements of a Scheme expression.
506
507 @item In @code{lyricmode} before and after @code{\set} and
508 @code{\override} commands.
509
510 @end itemize
511
512 @seealso
513 Learning Manual:
514 @rlearning{How LilyPond input files work}.
515
516 Notation Reference:
517 @ref{Titles explained},
518 @ref{The layout block,,The @code{@bs{}layout} block}.
519
520
521 @node Titles and headers
522 @section Titles and headers
523
524 @cindex titles
525 @cindex headers
526 @cindex footers
527
528 Almost all printed music includes a title and the composer's name;
529 some pieces include a lot more information.
530
531 @menu
532 * Creating titles headers and footers::
533 * Custom titles headers and footers::
534 * Creating PDF metadata::
535 * Creating footnotes::
536 * Reference to page numbers::
537 * Table of contents::
538 @end menu
539
540
541 @node Creating titles headers and footers
542 @subsection Creating titles headers and footers
543
544 @menu
545 * Titles explained::
546 * Default layout of bookpart and score titles::
547 * Default layout of headers and footers::
548 @end menu
549
550
551 @node Titles explained
552 @unnumberedsubsubsec Titles explained
553
554 Each @code{\book} block in a single input file produces a separate
555 output file, see @ref{File structure}.  Within each output file
556 three types of titling areas are provided: @emph{Book Titles} at the
557 beginning of each book, @emph{Bookpart Titles} at the beginning of
558 each bookpart and @emph{Score Titles} at the beginning of each score.
559
560 Values of titling fields such as @code{title} and @code{composer}
561 are set in @code{\header} blocks.  (For the syntax of @code{\header}
562 blocks and a complete list of the fields available by default see
563 @ref{Default layout of bookpart and score titles}).  Book Titles,
564 Bookpart Titles and Score Titles can all contain the same fields,
565 although by default the fields in Score Titles are limited to
566 @code{piece} and @code{opus}.
567
568 @code{\header} blocks may be placed in four different places to form
569 a descending hierarchy of @code{\header} blocks:
570
571 @itemize
572
573 @item
574 At the top of the input file, before all @code{\book},
575 @code{\bookpart}, and @code{\score} blocks.
576
577 @item
578 Within a @code{\book} block but outside all the @code{\bookpart} and
579 @code{\score} blocks within that book.
580
581 @item
582 Within a @code{\bookpart} block but outside all @code{\score} blocks
583 within that bookpart.
584
585 @item
586 After the music expression in a @code{\score} block.
587
588 @end itemize
589
590 The values of the fields filter down this hierarchy, with the values
591 set higher in the hierarchy persisting unless they are over-ridden
592 by a value set lower in the hierarchy, so:
593
594 @itemize
595
596 @item
597 A Book Title is derived from fields set at the top of the input file,
598 modified by fields set in the @code{\book} block.  The resulting
599 fields are used to print the Book Title for that book, providing that
600 there is other material which generates a page at the start of the
601 book, before the first bookpart.  A single @code{\pageBreak} will
602 suffice.
603
604 @item
605 A Bookpart Title is derived from fields set at the top of the input
606 file, modified by fields set in the @code{\book} block, and further
607 modified by fields set in the @code{\bookpart} block.  The resulting
608 values are used to print the Bookpart Title for that bookpart.
609
610 @item
611 A Score Title is derived from fields set at the top of the input
612 file, modified by fields set in the @code{\book} block, further
613 modified by fields set in the @code{\bookpart} block and finally
614 modified by fields set in the @code{\score} block.  The resulting
615 values are used to print the Score Title for that score.  Note,
616 though, that only @code{piece} and @code{opus} fields are printed
617 by default in Score Titles unless the @code{\paper} variable,
618 @code{print-all-headers}, is set to @code{#t}.
619
620 @end itemize
621
622 @warning{Remember when placing a @bs{}@code{header} block inside a
623 @bs{}@code{score} block, that the music expression must come before the
624 @bs{}@code{header} block.}
625
626 It is not necessary to provide @code{\header} blocks in all four
627 places: any or even all of them may be omitted.  Similarly, simple
628 input files may omit the @code{\book} and @code{\bookpart} blocks,
629 leaving them to be created implicitly.
630
631 If the book has only a single score, the @code{\header} block should
632 normally be placed at the top of the file so that just a Bookpart
633 Title is produced, making all the titling fields available for use.
634
635 If the book has multiple scores a number of different arrangements
636 of @code{\header} blocks are possible, corresponding to the various
637 types of musical publications.  For example, if the publication
638 contains several pieces by the same composer a @code{\header} block
639 placed at the top of the file specifying the book title and the
640 composer with @code{\header} blocks in each @code{\score} block
641 specifying the @code{piece} and/or @code{opus} would be most
642 suitable, as here:
643
644 @lilypond[papersize=a5,quote,verbatim,noragged-right]
645 \header {
646   title = "SUITE I."
647   composer = "J. S. Bach."
648 }
649
650 \score {
651   \new Staff \relative {
652     \clef bass
653     \key g \major
654     \repeat unfold 2 { g,16( d' b') a b d, b' d, } |
655     \repeat unfold 2 { g,16( e' c') b c e, c' e, } |
656   }
657   \header {
658     piece = "Prélude."
659   }
660 }
661
662 \score {
663   \new Staff \relative {
664     \clef bass
665     \key g \major
666     \partial 16 b16 |
667     <g, d' b'~>4 b'16 a( g fis) g( d e fis) g( a b c) |
668     d16( b g fis) g( e d c) b(c d e) fis( g a b) |
669   }
670   \header {
671     piece = "Allemande."
672   }
673 }
674 @end lilypond
675
676 More complicated arrangements are possible.  For example, text
677 fields from the @code{\header} block in a book can be displayed in
678 all Score Titles, with some fields over-ridden and some manually
679 suppressed:
680
681 @lilypond[papersize=a5,quote,verbatim,noragged-right]
682 \book {
683   \paper {
684     print-all-headers = ##t
685   }
686   \header {
687     title = "DAS WOHLTEMPERIRTE CLAVIER"
688     subtitle = "TEIL I"
689     % Do not display the tagline for this book
690     tagline = ##f
691   }
692   \markup { \vspace #1 }
693   \score {
694     \new PianoStaff <<
695       \new Staff { s1 }
696       \new Staff { \clef "bass" s1 }
697     >>
698     \header {
699       title = "PRAELUDIUM I"
700       opus = "BWV 846"
701       % Do not display the subtitle for this score
702       subtitle = ##f
703     }
704   }
705   \score {
706     \new PianoStaff <<
707       \new Staff { s1 }
708       \new Staff { \clef "bass" s1 }
709     >>
710     \header {
711       title = "FUGA I"
712       subsubtitle = "A 4 VOCI"
713       opus = "BWV 846"
714       % Do not display the subtitle for this score
715       subtitle = ##f
716     }
717   }
718 }
719 @end lilypond
720
721 @seealso
722 Notation Reference:
723 @ref{File structure},
724 @ref{Default layout of bookpart and score titles},
725 @ref{Custom layout for titles}.
726
727
728 @node Default layout of bookpart and score titles
729 @unnumberedsubsubsec Default layout of bookpart and score titles
730
731 This example demonstrates all printed @code{\header} variables:
732
733 @lilypond[papersize=a6landscape,quote,verbatim,noragged-right]
734 \book {
735   \header {
736       % The following fields are centered
737     dedication = "Dedication"
738     title = "Title"
739     subtitle = "Subtitle"
740     subsubtitle = "Subsubtitle"
741       % The following fields are evenly spread on one line
742       % the field "instrument" also appears on following pages
743     instrument = \markup \with-color #green "Instrument"
744     poet = "Poet"
745     composer = "Composer"
746       % The following fields are placed at opposite ends of the same line
747     meter = "Meter"
748     arranger = "Arranger"
749       % The following fields are centered at the bottom
750     tagline = "tagline goes at the bottom of the last page"
751     copyright = "copyright goes at the bottom of the first page"
752   }
753   \score {
754     { s1 }
755     \header {
756         % The following fields are placed at opposite ends of the same line
757       piece = "Piece 1"
758       opus = "Opus 1"
759     }
760   }
761   \score {
762     { s1 }
763     \header {
764         % The following fields are placed at opposite ends of the same line
765       piece = "Piece 2 on the same page"
766       opus = "Opus 2"
767     }
768   }
769   \pageBreak
770   \score {
771     { s1 }
772     \header {
773         % The following fields are placed at opposite ends of the same line
774       piece = "Piece 3 on a new page"
775       opus = "Opus 3"
776     }
777   }
778 }
779 @end lilypond
780
781 Note that
782
783 @itemize
784 @item
785 The instrument name will be repeated on every page.
786
787 @item
788 Only @code{piece} and @code{opus} are printed in a @code{\score}
789 when the paper variable @code{print-all-headers} is set to
790 @code{##f} (the default).
791
792 @item
793 @c Is the bit about \null markups true? -mp
794 Text fields left unset in a @code{\header} block are replaced with
795 @code{\null} markups so that the space is not wasted.
796
797 @item
798 The default settings for @code{scoreTitleMarkup} place the @code{piece}
799 and @code{opus} text fields at opposite ends of the same line.
800
801 @end itemize
802
803 To change the default layout see @ref{Custom layout for titles}.
804
805 @cindex breakbefore
806
807 If a @code{\book} block starts immediately with a @code{\bookpart}
808 block, no Book Title will be printed, as there is no page on which
809 to print it.  If a Book Title is required, begin the @code{\book}
810 block with some markup material or a @code{\pageBreak} command.
811
812 Use the @code{breakbefore} variable inside a @code{\header} block
813 that is itself in a @code{\score} block, to make the higher-level
814 @code{\header} block titles appear on the first page on their own, with
815 the music (defined in the @code{\score} block) starting on the next.
816
817 @lilypond[papersize=c7landscape,verbatim,noragged-right]
818 \book {
819   \header {
820     title = "This is my Title"
821     subtitle = "This is my Subtitle"
822     copyright = "This is the bottom of the first page"
823   }
824   \score {
825     \repeat unfold 4 { e'' e'' e'' e'' }
826     \header {
827       piece = "This is the Music"
828       breakbefore = ##t
829     }
830   }
831 }
832 @end lilypond
833
834 @seealso
835 Learning Manual:
836 @rlearning{How LilyPond input files work},
837
838 Notation Reference:
839 @ref{Custom layout for titles},
840 @ref{File structure}.
841
842 Installed Files:
843 @file{ly/titling-init.ly}.
844
845
846 @node Default layout of headers and footers
847 @unnumberedsubsubsec Default layout of headers and footers
848
849 @emph{Headers} and @emph{footers} are lines of text appearing at
850 the top and bottom of pages, separate from the main text of a book.
851 They are controlled by the following @code{\paper} variables:
852
853 @itemize
854 @item @code{oddHeaderMarkup}
855 @item @code{evenHeaderMarkup}
856 @item @code{oddFooterMarkup}
857 @item @code{evenFooterMarkup}
858 @end itemize
859
860 These markup variables can only access text fields from top-level
861 @code{\header} blocks (which apply to all scores in the book) and are
862 defined in @file{ly/titling-init.ly}.  By default:
863
864 @itemize
865
866 @item
867 page numbers are automatically placed on the top far left (if even) or
868 top far right (if odd), starting from the second page.
869
870 @item
871 the @code{instrument} text field is placed in the center of every
872 page, starting from the second page.
873
874 @item
875 the @code{copyright} text is centered on the bottom of the first page.
876
877 @item
878 the @code{tagline} is centered on the bottom of the last page, and below
879 the @code{copyright} text if there is only a single page.
880
881 @end itemize
882
883 The default tagline can be changed by adding a @code{tagline} in the
884 top-level @code{\header} block.
885
886 @lilypond[papersize=a8landscape,verbatim]
887 \book {
888   \header {
889     tagline = "... music notation for Everyone"
890   }
891   \score {
892     \relative {
893       c'4 d e f
894     }
895   }
896 }
897 @end lilypond
898
899 To remove the @code{tagline} set the value to @code{##f}.
900
901
902 @node Custom titles headers and footers
903 @subsection Custom titles headers and footers
904
905 @c TODO: somewhere put a link to header spacing info
906 @c       (you'll have to explain it more in NR 4).
907
908 @menu
909 * Custom text formatting for titles::
910 * Custom layout for titles::
911 * Custom layout for headers and footers::
912 @end menu
913
914
915 @node Custom text formatting for titles
916 @unnumberedsubsubsec Custom text formatting for titles
917
918 Standard @code{\markup} commands can be used to customize any header,
919 footer and title text within the @code{\header} block.
920
921 @lilypond[quote,verbatim,noragged-right]
922 \score {
923   { s1 }
924   \header {
925     piece = \markup { \fontsize #4 \bold "PRAELUDIUM I" }
926     opus = \markup { \italic "BWV 846" }
927   }
928 }
929 @end lilypond
930
931 @seealso
932 Notation Reference:
933 @ref{Formatting text}.
934
935
936 @node Custom layout for titles
937 @unnumberedsubsubsec Custom layout for titles
938
939 @cindex bookTitleMarkup
940 @cindex scoreTitleMarkup
941 @funindex bookTitleMarkup
942 @funindex scoreTitleMarkup
943
944 @code{\markup} commands in the @code{\header} block are useful for
945 simple text formatting, but they do not allow precise control over the
946 placement of titles.  To customize the placement of the text fields,
947 change either or both of the following @code{\paper} variables:
948
949 @itemize
950 @item @code{bookTitleMarkup}
951 @item @code{scoreTitleMarkup}
952 @end itemize
953
954 The placement of titles when using the default values of these
955 @code{\markup} variables is shown in the examples in
956 @ref{Default layout of bookpart and score titles}.
957
958 The default settings for @code{scoreTitleMarkup} as defined in
959 @file{ly/titling-init.ly} are:
960
961 @example
962 scoreTitleMarkup = \markup @{ \column @{
963   \on-the-fly \print-all-headers @{ \bookTitleMarkup \hspace #1 @}
964   \fill-line @{
965     \fromproperty #'header:piece
966     \fromproperty #'header:opus
967   @}
968 @}
969 @}
970 @end example
971
972 This places the @code{piece} and @code{opus} text fields at opposite
973 ends of the same line:
974
975 @lilypond[quote,verbatim,noragged-right]
976 \score {
977   { s1 }
978   \header {
979     piece = "PRAELUDIUM I"
980     opus = "BWV 846"
981   }
982 }
983 @end lilypond
984
985 This example redefines @code{scoreTitleMarkup} so that the @code{piece}
986 text field is centered and in a large, bold font.
987
988 @lilypond[papersize=a5,quote,verbatim,noragged-right]
989 \book {
990   \paper {
991     indent = 0\mm
992     scoreTitleMarkup = \markup {
993       \fill-line {
994         \null
995         \fontsize #4 \bold \fromproperty #'header:piece
996         \fromproperty #'header:opus
997       }
998     }
999   }
1000   \header { tagline = ##f }
1001   \score {
1002     { s1 }
1003     \header {
1004       piece = "PRAELUDIUM I"
1005       opus = "BWV 846"
1006     }
1007   }
1008 }
1009 @end lilypond
1010
1011 Text fields not normally effective in score @code{\header} blocks
1012 can be printed in the Score Title area if @code{print-all-headers} is
1013 placed inside the @code{\paper} block.  A disadvantage of using this
1014 method is that text fields that are intended specifically for the
1015 Bookpart Title area need to be manually suppressed in every
1016 @code{\score} block.  See @ref{Titles explained}.
1017
1018 To avoid this, add the desired text field to the @code{scoreTitleMarkup}
1019 definition.  In the following example, the @code{composer} text field
1020 (normally associated with @code{bookTitleMarkup}) is added to
1021 @code{scoreTitleMarkup}, allowing each score to list a different
1022 composer:
1023
1024 @lilypond[papersize=a5,quote,verbatim,noragged-right]
1025 \book {
1026   \paper {
1027     indent = 0\mm
1028     scoreTitleMarkup = \markup {
1029       \fill-line {
1030         \null
1031         \fontsize #4 \bold \fromproperty #'header:piece
1032         \fromproperty #'header:composer
1033       }
1034     }
1035   }
1036   \header { tagline = ##f }
1037   \score {
1038     { s1 }
1039     \header {
1040       piece = "MENUET"
1041       composer = "Christian Petzold"
1042     }
1043   }
1044   \score {
1045     { s1 }
1046     \header {
1047       piece = "RONDEAU"
1048       composer = "François Couperin"
1049     }
1050   }
1051 }
1052 @end lilypond
1053
1054 It is also possible to create your own custom text fields, and refer to
1055 them in the markup definition.
1056
1057 @lilypond[papersize=a5,quote,verbatim,noragged-right]
1058 \book {
1059   \paper {
1060     indent = 0\mm
1061     scoreTitleMarkup = \markup {
1062       \fill-line {
1063         \null
1064         \override #`(direction . ,UP) {
1065           \dir-column {
1066             \center-align \fontsize #-1 \bold
1067               \fromproperty #'header:mycustomtext %% User-defined field
1068             \center-align \fontsize #4 \bold
1069               \fromproperty #'header:piece
1070           }
1071         }
1072         \fromproperty #'header:opus
1073       }
1074     }
1075   }
1076   \header { tagline = ##f }
1077   \score {
1078     { s1 }
1079     \header {
1080       piece = "FUGA I"
1081       mycustomtext = "A 4 VOCI" %% User-defined field
1082       opus = "BWV 846"
1083     }
1084   }
1085 }
1086 @end lilypond
1087
1088 @seealso
1089 Notation Reference:
1090 @ref{Titles explained}.
1091
1092
1093 @node Custom layout for headers and footers
1094 @unnumberedsubsubsec Custom layout for headers and footers
1095
1096 @c can make-header and make-footer be removed from
1097 @c paper-defaults-init.ly? -mp
1098
1099 @code{\markup} commands in the @code{\header} block are useful for
1100 simple text formatting, but they do not allow precise control over the
1101 placement of headers and footers.  To customize the placement of
1102 the text fields, use either or both of the following @code{\paper}
1103 variables:
1104
1105 @itemize
1106 @item @code{oddHeaderMarkup}
1107 @item @code{evenHeaderMarkup}
1108 @item @code{oddFooterMarkup}
1109 @item @code{evenFooterMarkup}
1110 @end itemize
1111
1112 @cindex markup, conditional
1113 @cindex on-the-fly
1114 @funindex \on-the-fly
1115
1116 The @code{\markup} command @code{\on-the-fly} can be used to add
1117 markup conditionally to header and footer text defined within the
1118 @code{\paper} block, using the following syntax:
1119
1120 @example
1121 @code{variable} = @code{\markup} @{
1122   @dots{}
1123   @code{\on-the-fly}  \@var{procedure}  @var{markup}
1124   @dots{}
1125 @}
1126 @end example
1127
1128 The @var{procedure} is called each time the @code{\markup} command
1129 in which it appears is evaluated.  The @var{procedure} should test
1130 for a particular condition and interpret (i.e. print) the
1131 @var{markup} argument if and only if the condition is true.
1132
1133 A number of ready-made procedures for testing various conditions are
1134 provided:
1135
1136 @quotation
1137 @multitable {print-page-number-check-first-----} {should this page be printed-----}
1138
1139 @headitem  Procedure name           @tab  Condition tested
1140
1141 @item print-page-number-check-first @tab  should this page number be printed?
1142 @item create-page-number-stencil    @tab  print-page-numbers true?
1143 @item print-all-headers             @tab  print-all-headers true?
1144 @item first-page                    @tab  first page in the book?
1145 @item (on-page nmbr)                @tab  page number = nmbr?
1146 @item last-page                     @tab  last page in the book?
1147 @item not-first-page                @tab  not first page in the book?
1148 @item part-first-page               @tab  first page in the book part?
1149 @item part-last-page                @tab  last page in the book part?
1150 @item not-single-page               @tab  pages in book part > 1?
1151
1152 @end multitable
1153 @end quotation
1154
1155 The following example centers page numbers at the bottom of every
1156 page.  First, the default settings for @code{oddHeaderMarkup} and
1157 @code{evenHeaderMarkup} are removed by defining each as a @emph{null}
1158 markup.  Then, @code{oddFooterMarkup} is redefined with the page
1159 number centered.  Finally, @code{evenFooterMarkup} is given the
1160 same layout by defining it as @code{\oddFooterMarkup}:
1161
1162 @lilypond[papersize=a8,quote,verbatim,noragged-right]
1163 \book {
1164   \paper {
1165     print-page-number = ##t
1166     print-first-page-number = ##t
1167     oddHeaderMarkup = \markup \null
1168     evenHeaderMarkup = \markup \null
1169     oddFooterMarkup = \markup {
1170       \fill-line {
1171         \on-the-fly \print-page-number-check-first
1172         \fromproperty #'page:page-number-string
1173       }
1174     }
1175     evenFooterMarkup = \oddFooterMarkup
1176   }
1177   \score {
1178     \new Staff { s1 \break s1 \break s1 }
1179   }
1180 }
1181 @end lilypond
1182
1183 Several @code{\on-the-fly} conditions can be combined with an
1184 @q{and} operation, for example,
1185
1186 @example
1187   @code{\on-the-fly \first-page}
1188   @code{\on-the-fly \last-page}
1189   @code{@{ \markup @dots{} \fromproperty #'header: @dots{} @}}
1190 @end example
1191
1192 determines if the output is a single page.
1193
1194 @seealso
1195 Notation Reference:
1196 @ref{Titles explained},
1197 @ref{Default layout of bookpart and score titles}.
1198
1199 Installed Files:
1200 @file{../ly/titling-init.ly}.
1201
1202 @node Creating PDF metadata
1203 @subsection Creating PDF metadata
1204
1205 @cindex PDF metadata
1206
1207 In addition to being shown in the printed output, @code{\header} variables
1208 are also used to set PDF metadata (the information displayed by PDF readers
1209 as the @code{properties} of the PDF file).  For example, setting the
1210 @code{title} property of the @code{header} block @q{Symphony I} will also give
1211 this title to the PDF document.
1212
1213 @example
1214   @code{\header@{}
1215     @code{title = "Symphony I"}
1216   @code{@}}
1217 @end example
1218
1219 If you want to set the title of the printed output to one value, but have the
1220 title property of the PDF to have a different value, you can use
1221 @code{pdftitle}, as below.
1222
1223 @example
1224   @code{\header@{}
1225     @code{title = "Symphony I"}
1226     @code{pdftitle = "Symphony I by Beethoven"}
1227   @code{@}}
1228 @end example
1229
1230 The variables @code{title}, @code{subject}, @code{keywords},
1231 @code{subtitle}, @code{composer}, @code{arranger}, @code{poet}, @code{author}
1232 and @code{copyright} all set PDF properties and can all be prefixed with
1233 @q{pdf} to set a PDF property to a value different from the printed output.
1234
1235 The PDF property @code{Creator} is automatically set to @q{LilyPond} plus
1236 the current LilyPond version, and @code{CreationDate} and @code{ModDate} are
1237 both set to the current date and time.  @code{ModDate} can be overridden by
1238 setting the header variable @code{moddate} (or @code{pdfmoddate}) to a
1239 valid PDF date string.
1240
1241 @node Creating footnotes
1242 @subsection Creating footnotes
1243
1244 @cindex footnotes
1245
1246 Footnotes may be used in many different situations.  In all cases,
1247 a @q{footnote mark} is placed as a reference in text or music, and
1248 the corresponding @q{footnote text} appears at the bottom of the
1249 same page.
1250
1251 Footnotes within music expressions and footnotes in stand-alone text
1252 outside music expressions are created in different ways.
1253
1254 @menu
1255 * Footnotes in music expressions::
1256 * Footnotes in stand-alone text::
1257 @end menu
1258
1259 @node Footnotes in music expressions
1260 @unnumberedsubsubsec Footnotes in music expressions
1261
1262 @cindex footnotes in music expressions
1263 @funindex \footnote
1264
1265 @subsubsubheading Music footnotes overview
1266
1267 Footnotes in music expressions fall into two categories:
1268
1269 @table @emph
1270 @item Event-based footnotes
1271 are attached to a particular event.  Examples for such events are
1272 single notes, articulations (like fingering indications, accents,
1273 dynamics), and post-events (like slurs and manual beams).  The
1274 general form for event-based footnotes is as follows:
1275
1276 @example
1277 [@var{direction}] \footnote [@var{mark}] @var{offset} @var{footnote} @var{music}
1278 @end example
1279
1280 @item Time-based footnotes
1281 are bound to a particular point of time in a musical context.  Some
1282 commands like @code{\time} and @code{\clef} don't actually use events
1283 for creating objects like time signatures and clefs.  Neither does a
1284 chord create an event of its own: its stem or flag is created at the
1285 end of a time step (nominally through one of the note events inside).
1286 Exactly which of a chord's multiple note events will be deemed the
1287 root cause of a stem or flag is undefined.  So for annotating those,
1288 time-based footnotes are preferable as well.
1289
1290 A time-based footnote allows such layout objects to be annotated
1291 without referring to an event.  The general form for Time-based
1292 footnotes is:
1293
1294 @example
1295 \footnote [@var{mark}] @var{offset} @var{footnote} [@var{Context}].@var{GrobName}
1296 @end example
1297
1298 @end table
1299
1300 The elements for both forms are:
1301
1302 @table @var
1303
1304 @item direction
1305 If (and only if) the @code{\footnote} is being applied to a
1306 post-event or articulation, it must be preceded with a direction
1307 indicator (@code{-, _, ^}) in order to attach @var{music} (with
1308 a footnote mark) to the preceding note or rest.
1309
1310 @item mark
1311 is a markup or string specifying the footnote mark which is used for
1312 marking both the reference point and the footnote itself at the
1313 bottom of the page.  It may be omitted (or equivalently replaced with
1314 @code{\default}) in which case a number in sequence will be generated
1315 automatically.  Such numerical sequences restart on each page
1316 containing a footnote.
1317
1318 @item offset
1319 is a number pair such as @samp{#(2 . 1)} specifying the X and
1320 Y@tie{}offsets in units of staff-spaces from the boundary of the
1321 object where the mark should be placed.  Positive values of the
1322 offsets are taken from the right/top edge, negative values from the
1323 left/bottom edge and zero implies the mark is centered on the edge.
1324
1325 @item Context
1326 is the context in which the grob being footnoted is created.  It
1327 may be omitted if the grob is in a bottom context, e.g. a
1328 @code{Voice} context.
1329
1330 @item GrobName
1331 specifies a type of grob to mark (like @samp{Flag}).  If it is
1332 specified, the footnote is not attached to a music expression in
1333 particular, but rather to all grobs of the type specified which
1334 occur at that moment of musical time.
1335
1336 @item footnote
1337 is the markup or string specifying the footnote text to use at the
1338 bottom of the page.
1339
1340 @item music
1341 is the music event or post-event or articulation
1342 that is being annotated.
1343
1344 @end table
1345
1346 @subsubsubheading Event-based footnotes
1347
1348 @cindex footnotes, event-based
1349
1350 A footnote may be attached to a layout object directly caused
1351 by the event corresponding to @var{music} with the syntax:
1352
1353 @example
1354 \footnote [@var{mark}] @var{offset} @var{footnote} @var{music}
1355 @end example
1356
1357 @lilypond[quote,verbatim,papersize=a8landscape]
1358 \book {
1359   \header { tagline = ##f }
1360   \relative c'' {
1361     \footnote #'(-1 . 3) "A note" a4
1362     a4
1363     \footnote #'(2 . 2) "A rest" r4
1364     a4
1365   }
1366 }
1367 @end lilypond
1368
1369 Marking a @emph{whole} chord with an event-based footnote is not
1370 possible: a chord, even one containing just a single note, does
1371 not produce an actual event of its own.  However, individual
1372 notes @emph{inside} of the chord can be marked:
1373
1374 @lilypond[quote,verbatim,papersize=a8landscape]
1375 \book {
1376   \header { tagline = ##f }
1377   \relative c'' {
1378     \footnote #'(2 . 3) "Does not work" <a-3>2
1379     <\footnote #'(-2 . -3) "Does work" a-3>4
1380     <a-3 \footnote #'(3 . 1/2) "Also works" c-5>4
1381   }
1382 }
1383 @end lilypond
1384
1385 If the footnote is to be attached to a post-event or articulation
1386 the @code{\footnote} command @emph{must} be preceded by a direction
1387 indicator, @code{-, _, ^}, and followed by the post-event or
1388 articulation to be annotated as the @var{music} argument.  In this
1389 form the @code{\footnote} can be considered to be simply a copy of
1390 its last argument with a footnote mark attached to it.  The syntax
1391 is:
1392
1393 @example
1394 @var{direction} \footnote [@var{mark}] @var{offset} @var{footnote} @var{music}
1395 @end example
1396
1397 @lilypond[quote,verbatim,papersize=a8landscape]
1398 \book {
1399   \header { tagline = ##f }
1400   \relative {
1401     a'4_\footnote #'(0 . -1) "A slur forced down" (
1402     b8^\footnote #'(1 . 0.5) "A manual beam forced up" [
1403     b8 ]
1404     c4 )
1405     c-\footnote #'(1 . 1) "Tenuto" --
1406   }
1407 }
1408 @end lilypond
1409
1410 @subsubsubheading Time-based footnotes
1411
1412 @cindex footnotes, time-based
1413
1414 If the layout object being footmarked is @emph{indirectly} caused by
1415 an event (like an @code{Accidental} or @code{Stem} caused by a
1416 @code{NoteHead} event), the @var{GrobName} of the layout object
1417 is required after the footnote text instead of @var{music}:
1418
1419 @lilypond[quote,verbatim,papersize=a8landscape]
1420 \book {
1421   \header { tagline = ##f }
1422   \relative c'' {
1423     \footnote #'(-1 . -3) "A flat" Accidental
1424     aes4 c
1425     \footnote #'(-1 . 0.5) "Another flat" Accidental
1426     ees
1427     \footnote #'(1 . -2) "A stem" Stem
1428     aes
1429   }
1430 }
1431 @end lilypond
1432
1433 Note, however, that when a GrobName is specified, a footnote
1434 will be attached to all grobs of that type at the current time step:
1435
1436 @lilypond[quote,verbatim,papersize=a8landscape]
1437 \book {
1438   \header { tagline = ##f }
1439   \relative c' {
1440     \footnote #'(-1 . 3) "A flat" Accidental
1441     <ees ges bes>4
1442     \footnote #'(2 . 0.5) "Articulation" Script
1443     c'->-.
1444   }
1445 }
1446 @end lilypond
1447
1448 A note inside of a chord can be given an individual (event-based)
1449 footnote.  A @samp{NoteHead} is the only grob directly caused
1450 from a chord note, so an event-based footnote command is
1451 @emph{only} suitable for adding a footnote to the @samp{NoteHead}
1452 within a chord.  All other chord note grobs are indirectly caused.
1453 The @code{\footnote} command itself offers no syntax for
1454 specifying @emph{both} a particular grob type @emph{as well as} a
1455 particular event to attach to.  However, one can use a time-based
1456 @code{\footnote} command for specifying the grob type, and then
1457 prefix this command with @code{\single} in order to have it
1458 applied to just the following event:
1459
1460 @lilypond[quote,verbatim,papersize=a8landscape]
1461 \book {
1462   \header { tagline = ##f }
1463   \relative c'' {
1464     < \footnote #'(1 . -2) "An A" a
1465       \single \footnote #'(-1 . -1) "A sharp" Accidental
1466       cis
1467       \single \footnote #'(0.5 . 0.5) "A flat" Accidental
1468       ees fis
1469     >2
1470   }
1471 }
1472 @end lilypond
1473
1474 @warning {When footnotes are attached to several musical elements at
1475 the same musical moment, as they are in the example above, the
1476 footnotes are numbered from the higher to the lower elements as they
1477 appear in the printed output, not in the order in which they are
1478 written in the input stream.}
1479
1480 Layout objects like clefs and key-change signatures are mostly caused
1481 as a consequence of changed properties rather than actual events.
1482 Others, like bar lines and bar numbers, are a direct consequence of
1483 timing.  For this reason, footnotes on such objects have to be based
1484 on their musical timing.  Time-based footnotes are also preferable
1485 when marking features like stems and beams on @emph{chords}: while
1486 such per-chord features are nominally assigned to @emph{one} event
1487 inside the chord, relying on a particular choice would be imprudent.
1488
1489 The layout object in question must always be explicitly specified
1490 for time-based footnotes, and the appropriate context must be
1491 specified if the grob is created in a context other than the bottom
1492 context.
1493
1494 @lilypond[quote,verbatim,papersize=a8landscape]
1495 \book {
1496   \header { tagline = ##f }
1497   \relative c'' {
1498     r1 |
1499     \footnote #'(-0.5 . -1) "Meter change" Staff.TimeSignature
1500     \time 3/4
1501     \footnote #'(1 . -1) "Chord stem" Stem
1502     <c e g>4 q q
1503     \footnote #'(-0.5 . 1) "Bar line" Staff.BarLine
1504     q q
1505     \footnote #'(0.5 . -1) "Key change" Staff.KeySignature
1506     \key c \minor
1507     q
1508   }
1509 }
1510 @end lilypond
1511
1512 Custom marks can be used as alternatives to numerical marks, and the
1513 annotation line joining the marked object to the mark can be
1514 suppressed:
1515
1516 @lilypond[quote,verbatim,papersize=a8landscape]
1517 \book {
1518   \header { tagline = ##f }
1519   \relative c' {
1520     \footnote "*" #'(0.5 . -2) \markup { \italic "* The first note" } a'4
1521     b8
1522     \footnote \markup { \super "$" } #'(0.5 . 1)
1523       \markup { \super "$" \italic " The second note" } e
1524     c4
1525     \once \override Score.FootnoteItem.annotation-line = ##f
1526     b-\footnote \markup \tiny "+" #'(0.1 . 0.1)
1527       \markup { \super "+" \italic " Editorial" } \p
1528   }
1529 }
1530 @end lilypond
1531
1532 More examples of custom marks are shown in
1533 @ref{Footnotes in stand-alone text}.
1534
1535
1536 @node Footnotes in stand-alone text
1537 @unnumberedsubsubsec Footnotes in stand-alone text
1538
1539 @cindex footnotes in stand-alone text
1540
1541 These are for use in markup outside of music expressions.  They do
1542 not have a line drawn to their point of reference: their marks simply
1543 follow the referenced markup.  Marks can be inserted automatically,
1544 in which case they are numerical.  Alternatively, custom marks can be
1545 provided manually.
1546
1547 Footnotes to stand-alone text with automatic and custom marks are
1548 created in different ways.
1549
1550 @subsubsubheading Footnotes in stand-alone text with automatic marks
1551
1552 The syntax of a footnote in stand-alone text with automatic marks is
1553
1554 @example
1555 \markup @{ @dots{} \auto-footnote @var{text} @var{footnote} @dots{} @}
1556 @end example
1557
1558 The elements are:
1559
1560 @table @var
1561
1562 @item text
1563 is the markup or string to be marked.
1564
1565 @item footnote
1566 is the markup or string specifying the footnote text to use at the bottom
1567 of the page.
1568
1569 @end table
1570
1571 For example:
1572
1573 @lilypond[verbatim,quote,ragged-right,papersize=a8]
1574 \book {
1575   \header { tagline = ##f }
1576   \markup {
1577     "A simple"
1578     \auto-footnote "tune" \italic " By me"
1579     "is shown below.  It is a"
1580     \auto-footnote "recent" \italic " Aug 2012"
1581     "composition."
1582   }
1583   \relative {
1584     a'4 b8 e c4 d
1585   }
1586 }
1587 @end lilypond
1588
1589 @subsubsubheading Footnotes in stand-alone text with custom marks
1590
1591 The syntax of a footnote in stand-alone text with custom marks is
1592
1593 @example
1594 \markup @{ @dots{} \footnote @var{mark} @var{footnote} @dots{} @}
1595 @end example
1596
1597 The elements are:
1598
1599 @table @var
1600
1601 @item mark
1602 is a markup or string specifying the footnote mark which is used for
1603 marking the reference point.  Note that this mark is @emph{not}
1604 inserted automatically before the footnote itself.
1605
1606 @item footnote
1607 is the markup or string specifying the footnote text to use at the
1608 bottom of the page, preceded by the @var{mark}.
1609
1610 @end table
1611
1612 Any easy-to-type character such as * or + may be used as a mark, as
1613 shown in @ref{Footnotes in music expressions}.  Alteratively, ASCII
1614 aliases may be used (see @ref{ASCII aliases}):
1615
1616 @lilypond[verbatim,quote,ragged-right,papersize=a8]
1617 \book {
1618   \paper { #(include-special-characters) }
1619   \header { tagline = ##f }
1620   \markup {
1621     "A simple tune"
1622     \footnote "*" \italic "* By me"
1623     "is shown below.  It is a recent"
1624     \footnote \super &dagger; \concat {
1625       \super &dagger; \italic " Aug 2012"
1626     }
1627     "composition."
1628   }
1629   \relative {
1630     a'4 b8 e c4 d
1631   }
1632 }
1633 @end lilypond
1634
1635 Unicode character codes may also be used to specify marks
1636 (see @ref{Unicode}):
1637
1638 @lilypond[verbatim,quote,ragged-right,papersize=a8]
1639 \book {
1640   \header { tagline = ##f }
1641   \markup {
1642     "A simple tune"
1643     \footnote \super \char##x00a7 \concat {
1644       \super \char##x00a7 \italic " By me"
1645     }
1646     "is shown below.  It is a recent"
1647     \footnote \super \char##x00b6 \concat {
1648       \super \char##x00b6 \italic " Aug 2012"
1649     }
1650     "composition."
1651   }
1652   \relative {
1653     a'4 b8 e c4 d
1654   }
1655 }
1656 @end lilypond
1657
1658 @seealso
1659 Learning Manual:
1660 @rlearning{Objects and interfaces}.
1661
1662 Notation Reference:
1663 @ref{ASCII aliases},
1664 @ref{Balloon help},
1665 @ref{List of special characters},
1666 @ref{Text marks},
1667 @ref{Text scripts},
1668 @ref{Unicode}.
1669
1670 Internals Reference:
1671 @rinternals{FootnoteEvent},
1672 @rinternals{FootnoteItem},
1673 @rinternals{FootnoteSpanner},
1674 @rinternals{Footnote_engraver}.
1675
1676 @knownissues
1677 Multiple footnotes for the same page can only be stacked, one above
1678 the other; they cannot be printed on the same line.
1679
1680 Footnotes cannot be attached to @code{MultiMeasureRests} or
1681 automatic beams or lyrics.
1682
1683 Footnote marks may collide with staves, @code{\markup} objects, other
1684 footnote marks and annotation lines.
1685
1686
1687 @node Reference to page numbers
1688 @subsection Reference to page numbers
1689
1690 A particular place of a score can be marked using the @code{\label}
1691 command, either at top-level or inside music.  This label can then be
1692 referred to in a markup, to get the number of the page where the marked
1693 point is placed, using the @code{\page-ref} markup command.
1694
1695 @lilypond[verbatim,papersize=a8landscape]
1696 \header { tagline = ##f }
1697 \book {
1698   \label #'firstScore
1699   \score {
1700     {
1701       c'1
1702       \pageBreak \mark A \label #'markA
1703       c'1
1704     }
1705   }
1706   \markup { The first score begins on page \page-ref #'firstScore "0" "?" }
1707   \markup { Mark A is on page \page-ref #'markA "0" "?" }
1708 }
1709 @end lilypond
1710
1711 The @code{\page-ref} markup command takes three arguments:
1712 @enumerate
1713 @item the label, a scheme symbol, eg. @code{#'firstScore};
1714 @item a markup that will be used as a gauge to estimate the dimensions
1715 of the markup;
1716 @item a markup that will be used in place of the page number if the label
1717 is not known;
1718 @end enumerate
1719
1720 The reason why a gauge is needed is that, at the time markups are
1721 interpreted, the page breaking has not yet occurred, so the page numbers
1722 are not yet known.  To work around this issue, the actual markup
1723 interpretation is delayed to a later time; however, the dimensions of
1724 the markup have to be known before, so a gauge is used to decide these
1725 dimensions.  If the book has between 10 and 99 pages, it may be "00",
1726 ie. a two digit number.
1727
1728
1729 @predefined
1730 @funindex \label
1731 @code{\label},
1732 @funindex \page-ref
1733 @code{\page-ref}.
1734 @endpredefined
1735
1736
1737 @node Table of contents
1738 @subsection Table of contents
1739 A table of contents is included using the
1740 @code{\markuplist \table-of-contents} command.  The elements which
1741 should appear in the table of contents are entered with the
1742 @code{\tocItem} command, which may be used either at top-level, or
1743 inside a music expression.
1744
1745 @verbatim
1746 \markuplist \table-of-contents
1747 \pageBreak
1748
1749 \tocItem \markup "First score"
1750 \score {
1751   {
1752     c'4  % ...
1753     \tocItem \markup "Some particular point in the first score"
1754     d'4  % ...
1755   }
1756 }
1757
1758 \tocItem \markup "Second score"
1759 \score {
1760   {
1761     e'4 % ...
1762   }
1763 }
1764 @end verbatim
1765
1766 Markups used for formatting the table of contents are defined in the
1767 @code{\paper} block.  There two @q{pre-defined} markups already
1768 available;
1769
1770 @itemize
1771
1772 @item
1773 @code{tocTitleMarkup}
1774
1775 @noindent
1776 Used for formatting the title of the table of contents.
1777
1778 @verbatim
1779 tocTitleMarkup = \markup \huge \column {
1780   \fill-line { \null "Table of Contents" \null }
1781   \null
1782 }
1783 @end verbatim
1784
1785 @item
1786 @code{tocItemMarkup}
1787
1788 @noindent
1789 Used for formatting the elements within the table of contents.
1790
1791 @verbatim
1792 tocItemMarkup = \markup \fill-line {
1793   \fromproperty #'toc:text \fromproperty #'toc:page
1794 }
1795 @end verbatim
1796
1797 @end itemize
1798
1799 @noindent
1800 Both of these variables can be changed.
1801
1802 Here is an example changing the table of contents' title into French;
1803
1804 @verbatim
1805 \paper {
1806   tocTitleMarkup = \markup \huge \column {
1807     \fill-line { \null "Table des matières" \null }
1808     \hspace #1
1809   }
1810 @end verbatim
1811
1812 Here is an example changing the font-size of the elements in the table
1813 of contents;
1814
1815 @verbatim
1816 tocItemMarkup = \markup \large \fill-line {
1817   \fromproperty #'toc:text \fromproperty #'toc:page
1818 }
1819 @end verbatim
1820
1821 Note how the element text and page numbers are referred to in the
1822 @code{tocItemMarkup} definition.
1823
1824 The @code{\tocItemWithDotsMarkup} command can be included within the
1825 @code{tocItemMarkup} to fill the line, between a table of contents item
1826 and its corresponding page number, with dots;
1827
1828 @lilypond[verbatim,line-width=10.0\cm]
1829 \header { tagline = ##f }
1830 \paper {
1831   tocItemMarkup = \tocItemWithDotsMarkup
1832 }
1833
1834 \book {
1835   \markuplist \table-of-contents
1836   \tocItem \markup { Allegro }
1837   \tocItem \markup { Largo }
1838   \markup \null
1839 }
1840 @end lilypond
1841
1842 Custom commands with their own markups can also be defined to build a
1843 more complex table of contents.  In the following example, a new style
1844 is defined for entering act names in a table of contents of an opera;
1845
1846 @noindent
1847 A new markup variable (called @code{tocActMarkup}) is defined in the
1848 @code{\paper} block ;
1849
1850 @verbatim
1851 \paper {
1852   tocActMarkup = \markup \large \column {
1853     \hspace #1
1854     \fill-line { \null \italic \fromproperty #'toc:text \null }
1855     \hspace #1
1856   }
1857 }
1858 @end verbatim
1859
1860 @noindent
1861 A custom music function (@code{tocAct}) is then created -- which uses
1862 the new @code{tocActMarkup} markup definition.
1863
1864 @verbatim
1865 tocAct =
1866   #(define-music-function (text) (markup?)
1867      (add-toc-item! 'tocActMarkup text))
1868 @end verbatim
1869
1870 @noindent
1871 A LilyPond input file, using these customer definitions, could look
1872 something like this;
1873
1874 @lilypond[line-width=10.0\cm]
1875 \header { tagline = ##f }
1876 \paper {
1877   tocActMarkup = \markup \large \column {
1878     \hspace #1
1879     \fill-line { \null \italic \fromproperty #'toc:text \null }
1880     \hspace #1
1881   }
1882 }
1883
1884 tocAct =
1885 #(define-music-function (text) (markup?)
1886    (add-toc-item! 'tocActMarkup text))
1887
1888 \book {
1889   \markuplist \table-of-contents
1890   \tocAct \markup { Atto Primo }
1891   \tocItem \markup { Coro. Viva il nostro Alcide }
1892   \tocItem \markup { Cesare. Presti omai l'Egizzia terra }
1893   \tocAct \markup { Atto Secondo }
1894   \tocItem \markup { Sinfonia }
1895   \tocItem \markup { Cleopatra. V'adoro, pupille, saette d'Amore }
1896   \markup \null
1897 }
1898 @end lilypond
1899
1900
1901 Here is an example of the @code{\fill-with-pattern} command used within
1902 the context of a table of contents;
1903
1904 @verbatim
1905 \paper {
1906   tocItemMarkup = \markup { \fill-line {
1907     \override #'(line-width . 70)
1908     \fill-with-pattern #1.5 #CENTER . \fromproperty #'toc:text \fromproperty #'toc:page
1909     }
1910   }
1911 }
1912 @end verbatim
1913
1914 @seealso
1915 Installed Files:
1916 @file{ly/toc-init.ly}.
1917
1918 @predefined
1919 @funindex \table-of-contents
1920 @code{\table-of-contents},
1921 @funindex \tocItem
1922 @code{\tocItem}.
1923 @endpredefined
1924
1925
1926 @node Working with input files
1927 @section Working with input files
1928
1929 @menu
1930 * Including LilyPond files::
1931 * Different editions from one source::
1932 * Special characters::
1933 @end menu
1934
1935
1936 @node Including LilyPond files
1937 @subsection Including LilyPond files
1938
1939 @funindex \include
1940 @cindex including files
1941
1942 A large project may be split up into separate files.  To refer to
1943 another file, use
1944
1945 @example
1946 \include "otherfile.ly"
1947 @end example
1948
1949 The line @code{\include "otherfile.ly"} is equivalent to pasting the
1950 contents of @file{otherfile.ly} into the current file at the place
1951 where the @code{\include} appears.  For example, in a large
1952 project you might write separate files for each instrument part
1953 and create a @qq{full score} file which brings together the
1954 individual instrument files.  Normally the included file will
1955 define a number of variables which then become available
1956 for use in the full score file.  Tagged sections can be
1957 marked in included files to assist in making them usable in
1958 different places in a score, see @ref{Different editions from
1959 one source}.
1960
1961 Files in the current working directory may be referenced by
1962 specifying just the file name after the @code{\include} command.
1963 Files in other locations may be included by giving either a full
1964 path reference or a relative path reference (but use the UNIX
1965 forward slash, /, rather than the DOS/Windows back slash, \, as the
1966 directory separator.)  For example, if @file{stuff.ly} is located
1967 one directory higher than the current working directory, use
1968
1969 @example
1970 \include "../stuff.ly"
1971 @end example
1972
1973 @noindent
1974 or if the included orchestral parts files are all located in a
1975 subdirectory called @file{parts} within the current directory, use
1976
1977 @example
1978 \include "parts/VI.ly"
1979 \include "parts/VII.ly"
1980 @dots{} etc
1981 @end example
1982
1983 Files which are to be included can also contain @code{\include}
1984 statements of their own.  By default, these second-level
1985 @code{\include} statements are not interpreted until they have
1986 been brought into the main file, so the file names they specify
1987 must all be relative to the directory containing the main file,
1988 not the directory containing the included file.  However,
1989 this behavior can be changed globally by passing the option
1990 @option{-drelative-includes} option at the command line
1991 (or by adding @code{#(ly:set-option 'relative-includes #t)}
1992 at the top of the main input file).
1993
1994 When @code{relative-includes} is set to @code{#t}, the path for each
1995 @code{\include} command will be taken relative to the file containing
1996 that command.  This behavior is recommended and it will become the
1997 default behavior in a future version of lilypond.
1998
1999 Files relative to the main directory and files relative to some other
2000 directory may both be @code{\include}d by setting
2001 @code{relative-includes} to @code{#t} or @code{#f} at appropriate
2002 places in the files.  For example, if a general library, libA, has
2003 been created which itself uses sub-files which are @code{\include}d
2004 by the entry file of that library, those @code{\include} statements
2005 will need to be preceded by
2006 @code{#(ly:set-option #relative-includes #t)} so they are interpreted
2007 correctly when brought into the main @code{.ly} file, like this:
2008
2009 @example
2010 libA/
2011   libA.ly
2012   A1.ly
2013   A2.ly
2014   @dots{}
2015 @end example
2016
2017 @noindent
2018 then the entry file, @code{libA.ly}, will contain
2019
2020 @example
2021 #(ly:set-option 'relative-includes #t)
2022 \include "A1.ly"
2023 \include "A2.ly"
2024 @dots{}
2025 % return to default setting
2026 #(ly:set-option 'relative-includes #f)
2027 @end example
2028
2029 Any @file{.ly} file can then include the entire library simply with
2030
2031 @example
2032 \include "~/libA/libA.ly"
2033 @end example
2034
2035 More complex file structures may be devised by switching at
2036 appropriate places.
2037
2038 Files can also be included from a directory in a search path
2039 specified as an option when invoking LilyPond from the command
2040 line.  The included files are then specified using just their
2041 file name.  For example, to compile @file{main.ly} which includes
2042 files located in a subdirectory called @file{parts} by this method,
2043 cd to the directory containing @file{main.ly} and enter
2044
2045 @example
2046 lilypond --include=parts main.ly
2047 @end example
2048
2049 and in main.ly write
2050
2051 @example
2052 \include "VI.ly"
2053 \include "VII.ly"
2054 @dots{} etc
2055 @end example
2056
2057 Files which are to be included in many scores may be placed in
2058 the LilyPond directory @file{../ly}.  (The location of this
2059 directory is installation-dependent - see
2060 @rlearning{Other sources of information}).  These files can then
2061 be included simply by naming them on an @code{\include} statement.
2062 This is how the language-dependent files like @file{english.ly} are
2063 included.
2064
2065 LilyPond includes a number of files by default when you start
2066 the program.  These includes are not apparent to the user, but the
2067 files may be identified by running @code{lilypond --verbose} from
2068 the command line.  This will display a list of paths and files that
2069 LilyPond uses, along with much other information.  Alternatively,
2070 the more important of these files are discussed in
2071 @rlearning{Other sources of information}.  These files may be
2072 edited, but changes to them will be lost on installing a new
2073 version of LilyPond.
2074
2075 Some simple examples of using @code{\include} are shown in
2076 @rlearning{Scores and parts}.
2077
2078 @seealso
2079 Learning Manual:
2080 @rlearning{Other sources of information},
2081 @rlearning{Scores and parts}.
2082
2083 @knownissues
2084 If an included file is given a name which is the same as one in
2085 LilyPond's installation files, LilyPond's file from the
2086 installation files takes precedence.
2087
2088
2089 @node Different editions from one source
2090 @subsection Different editions from one source
2091
2092 Several methods can be used to generate different versions of a score
2093 from the same music source.  Variables are perhaps the most useful for
2094 combining lengthy sections of music and/or annotation.  Tags are more
2095 useful for selecting one section from several alternative shorter
2096 sections of music, and can also be used for splicing pieces of music
2097 together at different points.
2098
2099 Whichever method is used, separating the notation from the structure of
2100 the score will make it easier to change the structure while leaving the
2101 notation untouched.
2102
2103 @menu
2104 * Using variables::
2105 * Using tags::
2106 * Using global settings::
2107 @end menu
2108
2109 @node Using variables
2110 @unnumberedsubsubsec Using variables
2111
2112 @cindex variables, use of
2113
2114 If sections of the music are defined in variables they can be
2115 reused in different parts of the score, see @rlearning{Organizing
2116 pieces with variables}.  For example, an @notation{a cappella}
2117 vocal score frequently includes a piano reduction of the parts
2118 for rehearsal purposes which is identical to the vocal music, so
2119 the music need be entered only once.  Music from two variables
2120 may be combined on one staff, see @ref{Automatic part combining}.
2121 Here is an example:
2122
2123 @lilypond[verbatim,quote]
2124 sopranoMusic = \relative { a'4 b c b8( a) }
2125 altoMusic = \relative { e'4 e e f }
2126 tenorMusic = \relative { c'4 b e d8( c) }
2127 bassMusic = \relative { a4 gis a d, }
2128 allLyrics = \lyricmode {King of glo -- ry }
2129 <<
2130   \new Staff = "Soprano" \sopranoMusic
2131   \new Lyrics \allLyrics
2132   \new Staff = "Alto" \altoMusic
2133   \new Lyrics \allLyrics
2134   \new Staff = "Tenor" {
2135     \clef "treble_8"
2136     \tenorMusic
2137   }
2138   \new Lyrics \allLyrics
2139   \new Staff = "Bass" {
2140     \clef "bass"
2141     \bassMusic
2142   }
2143   \new Lyrics \allLyrics
2144   \new PianoStaff <<
2145     \new Staff = "RH" {
2146       \set Staff.printPartCombineTexts = ##f
2147       \partcombine
2148       \sopranoMusic
2149       \altoMusic
2150     }
2151     \new Staff = "LH" {
2152       \set Staff.printPartCombineTexts = ##f
2153       \clef "bass"
2154       \partcombine
2155       \tenorMusic
2156       \bassMusic
2157     }
2158   >>
2159 >>
2160 @end lilypond
2161
2162 Separate scores showing just the vocal parts or just the piano
2163 part can be produced by changing just the structural statements,
2164 leaving the musical notation unchanged.
2165
2166 For lengthy scores, the variable definitions may be placed in
2167 separate files which are then included, see @ref{Including
2168 LilyPond files}.
2169
2170 @node Using tags
2171 @unnumberedsubsubsec Using tags
2172
2173 @funindex \tag
2174 @funindex \keepWithTag
2175 @funindex \removeWithTag
2176 @cindex tag
2177 @cindex keep tagged music
2178 @cindex remove tagged music
2179
2180 The @code{\tag #'@var{partA}} command marks a music expression
2181 with the name @var{partA}.
2182 Expressions tagged in this way can be selected or filtered out by
2183 name later, using either @code{\keepWithTag #'@var{name}} or
2184 @code{\removeWithTag #'@var{name}}.  The result of applying these filters
2185 to tagged music is as follows:
2186 @multitable @columnfractions .5 .5
2187 @headitem Filter
2188   @tab Result
2189 @item
2190 Tagged music preceded by @code{\keepWithTag #'@var{name}} or
2191        @code{\keepWithTag #'(@var{name1} @var{name2}@dots{})}
2192 @tab Untagged music and music tagged with any of the given tag
2193      names is included;
2194      music tagged with any other tag name is excluded.
2195 @item
2196 Tagged music preceded by @code{\removeWithTag #'@var{name}} or
2197        @code{\removeWithTag #'(@var{name1} @var{name2}@dots{})}
2198 @tab Untagged music and music not tagged with any of the given tag names
2199      is included; music tagged with any of the given tag names is
2200      excluded.
2201 @item
2202 Tagged music not preceded by either @code{\keepWithTag} or
2203 @code{\removeWithTag}
2204 @tab All tagged and untagged music is included.
2205 @end multitable
2206
2207 The arguments of the @code{\tag}, @code{\keepWithTag} and
2208 @code{\removeWithTag} commands should be a symbol or list of
2209 symbols (such as @code{#'score} or @code{#'(violinI violinII}),
2210 followed by a music expression.  If @emph{and only if} the symbols
2211 are valid LilyPond identifiers (alphabetic characters only, no
2212 numbers, underscores, or dashes) which cannot be confused with notes,
2213 the @code{#'} may be omitted and, as a shorthand, a list of symbols
2214 can use the dot separator: i.e. @code{\tag #'(violinI violinII)} can
2215 be written @code{\tag violinI.violinII}.  The same applies to
2216 @code{\keepWithTag} and @code{\removeWithTag}.
2217
2218 In the following example, we see two versions of a piece of music,
2219 one showing trills with the usual notation, and one with trills
2220 explicitly expanded:
2221
2222 @lilypond[verbatim,quote]
2223 music = \relative {
2224   g'8. c32 d
2225   \tag #'trills { d8.\trill }
2226   \tag #'expand { \repeat unfold 3 { e32 d } }
2227   c32 d
2228  }
2229
2230 \score {
2231   \keepWithTag #'trills \music
2232 }
2233 \score {
2234   \keepWithTag #'expand \music
2235 }
2236 @end lilypond
2237
2238 @noindent
2239 Alternatively, it is sometimes easier to exclude sections of music:
2240
2241 @lilypond[verbatim,quote]
2242 music = \relative {
2243   g'8. c32 d
2244   \tag #'trills { d8.\trill }
2245   \tag #'expand {\repeat unfold 3 { e32 d } }
2246   c32 d
2247  }
2248
2249 \score {
2250   \removeWithTag #'expand
2251   \music
2252 }
2253 \score {
2254   \removeWithTag #'trills
2255   \music
2256 }
2257 @end lilypond
2258
2259 Tagged filtering can be applied to articulations, texts, etc. by
2260 prepending
2261
2262 @example
2263 -\tag #'@var{your-tag}
2264 @end example
2265
2266 to an articulation.  For example, this would define a note with a
2267 conditional fingering indication and a note with a conditional
2268 annotation:
2269
2270 @example
2271 c1-\tag #'finger ^4
2272 c1-\tag #'warn ^"Watch!"
2273 @end example
2274
2275 Multiple tags may be placed on expressions with multiple
2276 @code{\tag} entries, or by combining multiple tags into one symbol
2277 list:
2278
2279 @lilypond[quote,verbatim]
2280 music = \relative c'' {
2281   \tag #'a \tag #'both { a4 a a a }
2282   \tag #'(b both) { b4 b b b }
2283 }
2284 <<
2285 \keepWithTag #'a \music
2286 \keepWithTag #'b \music
2287 \keepWithTag #'both \music
2288 >>
2289 @end lilypond
2290
2291 Multiple @code{\removeWithTag} filters may be applied to a single
2292 music expression to remove several differently named tagged
2293 sections.  Alternatively, you can use a single
2294 @code{\removeWithTag} with a list of tags.
2295
2296 @lilypond[verbatim,quote]
2297 music = \relative c'' {
2298   \tag #'A { a4 a a a }
2299   \tag #'B { b4 b b b }
2300   \tag #'C { c4 c c c }
2301   \tag #'D { d4 d d d }
2302 }
2303 \new Voice {
2304   \removeWithTag #'B
2305   \removeWithTag #'C
2306   \music
2307   \removeWithTag #'(B C)
2308   \music
2309 }
2310 @end lilypond
2311
2312 Two or more @code{\keepWithTag} filters applied to a single music
2313 expression will cause @emph{all} tagged sections to be removed, as
2314 the first filter will remove all tagged sections except the one
2315 named, and the second filter will remove even that tagged section.
2316 Usually you would rather want to use a single @code{\keepWithTag}
2317 command with a list of multiple tags: this will only remove tagged
2318 sections not given in @emph{any} of the tags.
2319
2320 @cindex tag groups
2321 @funindex \tagGroup
2322 While @code{\keepWithTag} is convenient when dealing with
2323 @emph{one} set of alternatives, the removal of music tagged with
2324 @emph{unrelated} tags is problematic when using tags for more than
2325 one purpose.  For that reason, @q{tag groups} of related tags can
2326 be declared:
2327
2328 @example
2329 \tagGroup #'(violinI violinII viola cello)
2330 @end example
2331
2332 declares the respective tags as belonging to one tag group.
2333
2334 @example
2335 \keepWithTag #'violinI @dots{}
2336 @end example
2337
2338 will then only be concerned with tags from @code{violinI}'s tag
2339 group: any element of the included music that is tagged with one
2340 or more of tags from this set but @emph{not} with @code{violinI}
2341 will get removed.
2342
2343 To any @code{\keepWithTag} command, only tags from the tag groups
2344 of the tags given in the command are visible.
2345
2346 Tags cannot be members of more than one tag group.
2347
2348 @funindex \pushToTag
2349 @funindex \appendToTag
2350 @cindex splice into tagged music
2351
2352 Sometimes you want to splice some music at a particular place in an
2353 existing music expression.  You can use @code{\pushToTag} and
2354 @code{\appendToTag} for adding material at the front or end of the
2355 @code{elements} of an existing music construct.  Not every music
2356 construct has @code{elements}, but sequential and simultaneous music are
2357 safe bets:
2358
2359 @lilypond[verbatim,quote]
2360 test = { \tag #'here { \tag #'here <<c''>> } }
2361
2362 {
2363   \pushToTag #'here c'
2364   \pushToTag #'here e'
2365   \pushToTag #'here g' \test
2366   \appendToTag #'here c'
2367   \appendToTag #'here e'
2368   \appendToTag #'here g' \test
2369 }
2370 @end lilypond
2371
2372 Both commands get a tag, the material to splice in at every occurence of
2373 the tag, and the tagged expression.
2374
2375 @seealso
2376 Learning Manual:
2377 @rlearning{Organizing pieces with variables}.
2378
2379 Notation Reference:
2380 @ref{Automatic part combining},
2381 @ref{Including LilyPond files}.
2382
2383 @knownissues
2384 Calling @code{\relative} on a music expression obtained by filtering
2385 music through @code{\keepWithTag} or @code{\removeWithTag} might cause
2386 the octave relations to change, as only the pitches actually
2387 remaining in the filtered expression will be considered.  Applying
2388 @code{\relative} first, before @code{\keepWithTag} or
2389 @code{\removeWithTag}, avoids this danger as @code{\relative} then
2390 acts on all the pitches as-input.
2391
2392
2393 @node Using global settings
2394 @unnumberedsubsubsec Using global settings
2395
2396 @cindex include-settings
2397
2398 Global settings can be included from a separate file:
2399
2400 @example
2401 lilypond -dinclude-settings=MY_SETTINGS.ly MY_SCORE.ly
2402 @end example
2403
2404 Groups of settings such as page size, font or type face can be stored
2405 in separate files. This allows different editions from the same score
2406 as well as standard settings to be applied to many scores, simply by
2407 specifying the proper settings file.
2408
2409 This technique also works well with the use of style sheets, as
2410 discussed in @rlearning{Style sheets}.
2411
2412 @seealso
2413 Learning Manual:
2414 @rlearning{Organizing pieces with variables},
2415 @rlearning{Style sheets}.
2416
2417 Notation Reference:
2418 @ref{Including LilyPond files}.
2419
2420
2421 @node Special characters
2422 @subsection Special characters
2423
2424 @cindex special characters
2425 @cindex non-ASCII characters
2426
2427 @menu
2428 * Text encoding::
2429 * Unicode::
2430 * ASCII aliases::
2431 @end menu
2432
2433
2434 @node Text encoding
2435 @unnumberedsubsubsec Text encoding
2436
2437 @cindex UTF-8
2438
2439 LilyPond uses the character repertoire defined by the Unicode
2440 consortium and ISO/IEC 10646.  This defines a unique name and
2441 code point for the character sets used in virtually all modern
2442 languages and many others too.  Unicode can be implemented using
2443 several different encodings.  LilyPond uses the UTF-8 encoding
2444 (UTF stands for Unicode Transformation Format) which represents
2445 all common Latin characters in one byte, and represents other
2446 characters using a variable length format of up to four bytes.
2447
2448 The actual appearance of the characters is determined by the
2449 glyphs defined in the particular fonts available - a font defines
2450 the mapping of a subset of the Unicode code points to glyphs.
2451 LilyPond uses the Pango library to layout and render multi-lingual
2452 texts.
2453
2454 LilyPond does not perform any input-encoding conversions.  This
2455 means that any text, be it title, lyric text, or musical
2456 instruction containing non-ASCII characters, must be encoded in
2457 UTF-8.  The easiest way to enter such text is by using a
2458 Unicode-aware editor and saving the file with UTF-8 encoding.  Most
2459 popular modern editors have UTF-8 support, for example, vim, Emacs,
2460 jEdit, and GEdit do.  All MS Windows systems later than NT use
2461 Unicode as their native character encoding, so even Notepad can
2462 edit and save a file in UTF-8 format.  A more functional
2463 alternative for Windows is BabelPad.
2464
2465 If a LilyPond input file containing a non-ASCII character is not
2466 saved in UTF-8 format the error message
2467
2468 @example
2469 FT_Get_Glyph_Name () error: invalid argument
2470 @end example
2471
2472 will be generated.
2473
2474 Here is an example showing Cyrillic, Hebrew and Portuguese
2475 text:
2476
2477 @lilypond[quote]
2478 %c No verbatim here as the code does not display correctly in PDF
2479 % Font settings for Cyrillic and Hebrew
2480 % Linux Libertine fonts contain Cyrillic and Hebrew glyphs.
2481 \paper {
2482   #(define fonts
2483     (set-global-fonts
2484      #:roman "Linux Libertine O,serif"
2485      #:sans "Linux Biolinum O,sans-serif"
2486      #:typewriter "Linux Libertine Mono O,monospace"
2487    ))
2488 }
2489
2490 % Cyrillic
2491 bulgarian = \lyricmode {
2492   Жълтата дюля беше щастлива, че пухът, който цъфна, замръзна като гьон.
2493 }
2494
2495 % Hebrew
2496 hebrew = \lyricmode {
2497   זה כיף סתם לשמוע איך תנצח קרפד עץ טוב בגן.
2498 }
2499
2500 % Portuguese
2501 portuguese = \lyricmode {
2502   à vo -- cê uma can -- ção legal
2503 }
2504
2505 \relative {
2506   c'2 d e f g f e
2507 }
2508 \addlyrics { \bulgarian }
2509 \addlyrics { \hebrew }
2510 \addlyrics { \portuguese }
2511 @end lilypond
2512
2513
2514 @node Unicode
2515 @unnumberedsubsubsec Unicode
2516
2517 @cindex Unicode
2518
2519 To enter a single character for which the Unicode code point is
2520 known but which is not available in the editor being used, use
2521 either @code{\char ##xhhhh} or @code{\char #dddd} within a
2522 @code{\markup} block, where @code{hhhh} is the hexadecimal code for
2523 the character required and @code{dddd} is the corresponding decimal
2524 value.  Leading zeroes may be omitted, but it is usual to specify
2525 all four characters in the hexadecimal representation.  (Note that
2526 the UTF-8 encoding of the code point should @emph{not} be used
2527 after @code{\char}, as UTF-8 encodings contain extra bits indicating
2528 the number of octets.)  Unicode code charts and a character name
2529 index giving the code point in hexadecimal for any character can be
2530 found on the Unicode Consortium website,
2531 @uref{http://www.unicode.org/}.
2532
2533 For example, @code{\char ##x03BE} and @code{\char #958} would both
2534 enter the Unicode U+03BE character, which has the Unicode name
2535 @qq{Greek Small Letter Xi}.
2536
2537 Any Unicode code point may be entered in this way and if all special
2538 characters are entered in this format it is not necessary to save
2539 the input file in UTF-8 format.  Of course, a font containing all
2540 such encoded characters must be installed and available to LilyPond.
2541
2542 The following example shows Unicode hexadecimal values being entered
2543 in four places -- in a rehearsal mark, as articulation text, in
2544 lyrics and as stand-alone text below the score:
2545
2546 @lilypond[quote,verbatim]
2547 \score {
2548   \relative {
2549     c''1 \mark \markup { \char ##x03EE }
2550     c1_\markup { \tiny { \char ##x03B1 " to " \char ##x03C9 } }
2551   }
2552   \addlyrics { O \markup { \concat { Ph \char ##x0153 be! } } }
2553 }
2554 \markup { "Copyright 2008--2015" \char ##x00A9 }
2555 @end lilypond
2556
2557 @cindex copyright sign
2558
2559 To enter the copyright sign in the copyright notice use:
2560
2561 @example
2562 \header @{
2563   copyright = \markup @{ \char ##x00A9 "2008" @}
2564 @}
2565 @end example
2566
2567
2568 @node ASCII aliases
2569 @unnumberedsubsubsec ASCII aliases
2570
2571 A list of ASCII aliases for special characters can be included:
2572
2573 @lilypond[quote,verbatim]
2574 \paper {
2575   #(include-special-characters)
2576 }
2577
2578 \markup "&flqq; &ndash; &OE;uvre incomplète&hellip; &frqq;"
2579
2580 \score {
2581   \new Staff { \repeat unfold 9 a'4 }
2582   \addlyrics {
2583     This is al -- so wor -- kin'~in ly -- rics: &ndash;_&OE;&hellip;
2584   }
2585 }
2586
2587 \markup \column {
2588   "The replacement can be disabled:"
2589   "&ndash; &OE; &hellip;"
2590   \override #'(replacement-alist . ()) "&ndash; &OE; &hellip;"
2591 }
2592 @end lilypond
2593
2594 You can also make your own aliases, either globally:
2595
2596 @lilypond[quote,verbatim]
2597 \paper {
2598   #(add-text-replacements!
2599     '(("100" . "hundred")
2600       ("dpi" . "dots per inch")))
2601 }
2602 \markup "A 100 dpi."
2603 @end lilypond
2604
2605 or locally:
2606
2607 @lilypond[quote,verbatim]
2608 \markup \replace #'(("100" . "hundred")
2609                     ("dpi" . "dots per inch")) "A 100 dpi."
2610 @end lilypond
2611
2612 @seealso
2613 Notation Reference:
2614 @ref{List of special characters}.
2615
2616 Installed Files:
2617 @file{ly/text-replacements.ly}.
2618
2619
2620 @node Controlling output
2621 @section Controlling output
2622
2623 @menu
2624 * Extracting fragments of music::
2625 * Skipping corrected music::
2626 * Alternative output formats::
2627 * Replacing the notation font::
2628 @end menu
2629
2630 @funindex clip-regions
2631 @cindex Fragments, music
2632 @cindex Music fragments
2633
2634 @node Extracting fragments of music
2635 @subsection Extracting fragments of music
2636
2637 It is possible to output one or more fragments of a score by defining
2638 the explicit location of the music to be extracted within the
2639 @code{\layout} block of the the input file using the @code{clip-regions}
2640 function, and then running LilyPond with the @option{-dclip-systems}
2641 option;
2642
2643 @example
2644 \layout @{
2645   clip-regions
2646   = #(list
2647       (cons
2648        (make-rhythmic-location 5 1 2)
2649        (make-rhythmic-location 7 3 4)))
2650 @}
2651 @end example
2652
2653 @noindent
2654 This example will extract a single fragment of the input file
2655 @emph{starting} after a half-note duration in fifth measure
2656 (@code{5 1 2}) and @emph{ending} after the third quarter-note in the
2657 seventh measure (@code{7 3 4}).
2658
2659 Additional fragments can be extracted by adding more pairs of
2660 @code{make-rhythmic-location} entries to the @code{clip-regions} list in
2661 the @code{\layout} block.
2662
2663 By default, each music fragment will be output as a separate @code{EPS}
2664 file, but other formats such as @code{PDF} or @code{PNG} can also be
2665 created if required.  The extracted music is output as if had been
2666 literally @q{cut} from the original printed score so if a fragment runs
2667 over one or more lines, a separate ouput file for each line will be
2668 generated.
2669
2670 @seealso
2671 Notation Reference:
2672 @ref{The layout block}.
2673
2674 Application Usage
2675 @rprogram{Command-line usage}.
2676
2677
2678
2679 @node Skipping corrected music
2680 @subsection Skipping corrected music
2681
2682
2683 @funindex skipTypesetting
2684 @funindex showFirstLength
2685 @funindex showLastLength
2686
2687 When entering or copying music, usually only the music near the end (where
2688 you
2689 are adding notes) is interesting to view and correct.  To speed up
2690 this correction process, it is possible to skip typesetting of all but
2691 the last few measures.  This is achieved by putting
2692
2693 @example
2694 showLastLength = R1*5
2695 \score @{ @dots{} @}
2696 @end example
2697
2698 @noindent
2699 in your source file.  This will render only the last 5 measures
2700 (assuming 4/4 time signature) of every @code{\score} in the input
2701 file.  For longer pieces, rendering only a small part is often an order
2702 of magnitude quicker than rendering it completely.  When working on the
2703 beginning of a score you have already typeset (e.g. to add a new part),
2704 the @code{showFirstLength} property may be useful as well.
2705
2706 Skipping parts of a score can be controlled in a more fine-grained
2707 fashion with the property @code{Score.skipTypesetting}.  When it is
2708 set, no typesetting is performed at all.
2709
2710 This property is also used to control output to the MIDI file.  Note that
2711 it skips all events, including tempo and instrument changes.  You have
2712 been warned.
2713
2714 @lilypond[quote,ragged-right,verbatim]
2715 \relative c' {
2716   c1
2717   \set Score.skipTypesetting = ##t
2718   \tempo 4 = 80
2719   c4 c c c
2720   \set Score.skipTypesetting = ##f
2721   d4 d d d
2722 }
2723 @end lilypond
2724
2725 In polyphonic music, @code{Score.skipTypesetting} will affect all
2726 voices and staves, saving even more time.
2727
2728 @node Alternative output formats
2729 @subsection Alternative output formats
2730
2731 @cindex scalable vector graphics output
2732 @cindex SVG output
2733 @cindex encapsulated postscript output
2734 @cindex EPS output
2735
2736 The default output formats for the printed score are Portable
2737 Document Format (PDF) and PostScript (PS).  Scalable Vector
2738 Graphics (SVG), Encapsulated PostScript (EPS) and Portable
2739 Network Graphics (PNG) output formats are also available through
2740 command line options, see
2741 @rprogram{Basic command line options for LilyPond}.
2742
2743
2744 @node Replacing the notation font
2745 @subsection Replacing the notation font
2746
2747 Gonville is an alternative to the Feta font used in LilyPond and can
2748 be downloaded from:
2749 @example
2750 @uref{http://www.chiark.greenend.org.uk/~sgtatham/gonville/ ,http://www.chiark.greenend.org.uk/~sgtatham/gonville/}
2751 @end example
2752
2753 Here are a few sample bars of music set in Gonville:
2754
2755 @c NOTE: these images are a bit big, but that's important
2756 @c       for the font comparison.  -gp
2757 @sourceimage{Gonville_after,15cm,,}
2758
2759 Here are a few sample bars of music set in LilyPond's Feta font:
2760
2761 @sourceimage{Gonville_before,15cm,,}
2762
2763 @subsubheading Installation Instructions for MacOS
2764
2765 Download and extract the zip file.  Copy the @code{lilyfonts}
2766 directory to @file{@var{SHARE_DIR}/lilypond/current}; for more
2767 information, see @rlearning{Other sources of information}.  Rename the
2768 existing @code{fonts} directory to @code{fonts_orig} and the
2769 @code{lilyfonts} directory to @code{fonts}.  To revert back to Feta,
2770 reverse the process.
2771
2772 @seealso
2773 Learning Manual:
2774 @rlearning{Other sources of information}.
2775
2776 @knownissues
2777 Gonville cannot be used to typeset @q{Ancient Music} notation and it is
2778 likely newer glyphs in later releases of LilyPond may not exist in the
2779 Gonville font family.  Please refer to the author's website for more
2780 information on these and other specifics, including licensing of
2781 Gonville.
2782
2783
2784 @node Creating MIDI output
2785 @section Creating MIDI output
2786
2787 @cindex Sound
2788 @cindex MIDI
2789
2790 LilyPond can produce files that conform to the MIDI (Musical Instrument
2791 Digital Interface) standard and so allow for the checking of the music
2792 output aurally (with the help of an application or device that
2793 understands MIDI).  Listening to MIDI output may also help in spotting
2794 errors such as notes that have been entered incorrectly or are missing
2795 accidentals and so on.
2796
2797 MIDI files do not contain sound (like AAC, MP3 or Vorbis files) but
2798 require additional software to produce sound from them.
2799
2800 @menu
2801 * Supported notation for MIDI::
2802 * Unsupported notation for MIDI::
2803 * The MIDI block::
2804 * Controlling MIDI dynamics::
2805 * Using MIDI instruments::
2806 * Using repeats with MIDI::
2807 * MIDI channel mapping::
2808 * Context properties for MIDI effects::
2809 * Enhancing MIDI output::
2810 @end menu
2811
2812 @cindex MIDI, Supported notation
2813
2814 @node Supported notation for MIDI
2815 @subsection Supported notation for MIDI
2816
2817 The following musical notation can be used with LilyPond's default
2818 capabilities to produce MIDI output;
2819
2820 @itemize
2821 @item Breath marks
2822 @item Chords entered as chord names
2823 @item Crescendi, decrescendi over multiple notes.  The volume is altered
2824 linearly between the two extremes
2825 @item Dynamic markings from @code{ppppp} to @code{fffff}, including
2826 @code{mp}, @code{mf} and @code{sf}
2827 @item Microtones but @emph{not} microtonal chords.  A MIDI player that
2828 supports pitch bending will also be required.
2829 @item Lyrics
2830 @item Pitches
2831 @item Rhythms entered as note durations, including tuplets
2832 @item @q{Simple} articulations; staccato, staccatissimo, accent, marcato
2833 and portato
2834 @item Tempo changes using the @code{\tempo} function
2835 @item Ties
2836 @item Tremolos that are @emph{not} entered with a
2837 @q{@code{:}[@var{number}]} value
2838 @end itemize
2839
2840 Panning, balance, expression, reverb and chorus effects can also be
2841 controlled by setting context properties,
2842 see @ref{Context properties for MIDI effects}.
2843
2844 When combined with the @file{articulate} script the following,
2845 additional musical notation can be output to MIDI;
2846
2847 @itemize
2848 @item Appogiaturas.  These are made to take half the value of the note
2849 following (without taking dots into account).  For example;
2850
2851 @example
2852 \appoggiatura c8 d2.
2853 @end example
2854
2855 @noindent
2856 The c will take the value of a crotchet.
2857
2858 @item Ornaments (i.e. mordents, trills and turns et al.)
2859 @item Rallentando, accelerando, ritardando and a tempo
2860 @item Slurs, including phrasing slurs
2861 @item Tenuto
2862 @end itemize
2863
2864 @noindent
2865 See @ref{Enhancing MIDI output}.
2866
2867 @cindex MIDI, Unsupported notation
2868
2869 @node Unsupported notation for MIDI
2870 @subsection Unsupported notation for MIDI
2871
2872 The following items of musical notation cannot be output to MIDI;
2873
2874 @itemize
2875 @item Articulations other than staccato, staccatissimo, accent, marcato
2876 and portato
2877 @item Crescendi and decrescendi over a @emph{single} note
2878 @item Fermata
2879 @item Figured bass
2880 @item Glissandi
2881 @item Falls and doits
2882 @item Microtonal chords
2883 @item Rhythms entered as annotations, e.g. swing
2884 @item Tempo changes without @code{\tempo} (e.g. entered as annotations)
2885 @item Tremolos that @emph{are} entered with a @q{@code{:}[@var{number}]}
2886 value
2887 @end itemize
2888
2889
2890 @node The MIDI block
2891 @subsection The MIDI block
2892
2893 @cindex MIDI block
2894
2895 To create a MIDI output file from a LilyPond input file, insert a
2896 @code{\midi} block, which can be empty, within the @code{\score} block;
2897
2898 @example
2899 \score @{
2900   @var{@dots{} music @dots{}}
2901   \layout @{ @}
2902   \midi @{ @}
2903 @}
2904 @end example
2905
2906 @warning{ A @code{@bs{}score} block that, as well as the music, contains
2907 only a @code{@bs{}midi} block (i.e. @emph{without} the @code{@bs{}layout}
2908 block), will only produce MIDI output files.  No notation will be
2909 printed.}
2910
2911 The default output file extension (@code{.midi}) can be changed by using
2912 the @code{-dmidi-extension} option with the @code{lilypond} command:
2913
2914 @example
2915 lilypond -dmidi-extension=mid MyFile.ly
2916 @end example
2917
2918 Alternatively, add the following Scheme expression before the start of
2919 either the @code{\book}, @code{\bookpart} or @code{\score} blocks.  See
2920 @ref{File structure}.
2921
2922 @example
2923 #(ly:set-option 'midi-extension "mid")
2924 @end example
2925
2926 @seealso
2927 Notation Reference:
2928 @ref{File structure}.
2929
2930 Installed Files:
2931 @file{scm/midi.scm}.
2932
2933 @knownissues
2934 There are fifteen MIDI channels available and one additional channel
2935 (#10) for drums.  Staves are assigned to channels in sequence, so a
2936 score that contains more than fifteen staves will result in the extra
2937 staves sharing (but not overwriting) the same MIDI channel.  This may be
2938 a problem if the sharing staves have conflicting, channel-based, MIDI
2939 properties -- such as different MIDI instruments -- set.
2940
2941
2942 @node Controlling MIDI dynamics
2943 @subsection Controlling MIDI dynamics
2944
2945 It is possible to control the overall MIDI volume, the relative volume
2946 of dynamic markings and the relative volume of different instruments.
2947
2948 Dynamic marks translate automatically into volume levels in the
2949 available MIDI volume range whereas crescendi and decrescendi vary the
2950 volume linearly between their two extremes.  It is possible to control
2951 the relative volume of dynamic markings, and the overall volume levels
2952 of different instruments.
2953
2954 @menu
2955 * Dynamic marks in MIDI::
2956 * Setting MIDI volume::
2957 * Setting MIDI block properties::
2958 @end menu
2959
2960 @cindex MIDI volume
2961 @cindex MIDI equalization
2962 @cindex MIDI dynamics
2963 @cindex Dynamics in MIDI
2964
2965
2966 @node Dynamic marks in MIDI
2967 @unnumberedsubsubsec Dynamic marks in MIDI
2968
2969 Only the dynamic markings from @code{ppppp} to @code{fffff}, including
2970 @code{mp}, @code{mf} and @code{sf} have values assigned to them.  This
2971 value is then applied to the value of the overall MIDI volume range to
2972 obtain the final volume included in the MIDI output for that particular
2973 dynamic marking.  The default fractions range from 0.25 for
2974 @notation{ppppp} to 0.95 for @notation{fffff}. The complete set of
2975 dynamic marks and their associated fractions can be found in
2976 @file{scm/midi.scm}.
2977
2978
2979 @snippets
2980 @lilypondfile[verbatim,quote,ragged-right,texidoc,doctitle]
2981 {creating-custom-dynamics-in-midi-output.ly}
2982
2983 Installed Files:
2984 @file{ly/script-init.ly}
2985 @file{scm/midi.scm}.
2986
2987 Snippets:
2988 @rlsr{MIDI}.
2989
2990 Internals Reference:
2991 @rinternals{Dynamic_performer}.
2992
2993
2994 @node Setting MIDI volume
2995 @unnumberedsubsubsec Setting MIDI volume
2996
2997 The minimum and maximum overall volume of MIDI dynamic markings is
2998 controlled by setting the properties @code{midiMinimumVolume} and
2999 @code{midiMaximumVolume} at the @code{Score} level.  These properties
3000 have an effect only at the start of a voice and on dynamic marks.  The
3001 fraction corresponding to each dynamic mark is modified with this
3002 formula
3003
3004 @example
3005 midiMinimumVolume + (midiMaximumVolume - midiMinimumVolume) * fraction
3006 @end example
3007
3008 In the following example the dynamic range of the overall MIDI
3009 volume is limited to the range @code{0.2} - @code{0.5}.
3010
3011 @example
3012 \score @{
3013   <<
3014     \new Staff @{
3015       \set Staff.midiInstrument = #"flute"
3016       @var{@dots{} music @dots{}}
3017     @}
3018     \new Staff @{
3019       \set Staff.midiInstrument = #"clarinet"
3020       @var{@dots{} music @dots{}}
3021     @}
3022   >>
3023   \midi @{
3024     \context @{
3025       \Score
3026       midiMinimumVolume = #0.2
3027       midiMaximumVolume = #0.5
3028     @}
3029   @}
3030 @}
3031 @end example
3032
3033 Simple MIDI instrument equalization can be achieved by setting
3034 @code{midiMinimumVolume} and @code{midiMaximumVolume} properties within
3035 the @code{Staff} context.
3036
3037 @example
3038 \score @{
3039   \new Staff @{
3040     \set Staff.midiInstrument = #"flute"
3041     \set Staff.midiMinimumVolume = #0.7
3042     \set Staff.midiMaximumVolume = #0.9
3043     @var{@dots{} music @dots{}}
3044   @}
3045   \midi @{ @}
3046 @}
3047 @end example
3048
3049 For scores with multiple staves and multiple MIDI instruments, the
3050 relative volumes of each instrument can be set individually;
3051
3052 @example
3053 \score @{
3054   <<
3055     \new Staff @{
3056       \set Staff.midiInstrument = #"flute"
3057       \set Staff.midiMinimumVolume = #0.7
3058       \set Staff.midiMaximumVolume = #0.9
3059       @var{@dots{} music @dots{}}
3060     @}
3061     \new Staff @{
3062       \set Staff.midiInstrument = #"clarinet"
3063       \set Staff.midiMinimumVolume = #0.3
3064       \set Staff.midiMaximumVolume = #0.6
3065       @var{@dots{} music @dots{}}
3066     @}
3067   >>
3068   \midi @{ @}
3069 @}
3070 @end example
3071
3072 In this example the volume of the clarinet is reduced relative to the
3073 volume of the flute.
3074
3075 If these volumes properties are not set then LilyPond still applies a
3076 @q{small degree} of equalization to certain instruments.  See
3077 @file{scm/midi.scm}.
3078
3079 Installed Files:
3080 @file{scm/midi.scm}.
3081
3082 @seealso
3083 Notation Reference:
3084 @ref{Score layout}.
3085
3086 Internals Reference:
3087 @rinternals{Dynamic_performer}.
3088
3089 @snippets
3090 @lilypondfile[verbatim,quote,ragged-right,texidoc,doctitle]
3091 {replacing-default-midi-instrument-equalization.ly}
3092
3093 @knownissues
3094 Changes in the MIDI volume take place only on starting a note, so
3095 crescendi and decrescendi cannot affect the volume of a single note.
3096
3097
3098 @node Setting MIDI block properties
3099 @unnumberedsubsubsec Setting MIDI block properties
3100
3101 The @code{\midi} block can contain context rearrangements, new context
3102 definitions or code that sets the values of certain properties.
3103
3104 @example
3105 \score @{
3106   @var{@dots{} music @dots{}}
3107   \midi @{
3108     \tempo 4 = 72
3109   @}
3110 @}
3111 @end example
3112
3113 Here the tempo is set to 72 quarter-note beats per minute.  The tempo
3114 mark in the @code{\midi} block will not appear in the printed score.
3115 Although any other @code{\tempo} indications specified within the
3116 @code{\score} block will also be reflected in the MIDI output.
3117
3118 In a @code{\midi} block the @code{\tempo} command is setting properties
3119 during the interpretation of the music and in the context of output
3120 definitions; so it is interpreted @emph{as if} it were a context
3121 modification.
3122
3123 @cindex MIDI context definitions
3124 @cindex context definitions with MIDI
3125
3126 Context definitions follow the same syntax as those in a @code{\layout}
3127 block;
3128
3129 @example
3130 \score @{
3131   @var{@dots{} music @dots{}}
3132   \midi @{
3133     \context @{
3134       \Voice
3135       \remove "Dynamic_performer"
3136     @}
3137   @}
3138 @}
3139 @end example
3140
3141 This example removes the effect of dynamics from the MIDI output.  Note:
3142 LilyPond's translation modules used for sound are called @q{performers}.
3143
3144 @seealso
3145 Learning Manual:
3146 @rlearning{Other sources of information}.
3147
3148 Notation Reference:
3149 @ref{Expressive marks},
3150 @ref{Score layout}.
3151
3152 Installed Files:
3153 @file{ly/performer-init.ly}.
3154
3155 Snippets:
3156 @rlsr{MIDI}.
3157
3158 Internals Reference:
3159 @rinternals{Dynamic_performer}.
3160
3161 @knownissues
3162 Some MIDI players do not always correctly handle tempo changes in the
3163 midi output.
3164
3165 Changes to the @code{midiInstrument}, as well as some MIDI options, at
3166 the @emph{beginning} of a staff may appear twice in the MIDI output.
3167
3168
3169
3170 @node Using MIDI instruments
3171 @subsection Using MIDI instruments
3172
3173 MIDI instruments are set using the @code{midiInstrument} property within
3174 a @code{Staff} context.
3175
3176 @example
3177 \score @{
3178   \new Staff @{
3179     \set Staff.midiInstrument = #"glockenspiel"
3180     @var{@dots{} music @dots{}}
3181   @}
3182   \midi @{ @}
3183 @}
3184 @end example
3185
3186 or
3187
3188 @example
3189 \score @{
3190   \new Staff \with @{midiInstrument = #"cello"@} @{
3191     @var{@dots{} music @dots{}}
3192   @}
3193   \midi @{ @}
3194 @}
3195 @end example
3196
3197 If the instrument name does not match any of the instruments listed in
3198 the @q{MIDI instruments} section, the @code{acoustic grand} instrument
3199 will be used instead. See @ref{MIDI instruments}.
3200
3201 @seealso
3202 Learning Manual:
3203 @rlearning{Other sources of information}.
3204
3205 Notation Reference:
3206 @ref{MIDI instruments},
3207 @ref{Score layout}.
3208
3209 Installed Files:
3210 @file{scm/midi.scm}.
3211
3212 @knownissues
3213 Percussion instruments that are notated in a @code{DrumStaff}
3214 context will be output, correctly, to MIDI channel@tie{}10 but some
3215 pitched, percussion instruments like the xylophone, marimba, vibraphone
3216 or timpani, are treated as @qq{normal} instruments so the music for
3217 these should be entered in a @code{Staff} (not @code{DrumStaff}) context
3218 to obtain correct MIDI output.  A full list of
3219 @code{channel 10 drum-kits} entries can be found in @file{scm/midi.scm}.
3220 See @rlearning{Other sources of information}.
3221
3222
3223 @node Using repeats with MIDI
3224 @subsection Using repeats with MIDI
3225
3226 @cindex repeats in MIDI
3227 @cindex MIDI using repeats
3228 @funindex \unfoldRepeats
3229
3230 Repeats can be represented in the MIDI output by applying the
3231 @code{\unfoldRepeats} command.
3232
3233 @example
3234 \score @{
3235   \unfoldRepeats @{
3236     \repeat tremolo 8 @{ c'32 e' @}
3237     \repeat percent 2 @{ c''8 d'' @}
3238     \repeat volta 2 @{ c'4 d' e' f' @}
3239     \alternative @{
3240       @{ g' a' a' g' @}
3241       @{ f' e' d' c' @}
3242     @}
3243   @}
3244   \midi @{ @}
3245 @}
3246 @end example
3247
3248 In order to restrict the effect of @code{\unfoldRepeats} to the MIDI
3249 output only, while also generating printable scores, it is necessary to
3250 make @emph{two} @code{\score} blocks; one for MIDI (with unfolded
3251 repeats) and one for the notation (with volta, tremolo, and percent
3252 repeats);
3253
3254 @example
3255 \score @{
3256   @var{@dots{} music @dots{}}
3257   \layout @{ @}
3258 @}
3259 \score @{
3260   \unfoldRepeats @{
3261     @var{@dots{} music @dots{}}
3262   @}
3263   \midi @{ @}
3264 @}
3265 @end example
3266
3267 When using multiple voices, each of the voices must contain completely
3268 unfolded repeats for correct MIDI output.
3269
3270 @seealso
3271 Notation Reference:
3272 @ref{Repeats}.
3273
3274
3275 @node MIDI channel mapping
3276 @subsection MIDI channel mapping
3277
3278 @cindex MIDI Channels
3279 @cindex MIDI Tracks
3280 @funindex midiChannelMapping
3281
3282 When generating a MIDI file from a score, LilyPond will automatically
3283 assign every note in the score to a MIDI channel, the one on which it
3284 should be played when it is sent to a MIDI device.  A MIDI channel has
3285 a number of controls available to select, for example, the instrument
3286 to be used to play the notes on that channel, or to request the MIDI
3287 device to apply various effects to the sound produced on the channel.
3288 At all times, every control on a MIDI channel can have only a single
3289 value assigned to it (which can be modified, however, for example,
3290 to switch to another instrument in the middle of a score).
3291
3292 The MIDI standard supports only 16 channels per MIDI device.  This
3293 limit on the number of channels also limits the number of different
3294 instruments which can be played at the same time.
3295
3296 LilyPond creates separate MIDI tracks for each staff, (or discrete
3297 instrument or voice, depending on the value of
3298 @code{Score.midiChannelMapping}), and also for each lyrics context.
3299 There is no limit to the number of tracks.
3300
3301 To work around the limited number of MIDI channels, LilyPond supports
3302 a number of different modes for MIDI channel allocation, selected using
3303 the @code{Score.midiChannelMapping} context property.  In each case,
3304 if more MIDI channels than the limit are required, the allocated
3305 channel numbers wrap around back to 0, possibly causing the incorrect
3306 assignment of instruments to some notes.  This context property can be
3307 set to one of the following values:
3308
3309 @table @var
3310
3311 @item @code{'staff}
3312
3313 Allocate a separate MIDI channel to each staff in the score (this is
3314 the default).  All notes in all voices contained within each staff will
3315 share the MIDI channel of their enclosing staff, and all are encoded
3316 in the same MIDI track.
3317
3318 The limit of 16 channels is applied to the total number of staff and
3319 lyrics contexts, even though MIDI lyrics do not take up a MIDI channel.
3320
3321 @item @code{'instrument}
3322
3323 Allocate a separate MIDI channel to each distinct MIDI instrument
3324 specified in the score.  This means that all the notes played with the
3325 same MIDI instrument will share the same MIDI channel (and track), even
3326 if the notes come from different voices or staves.
3327
3328 In this case the lyrics contexts do not count towards the MIDI channel
3329 limit of 16 (as they will not be assigned to a MIDI instrument), so
3330 this setting may allow a better allocation of MIDI channels when the
3331 number of staves and lyrics contexts in a score exceeds 16.
3332
3333 @item @code{'voice}
3334
3335 Allocate a separate MIDI channel to each voice in the score that has a
3336 unique name among the voices in its enclosing staff.  Voices in
3337 different staves are always assigned separate MIDI channels, but any two
3338 voices contained within the same staff will share the same MIDI channel
3339 if they have the same name.  Because @code{midiInstrument} and the
3340 several MIDI controls for effects are properties of the staff context,
3341 they cannot be set separately for each voice.  The first voice will be
3342 played with the instrument and effects specified for the staff, and
3343 voices with a different name from the first will be assigned the default
3344 instrument and effects.
3345
3346 Note: different instruments and/or effects can be assigned to several
3347 voices on the same staff by moving the @code{Staff_performer} from the
3348 @code{Staff} to the @code{Voice} context, and leaving
3349 @code{midiChannelMapping} to default to @code{'staff} or set to
3350 @code{'instrument}; see the snippet below.
3351
3352 @end table
3353
3354 For example, the default MIDI channel mapping of a score can be changed
3355 to the @code{'instrument} setting as shown:
3356
3357 @example
3358 \score @{
3359   ...music...
3360   \midi @{
3361     \context @{
3362       \Score
3363       midiChannelMapping = #'instrument
3364     @}
3365   @}
3366 @}
3367 @end example
3368
3369 @snippets
3370 @lilypondfile[verbatim,quote,ragged-right,texidoc,doctitle]
3371 {changing-midi-output-to-one-channel-per-voice.ly}
3372
3373
3374 @node Context properties for MIDI effects
3375 @subsection Context properties for MIDI effects
3376
3377 @cindex Effects in MIDI
3378 @cindex Pan position in MIDI
3379 @cindex Stereo balance in MIDI
3380 @cindex Balance in MIDI
3381 @cindex Expression in MIDI
3382 @cindex Reverb in MIDI
3383 @cindex Chorus level in MIDI
3384 @funindex midiPanPosition
3385 @funindex midiBalance
3386 @funindex midiExpression
3387 @funindex midiReverbLevel
3388 @funindex midiChorusLevel
3389
3390 The following context properties can be used to apply various MIDI
3391 effects to notes played on the MIDI channel associated with the
3392 current staff, MIDI instrument or voice (depending on the value of the
3393 @code{Score.midiChannelMapping} context property and the context in
3394 which the @code{Staff_performer} is located; see
3395 @ref{MIDI channel mapping}).
3396
3397 Changing these context properties will affect all notes played on the
3398 channel after the change, however some of the effects may even apply
3399 also to notes which are already playing (depending on the
3400 implementation of the MIDI output device).
3401
3402 The following context properties are supported:
3403
3404 @table @var
3405
3406 @item @code{Staff.midiPanPosition}
3407
3408 The pan position controls how the sound on a MIDI channel is
3409 distributed between left and right stereo outputs.  The context
3410 property accepts a number between -1.0 (@code{#LEFT}) and 1.0
3411 (@code{#RIGHT}); the value -1.0 will put all sound power to the left
3412 stereo output (keeping the right output silent), the value 0.0
3413 (@code{#CENTER}) will distribute the sound evenly between the left and
3414 right stereo outputs, and the value 1.0 will move all sound to the
3415 right stereo output.  Values between -1.0 and 1.0 can be used to obtain
3416 mixed distributions between left and right stereo outputs.
3417
3418 @item @code{Staff.midiBalance}
3419
3420 The stereo balance of a MIDI channel.  Similarly to the pan position,
3421 this context property accepts a number between -1.0 (@code{#LEFT}) and
3422 1.0 (@code{#RIGHT}).  It varies the relative volume sent to the two
3423 stereo speakers without affecting the distribution of the stereo
3424 signals.
3425
3426 @item @code{Staff.midiExpression}
3427
3428 Expression level (as a fraction of the maximum available level) to
3429 apply to a MIDI channel.  A MIDI device combines the MIDI channel's
3430 expression level with a voice's current dynamic level (controlled using
3431 constructs such as @code{\p} or @code{\ff}) to obtain the total volume
3432 of each note within the voice.  The expression control could be used, for
3433 example, to implement crescendo or decrescendo effects over single
3434 sustained notes (not supported automatically by LilyPond).
3435
3436 @c Issue 4059 contains an attached snippet which shows how this might
3437 @c be done, but this is too large and complex for the NR, even as a
3438 @c referenced snippet.  It could be added to the LSR.
3439
3440 The expression level ranges from 0.0 (no expression, meaning zero
3441 volume) to 1.0 (full expression).
3442
3443 @item @code{Staff.midiReverbLevel}
3444
3445 Reverb level (as a fraction of the maximum available level) to apply
3446 to a MIDI channel.  This property accepts numbers between 0.0 (no
3447 reverb) and 1.0 (full effect).
3448
3449 @item @code{Staff.midiChorusLevel}
3450
3451 Chorus level (as a fraction of the maximum available level) to apply to
3452 a MIDI channel.  This property accepts numbers between 0.0 (no chorus
3453 effect) and 1.0 (full effect).
3454
3455 @end table
3456
3457
3458 @knownissues
3459
3460 As MIDI files do not contain any actual audio data, changes in these
3461 context properties translate only to requests for changing MIDI channel
3462 controls in the outputted MIDI files.  Whether a particular MIDI device
3463 (such as a software MIDI player) can actually handle any of these
3464 requests in a MIDI file is entirely up to the implementation of the
3465 device: a device may choose to ignore some or all of these requests.
3466 Also, how a MIDI device will interpret different values for these
3467 controls (generally, the MIDI standard fixes the behavior only at the
3468 endpoints of the value range available for each control), and whether a
3469 change in the value of a control will affect notes already playing on
3470 that MIDI channel or not, is also specific to the MIDI device
3471 implementation.
3472
3473 When generating MIDI files, LilyPond will simply transform the
3474 fractional values within each range linearly into values in a
3475 corresponding (7-bit, or 14-bit for MIDI channel controls which support
3476 fine resolution) integer range (0-127 or 0-32767, respectively),
3477 rounding fractional values towards the nearest integer away from zero.
3478 The converted integer values are stored as-is in the generated MIDI
3479 file.  Please consult the documentation of your MIDI device for
3480 information about how the device interprets these values.
3481
3482
3483 @node Enhancing MIDI output
3484 @subsection Enhancing MIDI output
3485
3486 @menu
3487 * The articulate script::
3488 @end menu
3489
3490 The default MIDI output is basic but can be improved by setting MIDI
3491 instruments, @code{\midi} block properties and/or using the
3492 @file{articulate} script.
3493
3494 @cindex instrument names
3495 @cindex MIDI, instruments
3496 @cindex articulate script
3497 @cindex articulate.ly
3498 @funindex Staff.midiInstrument
3499
3500
3501 @node The articulate script
3502 @unnumberedsubsubsec The @file{articulate} script
3503
3504 To use the @file{articulate} script add the appropriate @code{\include}
3505 command at the top of the input file;
3506
3507 @example
3508 \include "articulate.ly"
3509 @end example
3510
3511 The script creates MIDI output into appropriately @q{time-scaled} notes
3512 to match many articulation and tempo indications.  Engraved output
3513 however, will also be altered to literally match the MIDI output.
3514
3515 @example
3516 \score @{
3517   \articulate <<
3518     @var{@dots{} music @dots{}}
3519   >>
3520   \midi @{ @}
3521 @}
3522 @end example
3523
3524 The @code{\articulate} command enables abbreviatures (such as trills and
3525 turns) to be processed.  A full list of supported items can be found in
3526 the script itself.  See @file{ly/articulate.ly}.
3527
3528 @seealso
3529 Learning Manual:
3530 @rlearning{Other sources of information}.
3531
3532 Notation Reference:
3533 @ref{Score layout}.
3534
3535 Installed Files:
3536 @file{ly/articulate.ly}.
3537
3538 @warning{The @file{articulate} script may shorten chords, which might
3539 not be appropriate for some types of instrument, such as organ music.
3540 Notes that do not have any articulations attached to them may also be
3541 shortened; so to compensate for this, restrict the use of the
3542 @code{\articulate} function to shorter segments of music or modify the
3543 values of the variables defined in the @file{articulate} script to
3544 compenstate for the note-shortening behavior.}
3545
3546
3547
3548 @node Extracting musical information
3549 @section Extracting musical information
3550
3551 In addition to creating graphical output and MIDI, LilyPond can
3552 display musical information as text.
3553
3554 @menu
3555 * Displaying LilyPond notation::
3556 * Displaying scheme music expressions::
3557 * Saving music events to a file::
3558 @end menu
3559
3560 @node Displaying LilyPond notation
3561 @subsection Displaying LilyPond notation
3562
3563 @funindex \displayLilyMusic
3564 Displaying a music expression in LilyPond notation can be
3565 done with the music function @code{\displayLilyMusic}.  To see the
3566 output, you will typically want to call LilyPond using the command
3567 line.  For example,
3568
3569 @example
3570 @{
3571   \displayLilyMusic \transpose c a, @{ c4 e g a bes @}
3572 @}
3573 @end example
3574
3575 will display
3576
3577 @example
3578 @{ a,4 cis4 e4 fis4 g4 @}
3579 @end example
3580
3581 By default, LilyPond will print these messages to the console
3582 along with all the other LilyPond compilation messages.  To split
3583 up these messages and save the results of @code{\displayLilyMusic},
3584 redirect the output to a file.
3585
3586 @example
3587 lilypond file.ly >display.txt
3588 @end example
3589
3590 @funindex \void
3591 Note that Lilypond does not just display the music expression, but
3592 also interprets it (since @code{\displayLilyMusic} returns it in
3593 addition to displaying it).  This is convenient since you can just
3594 insert @code{\displayLilyMusic} into existing music in order to get
3595 information about it.  If you don't actually want Lilypond to
3596 interpret the displayed music as well as display it, use @code{\void}
3597 in order to have it ignored:
3598
3599 @example
3600 @{
3601   \void \displayLilyMusic \transpose c a, @{ c4 e g a bes @}
3602 @}
3603 @end example
3604
3605
3606 @node Displaying scheme music expressions
3607 @subsection Displaying scheme music expressions
3608
3609 See @rextend{Displaying music expressions}.
3610
3611
3612 @node Saving music events to a file
3613 @subsection Saving music events to a file
3614
3615 Music events can be saved to a file on a per-staff basis by
3616 including a file in your main score.
3617
3618 @example
3619 \include "event-listener.ly"
3620 @end example
3621
3622 This will create file(s) called @file{FILENAME-STAFFNAME.notes} or
3623 @file{FILENAME-unnamed-staff.notes} for each staff.  Note that if
3624 you have multiple unnamed staves, the events for all staves will
3625 be mixed together in the same file.  The output looks like this:
3626
3627 @example
3628 0.000   note     57       4   p-c 2 12
3629 0.000   dynamic  f
3630 0.250   note     62       4   p-c 7 12
3631 0.500   note     66       8   p-c 9 12
3632 0.625   note     69       8   p-c 14 12
3633 0.750   rest     4
3634 0.750   breathe
3635 @end example
3636
3637 The syntax is a tab-delimited line, with two fixed fields on each
3638 line followed by optional parameters.
3639
3640 @example
3641 @var{time}  @var{type}  @var{@dots{}params@dots{}}
3642 @end example
3643
3644 This information can easily be read into other programs such as
3645 python scripts, and can be very useful for researchers wishing to
3646 perform musical analysis or playback experiments with LilyPond.
3647
3648
3649 @knownissues
3650
3651 Not all lilypond music events are supported by
3652 @file{event-listener.ly}.  It is intended to be a well-crafted
3653 @qq{proof of concept}.  If some events that you want to see are
3654 not included, copy @file{event-listener.ly} into your lilypond
3655 directory and modify the file so that it outputs the information
3656 you want.