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