]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/notation/spacing.itely
Issue 2760: CG wants all engravers to have double-quotes around them
[lilypond.git] / Documentation / notation / spacing.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.16.0"
12
13 @ignore
14 GDP TODO list
15
16 Negative numbers are allowed:
17 > Are you sure? The following works well
18 > \paper{
19 >   first-page-number = -2
20 > }
21 > and prints page number -1 on the second page, for example.
22
23
24 In 5.2.1 the @refbugs (line 495 in spacing.itely on master) it
25 states:
26
27 "@code{layout-set-staff-size} does not change the distance between
28 the
29 staff lines."
30
31 Could we add a sentence:
32 "Use instead the pair               fontSize = #@var{N}
33             \override StaffSymbol #'staff-space = #(magstep
34 @var{N})
35 inside the Staff context to change the size of the font and the
36 distance between
37 staff lines accordingly."
38
39 Actually I found, that the @internalsref{StaffSymbol} at line 481
40 sends to an incomplete
41 documentation.  The property staff-space is not explained here.  I
42 thought Y-extent might be of
43 help, but it is in turn explained by x-space which again is
44 missing from the list.  Who has the
45 knowledge to fix this?
46
47
48 Clarify
49 http://code.google.com/p/lilypond/issues/detail?id=68
50
51 @end ignore
52
53
54 @node Spacing issues
55 @chapter Spacing issues
56
57 The global paper layout is determined by three factors: the page layout, the
58 line breaks, and the spacing.  These all influence each other.  The
59 choice of spacing determines how densely each system of music is set.
60 This influences where line breaks are chosen, and thus ultimately, how
61 many pages a piece of music takes.
62
63 Globally speaking, this procedure happens in four steps: first,
64 flexible distances (@q{springs}) are chosen, based on durations.  All
65 possible line breaking combinations are tried, and a @q{badness} score
66 is calculated for each.  Then the height of each possible system is
67 estimated.  Finally, a page breaking and line breaking combination is chosen
68 so that neither the horizontal nor the vertical spacing is too cramped
69 or stretched.
70
71 Two types of blocks can contain layout settings:
72 @code{\paper @{@dots{}@}} and @code{\layout @{@dots{}@}}.  The
73 @code{\paper} block contains page layout settings that are
74 expected to be the same for all scores in a book, such as the
75 paper height, or whether to print page numbers, etc.  See
76 @ref{Page layout}.  The @code{\layout} block contains score layout
77 settings, such as the number of systems to use, or the space
78 between staff-groups, etc.  See @ref{Score layout}.
79
80 @menu
81 * Page layout::
82 * Score layout::
83 * Breaks::
84 * Vertical spacing::
85 * Horizontal spacing::
86 * Fitting music onto fewer pages::
87 @end menu
88
89
90 @node Page layout
91 @section Page layout
92
93 This section discusses page layout options for the @code{\paper}
94 block.
95
96 @menu
97 * The \paper block::
98 * Paper size and automatic scaling::
99 * Fixed vertical spacing \paper variables::
100 * Flexible vertical spacing \paper variables::
101 * Horizontal spacing \paper variables::
102 * Other \paper variables::
103 @end menu
104
105
106 @node The \paper block
107 @subsection The @code{\paper} block
108
109 The @code{\paper} block can appear within a @code{\book} block,
110 but not within a @code{\score} block.  Settings in a @code{\paper}
111 block apply to the entire book, which may include multiple scores.
112 Settings that can appear in a @code{\paper} block include:
113
114 @itemize
115
116 @item
117 the @code{set-paper-size} scheme function,
118
119 @item
120 @code{\paper} variables used for customizing page layout, and
121
122 @item
123 markup definitions used for customizing the layout of headers,
124 footers, and titles.
125
126 @end itemize
127
128 The @code{set-paper-size} function is discussed in the next
129 section, @ref{Paper size and automatic scaling}.  The
130 @code{\paper} variables that deal with page layout are discussed
131 in later sections.  The markup definitions that deal with headers,
132 footers, and titles are discussed in
133 @ref{Custom headers footers and titles}.
134
135 Most @code{\paper} variables will only work in a @code{\paper}
136 block.  The few that will also work in a @code{\layout} block are
137 listed in @ref{The \layout block}.
138
139 Except when specified otherwise, all @code{\paper} variables that
140 correspond to distances on the page are measured in millimeters,
141 unless a different unit is specified by the user.  For example,
142 the following declaration sets @code{top-margin} to ten
143 millimeters:
144
145 @example
146 \paper @{
147   top-margin = 10
148 @}
149 @end example
150
151 To set it to @code{0.5} inches, use the @code{\in} unit suffix:
152
153 @example
154 \paper @{
155   top-margin = 0.5\in
156 @}
157 @end example
158
159 The available unit suffixes are @code{\mm}, @code{\cm},
160 @code{\in}, and @code{\pt}.  These units are simple values for
161 converting from millimeters; they are defined in
162 @file{ly/paper-defaults-init.ly}.  For the sake of clarity, when
163 using millimeters, the @code{\mm} is typically included in the
164 code, even though it is not technically necessary.
165
166 It is also possible to define @code{\paper} values using Scheme.
167 The Scheme equivalent of the above example is:
168
169 @example
170 \paper @{
171   #(define top-margin (* 0.5 in))
172 @}
173 @end example
174
175 @seealso
176 Notation Reference:
177 @ref{Paper size and automatic scaling},
178 @ref{Custom headers footers and titles},
179 @ref{The \layout block}.
180
181 Installed Files:
182 @file{ly/paper-defaults-init.ly}.
183
184
185 @node Paper size and automatic scaling
186 @subsection Paper size and automatic scaling
187
188 @cindex paper size
189 @cindex page size
190
191 @funindex \paper
192
193 @menu
194 * Setting the paper size::
195 * Automatic scaling to paper size::
196 @end menu
197
198
199 @node Setting the paper size
200 @unnumberedsubsubsec Setting the paper size
201
202 @q{A4} is the default value when no explicit paper size is set. However,
203 there are two functions that can be used to change it
204 @code{set-default-paper-size},
205
206 @example
207 #(set-default-paper-size "quarto")
208 @end example
209
210 which must always be placed at the toplevel scope. and
211 @code{set-paper-size},
212
213 @example
214 \paper @{
215   #(set-paper-size "tabloid")
216 @}
217 @end example
218
219 which must always be placed in a @code{\paper} block.
220
221 If the @code{set-default-paper-size} function is used in the toplevel
222 scope, it must come before the any @code{\paper} block.
223 @code{set-default-paper-size} sets the paper size for all pages,
224 whereas @code{set-paper-size} only sets the paper size for the pages
225 that the @code{\paper} block applies to.  For example, if the
226 @code{\paper} block is at the top of the file, then it will apply the
227 paper size to all pages.  If the @code{\paper} block is inside a
228 @code{\book}, then the paper size will only apply to that book.
229
230 When the @code{set-paper-size} function is used, it must be
231 placed @emph{before} any other functions used within the same
232 @code{\paper} block.  See @ref{Automatic scaling to paper size}.
233
234 Paper sizes are defined in @file{scm/paper.scm}, and while it is
235 possible to add custom sizes, they will be overwritten on subsequent
236 software updates.  The available paper sizes are listed in
237 @ref{Predefined paper sizes}.
238
239 @c An appendix entry exists for paper sizes but is not auto-generated
240
241 The following command can be used in the file to add a custom paper size
242 which can then be used with @code{set-default-paper-size} or
243 @code{set-paper-size} as appropriate,
244
245 @example
246 #(set! paper-alist (cons '("my size" . (cons (* 15 in) (* 3 in))) paper-alist))
247
248 \paper @{
249   #(set-paper-size "my size")
250 @}
251 @end example
252
253 The units @code{in} (inches), @code{cm} (centimeters) and @code{mm}
254 (millimeters) can all be used.
255
256 @cindex paper size, orientation
257 @cindex page, orientation
258 @cindex paper size, landscape
259
260 If the symbol @code{'landscape} is added to the paper size function,
261 pages will be rotated by 90 degrees, and wider line widths will be set
262 accordingly.
263
264 @example
265 #(set-default-paper-size "a6" 'landscape)
266 @end example
267
268 Swapping the paper dimensions @emph{without} having the print rotated
269 (like when printing to postcard size, or creating graphics for inclusion
270 rather than a standalone document) can be achieved by appending
271 @samp{landscape} to the name of the paper size itself:
272
273 @example
274 #(set-default-paper-size "a6landscape")
275 @end example
276
277 When the paper size ends with an explicit @samp{landscape} or
278 @samp{portrait}, the presence of a @code{'landscape} symbol @emph{only}
279 affects print orientation, not the paper dimensions used for layout.
280
281 @seealso
282 Notation Reference:
283 @ref{Automatic scaling to paper size},
284 @ref{Predefined paper sizes}.
285
286 Installed Files:
287 @file{scm/paper.scm}.
288
289
290 @node Automatic scaling to paper size
291 @unnumberedsubsubsec Automatic scaling to paper size
292
293 If the paper size is changed with one of the scheme functions
294 (@code{set-default-paper-size} or @code{set-paper-size}), the
295 values of several @code{\paper} variables are automatically scaled
296 to the new size.  To bypass the automatic scaling for a particular
297 variable, set the variable after setting the paper size.  Note
298 that the automatic scaling is not triggered by setting the
299 @code{paper-height} or @code{paper-width} variables, even though
300 @code{paper-width} can influence other values (this is separate
301 from scaling and is discussed below).  The
302 @code{set-default-paper-size} and @code{set-paper-size} functions
303 are described in @ref{Setting the paper size}.
304
305 The vertical dimensions affected by automatic scaling are
306 @code{top-margin} and @code{bottom-margin} (see
307 @ref{Fixed vertical spacing \paper variables}).  The horizontal
308 dimensions affected by automatic scaling are @code{left-margin},
309 @code{right-margin}, @code{inner-margin}, @code{outer-margin},
310 @code{binding-offset}, @code{indent}, and @code{short-indent} (see
311 @ref{Horizontal spacing \paper variables}).
312
313 The default values for these dimensions are set in
314 @file{ly/paper-defaults-init.ly}, using internal variables named
315 @code{top-margin-default}, @code{bottom-margin-default}, etc.
316 These are the values that result at the default paper size
317 @code{a4}.  For reference, with @code{a4} paper the
318 @code{paper-height} is @code{297\mm} and the @code{paper-width} is
319 @code{210\mm}.
320
321 @seealso
322 Notation Reference:
323 @ref{Fixed vertical spacing \paper variables},
324 @ref{Horizontal spacing \paper variables}.
325
326 Installed Files:
327 @file{ly/paper-defaults-init.ly},
328 @file{scm/paper.scm}.
329
330
331 @node Fixed vertical spacing \paper variables
332 @subsection Fixed vertical spacing @code{\paper} variables
333
334 @warning{Some @code{@bs{}paper} dimensions are automatically
335 scaled to the paper size, which may lead to unexpected behavior.
336 See @ref{Automatic scaling to paper size}.}
337
338 Default values (before scaling) are defined in
339 @file{ly/paper-defaults-init.ly}.
340
341 @table @code
342 @item paper-height
343 @funindex paper-height
344
345 The height of the page, unset by default.  Note that the automatic
346 scaling of some vertical dimensions is not affected by this.
347
348 @item top-margin
349 @funindex top-margin
350
351 The margin between the top of the page and the top of the
352 printable area.  If the paper size is modified, this dimension's
353 default value is scaled accordingly.
354
355 @item bottom-margin
356 @funindex bottom-margin
357
358 The margin between the bottom of the printable area and the bottom
359 of the page.  If the paper size is modified, this dimension's
360 default value is scaled accordingly.
361
362 @item ragged-bottom
363 @funindex ragged-bottom
364
365 If set to true, systems will not spread vertically down the page.
366 This does not affect the last page.  This should be set to true
367 for pieces that have only two or three systems per page, for
368 example orchestral scores.
369
370 @item ragged-last-bottom
371 @funindex ragged-last-bottom
372
373 If set to false, systems will spread vertically down the last
374 page.  Pieces that amply fill two pages or more should have this
375 set to false.  It also affects the last page of book parts, i.e.
376 parts of a book created with @code{\bookpart} blocks.
377
378 @end table
379
380 @seealso
381 Notation Reference:
382 @ref{Automatic scaling to paper size}.
383
384 Installed Files:
385 @file{ly/paper-defaults-init.ly}.
386
387 Snippets:
388 @rlsr{Spacing}.
389
390 @knownissues
391 The titles (from the @code{\header} block) are treated as a
392 system, so @code{ragged-bottom} and @code{ragged-last-bottom} will
393 add space between the titles and the first system of the score.
394
395 Explicitly defined paper-sizes will override any user-defined top or
396 bottom margin settings.
397
398
399 @node Flexible vertical spacing \paper variables
400 @subsection Flexible vertical spacing @code{\paper} variables
401
402 In most cases, it is preferable for the vertical distances between
403 certain items (such as margins, titles, systems, and separate
404 scores) to be flexible, so that they stretch and compress nicely
405 according to each situation.  A number of @code{\paper} variables
406 (listed below) are available to fine-tune the stretching behavior
407 of these dimensions.
408
409 Note that the @code{\paper} variables discussed in this section do
410 not control the spacing of staves within individual systems.
411 Within-system spacing is controlled by grob properties, with
412 settings typically entered inside a @code{\score} or
413 @code{\layout} block, and not inside a @code{\paper} block.  See
414 @ref{Flexible vertical spacing within systems}.
415
416 @menu
417 * Structure of flexible vertical spacing alists::
418 * List of flexible vertical spacing \paper variables::
419 @end menu
420
421
422 @node Structure of flexible vertical spacing alists
423 @unnumberedsubsubsec Structure of flexible vertical spacing alists
424
425 Each of the flexible vertical spacing @code{\paper} variables is
426 an alist (association list) containing four @emph{keys}:
427
428 @itemize
429
430 @item
431 @code{basic-distance} -- the vertical distance, measured in
432 staff-spaces, between the @emph{reference points} of the two
433 items, when no collisions would result, and no stretching or
434 compressing is in effect.  The reference point of a (title or
435 top-level) markup is its highest point, and the reference point of
436 a system is the vertical center of the nearest @code{StaffSymbol}
437 -- even if a non-staff line (such as a @code{Lyrics} context) is
438 in the way.  Values for @code{basic-distance} that are less than
439 either @code{padding} or @code{minimum-distance} are not
440 meaningful, since the resulting distance will never be less than
441 either @code{padding} or @code{minimum-distance}.
442
443 @item
444 @code{minimum-distance} -- the smallest allowable vertical
445 distance, measured in staff-spaces, between the reference points
446 of the two items, when compressing is in effect.  Values for
447 @code{minimum-distance} that are less than @code{padding} are not
448 meaningful, since the resulting distance will never be less than
449 @code{padding.}
450
451 @c TODO: explain skylines somewhere and xref to it from here.
452
453 @item
454 @code{padding} -- the minimum required amount of unobstructed
455 vertical whitespace between the bounding boxes (or skylines) of
456 the two items, measured in staff-spaces.
457
458 @item
459 @code{stretchability} -- a unitless measure of the dimension's
460 relative propensity to stretch.  If zero, the distance will not
461 stretch (unless collisions would result).  When positive, the
462 significance of a particular dimension's @code{stretchability}
463 value lies only in its relation to the @code{stretchability}
464 values of the other dimensions.  For example, if one dimension has
465 twice the @code{stretchability} of another, it will stretch twice
466 as easily.  Values should be non-negative and finite.  The value
467 @code{+inf.0} triggers a @code{programming_error} and is ignored,
468 but @code{1.0e7} can be used for an almost infinitely stretchable
469 spring.  If unset, the default value is set to
470 @code{basic-distance}.  Note that the dimension's propensity to
471 @emph{compress} cannot be directly set by the user and is equal to
472 (@code{basic-distance}@tie{}@minus{}@tie{}@code{minimum-distance}).
473
474 @end itemize
475
476 If a page has a ragged bottom, the resulting distance is the
477 largest of:
478
479 @itemize
480
481 @item
482 @code{basic-distance},
483
484 @item
485 @code{minimum-distance}, and
486
487 @item
488 @code{padding} plus the smallest distance necessary to eliminate
489 collisions.
490
491 @end itemize
492
493 For multi-page scores with a ragged bottom on the last page, the last
494 page uses the same spacing as the preceding page, provided there is
495 enough space for that.
496
497 Specific methods for modifying alists are discussed in
498 @ref{Modifying alists}.  The following example demonstrates the
499 two ways these alists can be modified.  The first declaration
500 updates one key-value individually, and the second completely
501 redefines the variable:
502
503 @example
504 \paper @{
505   system-system-spacing #'basic-distance = #8
506   score-system-spacing =
507     #'((basic-distance . 12)
508        (minimum-distance . 6)
509        (padding . 1)
510        (stretchability . 12))
511 @}
512 @end example
513
514
515 @node List of flexible vertical spacing \paper variables
516 @unnumberedsubsubsec List of flexible vertical spacing @code{\paper} variables
517
518 The names of these variables follow the format
519 @code{@var{upper}-@var{lower}-spacing}, where @code{@var{upper}}
520 and @code{@var{lower}} are the items to be spaced.  Each distance
521 is measured between the reference points of the two items (see the
522 description of the alist structure above).  Note that in these
523 variable names, the term @q{@code{markup}} refers to both
524 @emph{title markups} (@code{bookTitleMarkup} or
525 @code{scoreTitleMarkup}) and @emph{top-level markups} (see
526 @ref{File structure}).  All distances are measured in
527 staff-spaces.
528
529 Default settings are defined in @file{ly/paper-defaults-init.ly}.
530
531 @c TODO: Where do headers/footers fit in? -mp
532
533 @table @code
534 @item markup-system-spacing
535 @funindex markup-system-spacing
536
537 the distance between a (title or top-level) markup and the system
538 that follows it.
539
540 @item score-markup-spacing
541 @funindex score-markup-spacing
542
543 the distance between the last system of a score and the (title or
544 top-level) markup that follows it.
545
546 @item score-system-spacing
547 @funindex score-system-spacing
548
549 the distance between the last system of a score and the first
550 system of the score that follows it, when no (title or top-level)
551 markup exists between them.
552
553 @item system-system-spacing
554 @funindex system-system-spacing
555
556 the distance between two systems in the same score.
557
558 @item markup-markup-spacing
559 @funindex markup-markup-spacing
560
561 the distance between two (title or top-level) markups.
562
563 @item last-bottom-spacing
564 @funindex last-bottom-spacing
565
566 the distance from the last system or top-level markup on a page to
567 the bottom of the printable area (i.e. the top of the bottom
568 margin).
569
570 @item top-system-spacing
571 @funindex top-system-spacing
572
573 the distance from the top of the printable area (i.e. the bottom
574 of the top margin) to the first system on a page, when there is no
575 (title or top-level) markup between the two.
576
577 @item top-markup-spacing
578 @funindex top-markup-spacing
579
580 the distance from the top of the printable area (i.e. the bottom
581 of the top margin) to the first (title or top-level) markup on a
582 page, when there is no system between the two.
583 @end table
584
585 @seealso
586 Notation Reference:
587 @ref{Flexible vertical spacing within systems}.
588
589 Installed Files:
590 @file{ly/paper-defaults-init.ly}.
591
592 Snippets:
593 @rlsr{Spacing}.
594
595
596 @node Horizontal spacing \paper variables
597 @subsection Horizontal spacing @code{\paper} variables
598
599 @warning{Some @code{@bs{}paper} dimensions are automatically
600 scaled to the paper size, which may lead to unexpected behavior.
601 See @ref{Automatic scaling to paper size}.}
602
603 @menu
604 * \paper variables for widths and margins::
605 * \paper variables for two-sided mode::
606 * \paper variables for shifts and indents::
607 @end menu
608
609
610 @node \paper variables for widths and margins
611 @unnumberedsubsubsec @code{\paper} variables for widths and margins
612
613 Default values (before scaling) that are not listed here are
614 defined in @file{ly/paper-defaults-init.ly}.
615
616 @table @code
617
618 @item paper-width
619 @funindex paper-width
620
621 The width of the page, unset by default.  While @code{paper-width}
622 has no effect on the automatic scaling of some horizontal
623 dimensions, it does influence the @code{line-width} variable.  If
624 both @code{paper-width} and @code{line-width} are set, then
625 @code{left-margin} and @code{right-margin} will also be updated.
626 Also see @code{check-consistency}.
627
628 @item line-width
629 @funindex line-width
630
631 The horizontal extent of the staff lines in unindented, non-ragged
632 systems, equal to
633 @code{(paper-width@tie{}@minus{}@tie{}left-margin@tie{}@minus{}@tie{}right-margin)}
634 when unset.  If @code{line-width} is set, and both
635 @code{left-margin} and @code{right-margin} are unset, then the
636 margins will be updated to center the systems on the page
637 automatically.  Also see @code{check-consistency}.  This variable
638 can also be set in a @code{\layout} block.
639
640 @item left-margin
641 @funindex left-margin
642
643 The margin between the left edge of the page and the start of the
644 staff lines in unindented systems.  If the paper size is modified,
645 this dimension's default value is scaled accordingly.  If
646 @code{left-margin} is unset, and both @code{line-width} and
647 @code{right-margin} are set, then @code{left-margin} is set to
648 @code{(paper-width@tie{}@minus{}@tie{}line-width@tie{}@minus{}@tie{}right-margin)}.
649 If only @code{line-width} is set, then both margins are set to
650 @code{((paper-width@tie{}@minus{}@tie{}line-width)@tie{}/@tie{}2)},
651 and the systems are consequently centered on the page.  Also see
652 @code{check-consistency}.
653
654 @item right-margin
655 @funindex right-margin
656
657 The margin between the right edge of the page and the end of the
658 staff lines in non-ragged systems.  If the paper size is modified,
659 this dimension's default value is scaled accordingly.  If
660 @code{right-margin} is unset, and both @code{line-width} and
661 @code{left-margin} are set, then @code{right-margin} is set to
662 @code{(paper-width@tie{}@minus{}@tie{}line-width@tie{}@minus{}@tie{}left-margin)}.
663 If only @code{line-width} is set, then both margins are set to
664 @code{((paper-width@tie{}@minus{}@tie{}line-width)@tie{}/@tie{}2)},
665 and the systems are consequently centered on the page.  Also see
666 @code{check-consistency}.
667
668 @item check-consistency
669 @funindex check-consistency
670
671 If set to true, print a warning if @code{left-margin},
672 @code{line-width}, and @code{right-margin} do not exactly add up
673 to @code{paper-width}, and replace each of these (except
674 @code{paper-width}) with its default value (scaled to the paper
675 size if necessary).  If set to false, ignore any inconsistencies
676 and allow systems to run off the edge of the page.
677
678 @item ragged-right
679 @funindex ragged-right
680
681 If set to true, systems will not fill the line width.  Instead,
682 systems end at their natural horizontal length.  Default:
683 @code{#t} for scores with only one system, and @code{#f} for
684 scores with two or more systems.  This variable can also be set in
685 a @code{\layout} block.
686
687 @item ragged-last
688 @funindex ragged-last
689
690 If set to true, the last system in the score will not fill the
691 line width.  Instead the last system ends at its natural
692 horizontal length.  Default: @code{#f}.  This variable can also be
693 set in a @code{\layout} block.
694
695 @end table
696
697 @seealso
698 Notation Reference:
699 @ref{Automatic scaling to paper size}.
700
701 Installed Files:
702 @file{ly/paper-defaults-init.ly}.
703
704 @knownissues
705 Explicitly defined paper-sizes will override any user-defined left or
706 right margin settings.
707
708
709 @node \paper variables for two-sided mode
710 @unnumberedsubsubsec @code{\paper} variables for two-sided mode
711
712 Default values (before scaling) are defined in
713 @file{ly/paper-defaults-init.ly}.
714
715 @table @code
716
717 @item two-sided
718 @funindex two-sided
719
720 @cindex gutter
721 @cindex binding gutter
722
723 If set to true, use @code{inner-margin}, @code{outer-margin} and
724 @code{binding-offset} to determine margins depending on whether
725 the page number is odd or even.  This overrides @code{left-margin}
726 and @code{right-margin}.
727
728 @item inner-margin
729 @funindex inner-margin
730
731 The margin all pages have at the inner side if they are part of a
732 book.  If the paper size is modified, this dimension's default
733 value is scaled accordingly.  Works only with @code{two-sided} set
734 to true.
735
736 @item outer-margin
737 @funindex outer-margin
738
739 The margin all pages have at the outer side if they are part of a
740 book.  If the paper size is modified, this dimension's default
741 value is scaled accordingly.  Works only with @code{two-sided} set
742 to true.
743
744 @item binding-offset
745 @funindex binding-offset
746
747 The amount @code{inner-margin} is increased to make sure nothing
748 will be hidden by the binding.  If the paper size is modified,
749 this dimension's default value is scaled accordingly.  Works only
750 with @code{two-sided} set to true.
751
752 @end table
753
754 @seealso
755 Notation Reference:
756 @ref{Automatic scaling to paper size}.
757
758 Installed Files:
759 @file{ly/paper-defaults-init.ly}.
760
761
762 @node \paper variables for shifts and indents
763 @unnumberedsubsubsec @code{\paper} variables for shifts and indents
764
765 Default values (before scaling) that are not listed here are
766 defined in @file{ly/paper-defaults-init.ly}.
767
768 @table @code
769
770 @item horizontal-shift
771 @funindex horizontal-shift
772
773 @c This default value is buried in the middle of page.scm.  -mp
774
775 The amount that all systems (including titles and system
776 separators) are shifted to the right.  Default: @code{0.0\mm}.
777
778 @item indent
779 @funindex indent
780
781 The level of indentation for the first system in a score.  If the
782 paper size is modified, this dimension's default value is scaled
783 accordingly.  This variable can also be set in a @code{\layout}
784 block.
785
786 @item short-indent
787 @funindex short-indent
788
789 The level of indentation for all systems in a score besides the
790 first system.  If the paper size is modified, this dimension's
791 default value is scaled accordingly.  This variable can also be
792 set in a @code{\layout} block.
793
794 @end table
795
796 @seealso
797 Notation Reference:
798 @ref{Automatic scaling to paper size}.
799
800 Installed Files:
801 @file{ly/paper-defaults-init.ly}.
802
803 Snippets:
804 @rlsr{Spacing}.
805
806
807 @node Other \paper variables
808 @subsection Other @code{\paper} variables
809
810 @menu
811 * \paper variables for line breaking::
812 * \paper variables for page breaking::
813 * \paper variables for page numbering::
814 * Miscellaneous \paper variables::
815 @end menu
816
817
818 @node \paper variables for line breaking
819 @unnumberedsubsubsec @code{\paper} variables for line breaking
820
821 @c TODO: Mention that ly:optimal-breaking is on by default? -mp
822
823 @table @code
824
825 @item max-systems-per-page
826 @funindex max-systems-per-page
827
828 The maximum number of systems that will be placed on a page.  This
829 is currently supported only by the @code{ly:optimal-breaking} algorithm.
830 Default: unset.
831
832 @item min-systems-per-page
833 @funindex min-systems-per-page
834
835 The minimum number of systems that will be placed on a page.  This
836 may cause pages to be overfilled if it is made too large.  This is
837 currently supported only by the @code{ly:optimal-breaking} algorithm.
838 Default: unset.
839
840 @item systems-per-page
841 @funindex systems-per-page
842
843 The number of systems that should be placed on each page.
844 This is currently supported only by the @code{ly:optimal-breaking} algorithm.
845 Default: unset.
846
847 @item system-count
848 @funindex system-count
849
850 The number of systems to be used for a score.  Default: unset.
851 This variable can also be set in a @code{\layout} block.
852
853 @end table
854
855 @seealso
856 Notation Reference:
857 @ref{Line breaking}.
858
859
860 @node \paper variables for page breaking
861 @unnumberedsubsubsec @code{\paper} variables for page breaking
862
863 Default values not listed here are defined in
864 @file{ly/paper-defaults-init.ly}
865
866 @table @code
867
868 @item blank-after-score-page-force
869 @funindex blank-after-score-page-force
870
871 The penalty for having a blank page after the end of one score and
872 before the next.  By default, this is smaller than
873 @code{blank-page-force}, so that we prefer blank pages after
874 scores to blank pages within a score.
875
876 @item blank-last-page-force
877 @funindex blank-last-page-force
878
879 The penalty for ending the score on an odd-numbered page.
880
881 @item blank-page-force
882 @funindex blank-page-force
883
884 The penalty for having a blank page in the middle of a
885 score.  This is not used by @code{ly:optimal-breaking} since it will
886 never consider blank pages in the middle of a score.
887
888 @item page-breaking
889 @funindex page-breaking
890
891 The page-breaking algorithm to use.  Choices are
892 @code{ly:minimal-breaking}, @code{ly:page-turn-breaking}, and
893 @code{ly:optimal-breaking}.
894
895 @item page-breaking-system-system-spacing
896 @funindex page-breaking-system-system-spacing
897
898 Tricks the page breaker into thinking that
899 @code{system-system-spacing} is set to something different than
900 it really is.  For example, if
901 @code{page-breaking-system-system-spacing #'padding} is set to something
902 substantially larger than @code{system-system-spacing #'padding}, then the
903 page-breaker will put fewer systems on each page.  Default: unset.
904
905 @item page-count
906 @funindex page-count
907
908 The number of pages to be used for a score, unset by default.
909
910 @end table
911
912 @seealso
913 Notation Reference:
914 @ref{Page breaking},
915 @ref{Optimal page breaking},
916 @ref{Optimal page turning},
917 @ref{Minimal page breaking},
918 @ref{One-line page breaking}.
919
920 Installed Files:
921 @file{ly/paper-defaults-init.ly}.
922
923
924 @node \paper variables for page numbering
925 @unnumberedsubsubsec @code{\paper} variables for page numbering
926
927 Default values not listed here are defined in
928 @file{ly/paper-defaults-init.ly}
929
930 @table @code
931
932 @cindex page numbers, auto-numbering
933 @item auto-first-page-number
934 @funindex auto-first-page-number
935
936 The page breaking algorithm is affected by the first page number
937 being odd or even.  If set to true, the page breaking algorithm
938 will decide whether to start with an odd or even number.  This
939 will result in the first page number remaining as is or being
940 increased by one.  Default: @code{#f}.
941
942 @cindex page numbers, specify the first
943 @item first-page-number
944 @funindex first-page-number
945
946 The value of the page number on the first page.
947
948 @item print-first-page-number
949 @funindex print-first-page-number
950
951 If set to true, a page number is printed on the first page.
952
953 @cindex page numbers, suppress
954 @item print-page-number
955 @funindex print-page-number
956
957 If set to false, page numbers are not printed.
958
959 @end table
960
961 @seealso
962 Installed Files:
963 @file{ly/paper-defaults-init.ly}.
964
965 @knownissues
966 Odd page numbers are always on the right.  If you want the
967 music to start on page 1 there must be a blank page on the back
968 of the cover page so that page 1 is on the right hand side.
969
970
971 @node Miscellaneous \paper variables
972 @unnumberedsubsubsec Miscellaneous @code{\paper} variables
973
974 @table @code
975
976 @item page-spacing-weight
977 @funindex page-spacing-weight
978
979 The relative importance of page (vertical) spacing and line
980 (horizontal) spacing.  High values will make page spacing more
981 important.  Default: @code{10}.
982
983 @item print-all-headers
984 @funindex print-all-headers
985
986 If set to true, this will print all headers for each @code{\score}
987 in the output.  Normally only the @code{piece} and @code{opus}
988 header variables are printed.  Default: @code{#f}.
989
990 @item system-separator-markup
991 @funindex system-separator-markup
992
993 A markup object that is inserted between systems, often used for
994 orchestral scores.  Default: unset.  The @code{\slashSeparator}
995 markup, defined in @file{ly/titling-init.ly}, is provided as a
996 sensible default, for example:
997
998 @lilypond[quote,verbatim,noragged-right,line-width=30\mm]
999 #(set-default-paper-size "a8")
1000
1001 \book {
1002   \paper {
1003     system-separator-markup = \slashSeparator
1004   }
1005   \header {
1006     tagline = ##f
1007   }
1008   \score {
1009     \relative c'' { c1 \break c1 \break c1 }
1010   }
1011 }
1012 @end lilypond
1013
1014 @end table
1015
1016 @seealso
1017 Installed Files:
1018 @file{ly/titling-init.ly}.
1019
1020 Snippets:
1021 @rlsr{Spacing}.
1022
1023 @knownissues
1024 The default page header puts the page number and the @code{instrument}
1025 field from the @code{\header} block on a line.
1026
1027
1028 @node Score layout
1029 @section Score layout
1030
1031 This section discusses score layout options for the @code{\layout}
1032 block.
1033
1034 @menu
1035 * The \layout block::
1036 * Setting the staff size::
1037 @end menu
1038
1039
1040 @node The \layout block
1041 @subsection The @code{\layout} block
1042
1043 @funindex \layout
1044
1045 While the @code{\paper} block contains settings that relate to the
1046 page formatting of the whole document, the @code{\layout} block
1047 contains settings for score-specific layout.  To set score layout
1048 options globally, enter them in a toplevel @code{\layout} block.
1049 To set layout options for an individual score, enter them in a
1050 @code{\layout} block inside the @code{\score} block, after the
1051 music.  Settings that can appear in a @code{\layout} block
1052 include:
1053
1054 @itemize
1055 @item the @code{layout-set-staff-size} scheme function,
1056 @item context modifications in @code{\context} blocks, and
1057 @item @code{\paper} variables that affect score layout.
1058 @end itemize
1059
1060 The @code{layout-set-staff-size} function is discussed in the next
1061 section, @ref{Setting the staff size}.  Context modifications are
1062 discussed in a separate chapter; see
1063 @ref{Modifying context plug-ins} and
1064 @ref{Changing context default settings}.  The @code{\paper}
1065 variables that can appear in a @code{\layout} block are:
1066
1067 @itemize
1068
1069 @item
1070 @code{line-width}, @code{ragged-right} and @code{ragged-last}
1071 (see @ref{\paper variables for widths and margins})
1072
1073 @item
1074 @code{indent} and @code{short-indent}
1075 (see @ref{\paper variables for shifts and indents})
1076
1077 @item
1078 @code{system-count}
1079 (see @ref{\paper variables for line breaking})
1080
1081 @end itemize
1082
1083 Here is an example @code{\layout} block:
1084
1085 @example
1086 \layout @{
1087   indent = 2\cm
1088   \context @{
1089     \StaffGroup
1090     \override StaffGrouper #'staff-staff-spacing #'basic-distance = #8
1091   @}
1092   \context @{
1093     \Voice
1094     \override TextScript #'padding = #1
1095     \override Glissando #'thickness = #3
1096   @}
1097 @}
1098 @end example
1099
1100 Multiple @code{\layout} blocks can be entered as toplevel expressions.
1101 This can, for example, be useful if different settings are stored in
1102 separate files and included optionally.  Internally, when
1103 a @code{\layout} block is evaluated, a copy of the current
1104 @code{\layout} configuration is made, then any changes defined within
1105 the block are applied and the result is saved as the new current
1106 configuration.  From the user's perspective the @code{\layout} blocks
1107 are combined, but in conflicting situations (when the same property
1108 is changed in different blocks) the later definitions take precedence.
1109
1110 For example, if this block:
1111
1112 @example
1113 \layout @{
1114   \context @{
1115     \Voice
1116     \override TextScript #'color = #magenta
1117     \override Glissando #'thickness = #1.5
1118   @}
1119 @}
1120 @end example
1121
1122 is placed after the one from the preceding example the @code{'padding}
1123 and @code{'color} overrides for @code{TextScript} are combined, but
1124 the later @code{'thickness} override for @code{Glissando} replaces
1125 (or hides) the earlier one.
1126
1127 @code{\layout} blocks may be assigned to variables for reuse later,
1128 but the way this works is slightly but significantly different from
1129 writing them literally.
1130
1131 If a variable is defined like this:
1132
1133 @example
1134 layoutVariable = \layout @{
1135   \context @{
1136     \Voice
1137     \override NoteHead #'font-size = #4
1138   @}
1139 @}
1140 @end example
1141
1142 it will hold the current @code{\layout} configuration with the
1143 @code{NoteHead #'font-size} override added, but this combination
1144 is @emph{not} saved as the new current configuration.  Be aware
1145 that the @q{current configuration} is read when the variable is
1146 defined and not when it is used, so the content of the variable
1147 is dependent on its position in the source.
1148
1149 The variable can then be used inside another @code{\layout} block,
1150 for example:
1151
1152 @example
1153 \layout @{
1154   \layoutVariable
1155   \context @{
1156     \Voice
1157     \override NoteHead #'color = #red
1158   @}
1159 @}
1160 @end example
1161
1162 A @code{\layout} block containing a variable, as in the example above,
1163 does @emph{not} copy the current configuration but instead uses the
1164 content of @code{\layoutVariable} as the base configuration for the
1165 further additions.  This means that any changes defined between the
1166 definition and the use of the variable are lost.
1167
1168 If @code{layoutVariable} is defined (or @code{\include}d) immediately
1169 before being used, its content is just the current configuration plus
1170 the overrides defined within it.  So in the example above showing the
1171 use of @code{\layoutVariable} the final @code{\layout} block would
1172 consist of:
1173
1174 @example
1175   TextScript #'padding = #1
1176   TextScript #'color = #magenta
1177   Glissando #'thickness = #1.5
1178   NoteHead #' font-size = #4
1179   NoteHead #' color = #red
1180 @end example
1181
1182 plus the @code{indent} and the @code{StaffGrouper} overrides.
1183
1184 But if the variable had already been defined before the first
1185 @code{\layout} block the current configuration would now contain
1186 only
1187
1188 @example
1189   NoteHead #' font-size= #4 % (written in the variable definition)
1190   NoteHead #' color = #red % (added after the use of the variable)
1191 @end example
1192
1193 If carefully planned, @code{\layout} variables can be a valuable tool
1194 to structure the layout design of sources, and also to reset the
1195 @code{\layout} configuration to a known state.
1196
1197 @seealso
1198 Notation Reference:
1199 @ref{Changing context default settings}.
1200
1201 Snippets:
1202 @rlsr{Spacing}.
1203
1204
1205 @node Setting the staff size
1206 @subsection Setting the staff size
1207
1208 @cindex font size, setting
1209 @cindex staff size, setting
1210 @funindex layout file
1211
1212 The default @strong{staff size} is set to 20 points.
1213 This may be changed in two ways:
1214
1215 To set the staff size globally for all scores in a file (or
1216 in a @code{book} block, to be precise), use @code{set-global-staff-size}.
1217
1218 @example
1219 #(set-global-staff-size 14)
1220 @end example
1221
1222 @noindent
1223 This sets the global default size to 14pt staff height and scales all
1224 fonts accordingly.
1225
1226 To set the staff size individually for each score, use
1227 @example
1228 \score@{
1229   @dots{}
1230   \layout @{
1231     #(layout-set-staff-size 15)
1232   @}
1233 @}
1234 @end example
1235
1236 The Feta font provides musical symbols at eight different
1237 sizes.  Each font is tuned for a different staff size: at a smaller size
1238 the font becomes heavier, to match the relatively heavier staff lines.
1239 The recommended font sizes are listed in the following table:
1240
1241 @quotation
1242 @multitable @columnfractions .15 .2 .22 .2
1243
1244 @item @b{font name}
1245 @tab @b{staff height (pt)}
1246 @tab @b{staff height (mm)}
1247 @tab @b{use}
1248
1249 @item feta11
1250 @tab 11.22
1251 @tab 3.9
1252 @tab pocket scores
1253
1254 @item feta13
1255 @tab 12.60
1256 @tab 4.4
1257 @tab
1258
1259 @item feta14
1260 @tab 14.14
1261 @tab 5.0
1262 @tab
1263
1264 @item feta16
1265 @tab 15.87
1266 @tab 5.6
1267 @tab
1268
1269 @item feta18
1270 @tab 17.82
1271 @tab 6.3
1272 @tab song books
1273
1274 @item feta20
1275 @tab 20
1276 @tab 7.0
1277 @tab standard parts
1278
1279 @item feta23
1280 @tab 22.45
1281 @tab 7.9
1282 @tab
1283
1284 @item feta26
1285 @tab 25.2
1286 @tab 8.9
1287 @tab
1288 @c modern rental material?
1289
1290 @end multitable
1291 @end quotation
1292
1293 These fonts are available in any sizes.  The context property
1294 @code{fontSize} and the layout property @code{staff-space} (in
1295 @rinternals{StaffSymbol}) can be used to tune the size for individual
1296 staves.  The sizes of individual staves are relative to the global size.
1297
1298 @seealso
1299 Notation Reference:
1300 @ref{Selecting notation font size}.
1301
1302 Snippets:
1303 @rlsr{Spacing}.
1304
1305 @knownissues
1306 @code{layout-set-staff-size} does not change the distance between the
1307 staff lines.
1308
1309
1310 @node Breaks
1311 @section Breaks
1312
1313 @menu
1314 * Line breaking::
1315 * Page breaking::
1316 * Optimal page breaking::
1317 * Optimal page turning::
1318 * Minimal page breaking::
1319 * One-line page breaking::
1320 * Explicit breaks::
1321 * Using an extra voice for breaks::
1322 @end menu
1323
1324
1325 @node Line breaking
1326 @subsection Line breaking
1327
1328 @cindex line breaks
1329 @cindex breaking lines
1330
1331 Line breaks are normally determined automatically.  They are chosen
1332 so that lines look neither cramped nor loose, and consecutive
1333 lines have similar density.
1334
1335 To manually force a line break at a bar line, use the
1336 @code{\break} command:
1337
1338 @lilypond[quote,ragged-right,relative=2,verbatim]
1339 c4 c c c | \break
1340 c4 c c c |
1341 @end lilypond
1342
1343 By default, a @code{\break} in the middle of a measure is ignored,
1344 and a warning is printed.  To force a line break in the middle of
1345 a measure, add an invisible bar line with @w{@samp{\bar ""}}:
1346
1347 @lilypond[quote,ragged-right,relative=2,verbatim]
1348 c4 c c
1349 \bar "" \break
1350 c |
1351 c4 c c c |
1352 @end lilypond
1353
1354 A @code{\break} occurring at a bar line is also ignored if the
1355 previous measure ends in the middle of a note, such as when a
1356 tuplet begins and ends in different measures.  To allow
1357 @code{\break} commands to work in these situations, remove the
1358 @code{Forbid_line_break_engraver} from the @code{Voice} context.
1359 Note that manually forced line breaks have to be added in parallel
1360 with the music:
1361
1362 @lilypond[quote,ragged-right,verbatim]
1363 \new Voice \with {
1364   \remove "Forbid_line_break_engraver"
1365 } \relative c'' {
1366   <<
1367     { c2. \times 2/3 { c4 c c } c2. | }
1368     { s1 | \break s1 | }
1369   >>
1370 }
1371 @end lilypond
1372
1373 Similarly, line breaks are normally forbidden when beams cross bar
1374 lines.  This behavior can be changed by setting
1375 @code{\override Beam #'breakable = ##t}:
1376
1377 @lilypond[quote,ragged-right,relative=2,verbatim]
1378 \override Beam #'breakable = ##t
1379 c2. c8[ c | \break
1380 c8 c] c2. |
1381 @end lilypond
1382
1383 The @code{\noBreak} command forbids a line break at the bar line
1384 where it is inserted.
1385
1386 The most basic settings influencing line spacing are @code{indent}
1387 and @code{line-width}.  They are set in the @code{\layout} block.
1388 They control the indentation of the first line of music, and the
1389 lengths of the lines.
1390
1391 If @code{ragged-right} is set to true in the @code{\layout} block,
1392 then systems end at their natural horizontal length, instead of
1393 being spread horizontally to fill the whole line.  This is useful
1394 for short fragments, and for checking how tight the natural
1395 spacing is.
1396
1397 @c TODO Check and add para on default for ragged-right
1398
1399 The option @code{ragged-last} is similar to @code{ragged-right},
1400 but affects only the last line of the piece.
1401
1402 @example
1403 \layout @{
1404   indent = 0\mm
1405   line-width = 150\mm
1406   ragged-last = ##t
1407 @}
1408 @end example
1409
1410
1411
1412 @cindex regular line breaks
1413 @cindex four bar music.
1414
1415 For line breaks at regular intervals use @code{\break} separated by
1416 skips and repeated with @code{\repeat}.  For example, this would
1417 cause the following 28 measures (assuming 4/4 time) to be broken
1418 every 4 measures, and only there:
1419
1420 @example
1421 <<
1422   \repeat unfold 7 @{
1423     s1 \noBreak s1 \noBreak
1424     s1 \noBreak s1 \break
1425   @}
1426   @{ @var{the actual music@dots{}} @}
1427 >>
1428 @end example
1429
1430 @c TODO Check this
1431 A linebreaking configuration can be saved as a @file{.ly} file
1432 automatically.  This allows vertical alignments to be stretched to
1433 fit pages in a second formatting run.  This is fairly new and
1434 complicated.  More details are available in
1435 @rlsr{Spacing}.
1436
1437
1438 @predefined
1439 @funindex \break
1440 @code{\break},
1441 @funindex \noBreak
1442 @code{\noBreak}.
1443 @endpredefined
1444
1445 @seealso
1446 Notation Reference:
1447 @ref{\paper variables for line breaking}.
1448
1449 Snippets:
1450 @rlsr{Spacing}.
1451
1452 Internals Reference:
1453 @rinternals{LineBreakEvent}.
1454
1455
1456 @node Page breaking
1457 @subsection Page breaking
1458
1459 The default page breaking may be overridden by inserting
1460 @code{\pageBreak} or @code{\noPageBreak} commands.  These commands are
1461 analogous to @code{\break} and @code{\noBreak}.  They should be
1462 inserted at a bar line.  These commands force and forbid a page-break
1463 from happening.  Of course, the @code{\pageBreak} command also forces
1464 a line break.
1465
1466 The @code{\pageBreak} and @code{\noPageBreak} commands may also be
1467 inserted at top-level, between scores and top-level markups.
1468
1469 There are also analogous settings to @code{ragged-right} and
1470 @code{ragged-last} which have the same effect on vertical spacing:
1471 @code{ragged-bottom} and @code{ragged-last-bottom}.  If set to
1472 @code{#t} the systems on all pages or just the last page
1473 respectively will not be justified vertically.  See
1474 @ref{Fixed vertical spacing \paper variables}.
1475
1476 Page breaks are computed by the @code{page-breaking} function.  LilyPond
1477 provides three algorithms for computing page breaks,
1478 @code{ly:optimal-breaking}, @code{ly:page-turn-breaking} and
1479 @code{ly:minimal-breaking}.  The default is @code{ly:optimal-breaking},
1480 but the value can be changed in the @code{\paper} block:
1481
1482 @example
1483 \paper @{
1484   page-breaking = #ly:page-turn-breaking
1485 @}
1486 @end example
1487
1488 @funindex \bookpart
1489
1490 When a book has many scores and pages, the page breaking problem may be
1491 difficult to solve, requiring large processing time and memory.  To ease
1492 the page breaking process, @code{\bookpart} blocks are used to divide
1493 the book into several parts: the page breaking occurs separately on each
1494 part.  Different page breaking functions may also be used in different
1495 book parts.
1496
1497 @example
1498 \bookpart @{
1499   \header @{
1500     subtitle = "Preface"
1501   @}
1502   \paper @{
1503      %% In a part consisting mostly of text,
1504      %% ly:minimal-breaking may be preferred
1505      page-breaking = #ly:minimal-breaking
1506   @}
1507   \markup @{ @dots{} @}
1508   @dots{}
1509 @}
1510 \bookpart @{
1511   %% In this part, consisting of music, the default optimal
1512   %% page breaking function is used.
1513   \header @{
1514     subtitle = "First movement"
1515   @}
1516   \score @{ @dots{} @}
1517   @dots{}
1518 @}
1519 @end example
1520
1521
1522 @predefined
1523 @funindex \pageBreak
1524 @code{\pageBreak},
1525 @funindex \noPageBreak
1526 @code{\noPageBreak}.
1527 @endpredefined
1528
1529 @seealso
1530 Notation Reference:
1531 @ref{\paper variables for page breaking}.
1532
1533 Snippets:
1534 @rlsr{Spacing}.
1535
1536
1537 @node Optimal page breaking
1538 @subsection Optimal page breaking
1539
1540 @funindex ly:optimal-breaking
1541
1542 The @code{ly:optimal-breaking} function is LilyPond's default method of
1543 determining page breaks.  It attempts to find a page breaking that minimizes
1544 cramping and stretching, both horizontally and vertically.  Unlike
1545 @code{ly:page-turn-breaking}, it has no concept of page turns.
1546
1547 @seealso
1548 Snippets:
1549 @rlsr{Spacing}.
1550
1551
1552 @node Optimal page turning
1553 @subsection Optimal page turning
1554
1555 @funindex ly:page-turn-breaking
1556
1557 Often it is necessary to find a page breaking configuration so that there is
1558 a rest at the end of every second page.  This way, the musician can turn the
1559 page without having to miss notes.  The @code{ly:page-turn-breaking} function
1560 attempts to find a page breaking minimizing cramping and stretching, but with
1561 the additional restriction that it is only allowed to introduce page turns
1562 in specified places.
1563
1564 There are two steps to using this page breaking function.  First, you
1565 must enable it in the @code{\paper} block, as explained in @ref{Page
1566 breaking}.  Then you must tell the function where you would like to allow
1567 page breaks.
1568
1569 There are two ways to achieve the second step.  First, you can specify each
1570 potential page turn manually, by inserting @code{\allowPageTurn} into your
1571 input file at the appropriate places.
1572
1573 If this is too tedious, you can add a @code{Page_turn_engraver} to a Staff or
1574 Voice context.  The @code{Page_turn_engraver} will scan the context for
1575 sections without notes (note that it does not scan for rests; it scans for
1576 the absence of notes.  This is so that single-staff polyphony with rests in one
1577 of the parts does not throw off the @code{Page_turn_engraver}).  When it finds
1578 a sufficiently long section without notes, the @code{Page_turn_engraver} will
1579 insert an @code{\allowPageTurn} at the final bar line in that section, unless
1580 there is a @q{special} bar line (such as a double bar), in which case the
1581 @code{\allowPageTurn} will be inserted at the final @q{special} bar line in
1582 the section.
1583
1584 @funindex minimumPageTurnLength
1585 The @code{Page_turn_engraver} reads the context property
1586 @code{minimumPageTurnLength} to determine how long a note-free section must
1587 be before a page turn is considered.  The default value for
1588 @code{minimumPageTurnLength} is @code{(ly:make-moment 1 1)}.  If you want
1589 to disable page turns, you can set it to something very large.
1590
1591 @example
1592 \new Staff \with @{ \consists "Page_turn_engraver" @}
1593 @{
1594   a4 b c d |
1595   R1 | % a page turn will be allowed here
1596   a4 b c d |
1597   \set Staff.minimumPageTurnLength = #(ly:make-moment 5 2)
1598   R1 | % a page turn will not be allowed here
1599   a4 b r2 |
1600   R1*2 | % a page turn will be allowed here
1601   a1
1602 @}
1603 @end example
1604
1605 @funindex minimumRepeatLengthForPageTurn
1606 The @code{Page_turn_engraver} detects volta repeats.  It will only allow a page
1607 turn during the repeat if there is enough time at the beginning and end of the
1608 repeat to turn the page back.  The @code{Page_turn_engraver} can also disable
1609 page turns if the repeat is very short.  If you set the context property
1610 @code{minimumRepeatLengthForPageTurn} then the @code{Page_turn_engraver} will
1611 only allow turns in repeats whose duration is longer than this value.
1612
1613 The page turning commands, @code{\pageTurn}, @code{\noPageTurn} and
1614 @code{\allowPageTurn}, may also be used at top-level, between scores and
1615 top-level markups.
1616
1617 @predefined
1618 @funindex \pageTurn
1619 @code{\pageTurn},
1620 @funindex \noPageTurn
1621 @code{\noPageTurn},
1622 @funindex \allowPageTurn
1623 @code{\allowPageTurn}.
1624 @endpredefined
1625
1626 @seealso
1627 Snippets:
1628 @rlsr{Spacing}.
1629
1630 @knownissues
1631 There should only be one @code{Page_turn_engraver} in a score.  If there is more
1632 than one, they will interfere with each other.
1633
1634
1635 @node Minimal page breaking
1636 @subsection Minimal page breaking
1637
1638 @funindex ly:minimal-breaking
1639
1640 The @code{ly:minimal-breaking} function performs minimal computations to
1641 calculate the page breaking: it fills a page with as many systems as
1642 possible before moving to the next one.  Thus, it may be preferred for
1643 scores with many pages, where the other page breaking functions could be
1644 too slow or memory demanding, or a lot of texts.  It is enabled using:
1645
1646 @example
1647 \paper @{
1648   page-breaking = #ly:minimal-breaking
1649 @}
1650 @end example
1651
1652 @seealso
1653 Snippets:
1654 @rlsr{Spacing}.
1655
1656 @node One-line page breaking
1657 @subsection One-line page breaking
1658
1659 @funindex ly:one-line-breaking
1660
1661 The @code{ly:one-line-breaking} function is a special-purpose
1662 page breaking algorithm that puts each score on its own page,
1663 and on a single line.  This page breaking function does not
1664 typeset titles or margins; only the score will be displayed.
1665
1666 The page width will be adjusted so that
1667 the longest score fits on one line.  In particular,
1668 @code{paper-width}, @code{line-width} and @code{indent}
1669 variables in the @code{\paper} block will be ignored, although
1670 @code{left-margin} and @code{right-margin} will
1671 still be honored.  The height of the page will
1672 be left unmodified.
1673
1674 @node Explicit breaks
1675 @subsection Explicit breaks
1676
1677 Lily sometimes rejects explicit @code{\break} and @code{\pageBreak}
1678 commands.  There are two commands to override this behavior:
1679
1680 @example
1681 \override NonMusicalPaperColumn #'line-break-permission = ##f
1682 \override NonMusicalPaperColumn #'page-break-permission = ##f
1683 @end example
1684
1685 When @code{line-break-permission} is overridden to false, Lily will insert
1686 line breaks at explicit @code{\break} commands and nowhere else.  When
1687 @code{page-break-permission} is overridden to false, Lily will insert
1688 page breaks at explicit @code{\pageBreak} commands and nowhere else.
1689
1690 @lilypond[quote,verbatim]
1691 \paper {
1692   indent = #0
1693   ragged-right = ##t
1694   ragged-bottom = ##t
1695 }
1696
1697 music = \relative c'' { c8 c c c }
1698
1699 \score {
1700   \new Staff {
1701     \repeat unfold 2 { \music } \break
1702     \repeat unfold 4 { \music } \break
1703     \repeat unfold 6 { \music } \break
1704     \repeat unfold 8 { \music } \pageBreak
1705     \repeat unfold 8 { \music } \break
1706     \repeat unfold 6 { \music } \break
1707     \repeat unfold 4 { \music } \break
1708     \repeat unfold 2 { \music }
1709   }
1710   \layout {
1711     \context {
1712       \Score
1713       \override NonMusicalPaperColumn #'line-break-permission = ##f
1714       \override NonMusicalPaperColumn #'page-break-permission = ##f
1715     }
1716   }
1717 }
1718 @end lilypond
1719
1720 @seealso
1721 Snippets:
1722 @rlsr{Spacing}.
1723
1724
1725 @node Using an extra voice for breaks
1726 @subsection Using an extra voice for breaks
1727
1728 Line- and page-breaking information usually appears within note entry directly.
1729
1730 @example
1731 music = \relative c'' @{ c4 c c c @}
1732
1733 \score @{
1734   \new Staff @{
1735     \repeat unfold 2 @{ \music @} \break
1736     \repeat unfold 3 @{ \music @}
1737   @}
1738 @}
1739 @end example
1740
1741 This makes @code{\break} and @code{\pageBreak} commands easy to enter but mixes
1742 music entry with information that specifies how music should lay out
1743 on the page.  You can keep music entry and line- and page-breaking
1744 information in two separate places by introducing an extra voice to
1745 contain the  breaks.  This extra voice
1746 contains only skips together with @code{\break}, @code{pageBreak} and other
1747 breaking layout information.
1748
1749 @lilypond[quote,verbatim]
1750 music = \relative c'' { c4 c c c }
1751
1752 \score {
1753   \new Staff <<
1754     \new Voice {
1755       s1 * 2 \break
1756       s1 * 3 \break
1757       s1 * 6 \break
1758       s1 * 5 \break
1759     }
1760     \new Voice {
1761       \repeat unfold 2 { \music }
1762       \repeat unfold 3 { \music }
1763       \repeat unfold 6 { \music }
1764       \repeat unfold 5 { \music }
1765     }
1766   >>
1767 }
1768 @end lilypond
1769
1770 This pattern becomes especially helpful when overriding
1771 @code{line-break-system-details} and the other useful but long properties of
1772 @code{NonMusicalPaperColumnGrob}, as explained in @ref{Vertical spacing}.
1773
1774 @lilypond[quote,verbatim]
1775 music = \relative c'' { c4 c c c }
1776
1777 \score {
1778   \new Staff <<
1779     \new Voice {
1780       \overrideProperty "Score.NonMusicalPaperColumn"
1781         #'line-break-system-details #'((Y-offset . 0))
1782       s1 * 2 \break
1783
1784       \overrideProperty "Score.NonMusicalPaperColumn"
1785         #'line-break-system-details #'((Y-offset . 35))
1786       s1 * 3 \break
1787
1788       \overrideProperty "Score.NonMusicalPaperColumn"
1789         #'line-break-system-details #'((Y-offset . 70))
1790       s1 * 6 \break
1791
1792       \overrideProperty "Score.NonMusicalPaperColumn"
1793         #'line-break-system-details #'((Y-offset . 105))
1794       s1 * 5 \break
1795     }
1796     \new Voice {
1797       \repeat unfold 2 { \music }
1798       \repeat unfold 3 { \music }
1799       \repeat unfold 6 { \music }
1800       \repeat unfold 5 { \music }
1801     }
1802   >>
1803 }
1804 @end lilypond
1805
1806 @seealso
1807 Notation Reference:
1808 @ref{Vertical spacing}.
1809
1810 Snippets:
1811 @rlsr{Spacing}.
1812
1813
1814 @node Vertical spacing
1815 @section Vertical spacing
1816
1817 @cindex vertical spacing
1818 @cindex spacing, vertical
1819
1820 Vertical spacing is controlled by three things: the amount of
1821 space available (i.e., paper size and margins), the amount of
1822 space between systems, and the amount of space between
1823 staves inside a system.
1824
1825 @menu
1826 * Flexible vertical spacing within systems::
1827 * Explicit staff and system positioning::
1828 * Vertical collision avoidance::
1829 @end menu
1830
1831
1832 @node Flexible vertical spacing within systems
1833 @subsection Flexible vertical spacing within systems
1834
1835 @cindex distance between staves
1836 @cindex staff distance
1837 @cindex space between staves
1838 @cindex space inside systems
1839
1840 Three separate mechanisms control the flexible vertical spacing
1841 within systems, one for each of the following categories:
1842
1843 @itemize
1844
1845 @item
1846 @emph{ungrouped staves},
1847
1848 @item
1849 @emph{grouped staves} (staves within a staff-group such as
1850 @code{ChoirStaff}, etc.), and
1851
1852 @item
1853 @emph{non-staff lines} (such as @code{Lyrics}, @code{ChordNames},
1854 etc.).
1855
1856 @end itemize
1857
1858 @c TODO: Clarify this.  This almost implies that non-staff lines
1859 @c       have NO effect on the spacing between staves.  -mp
1860
1861 The height of each system is determined in two steps.  First, all
1862 of the staves are spaced according to the amount of space
1863 available.  Then, the non-staff lines are distributed between the
1864 staves.
1865
1866 Note that the spacing mechanisms discussed in this section only
1867 control the vertical spacing of staves and non-staff lines within
1868 individual systems.  The vertical spacing between separate
1869 systems, scores, markups, and margins is controlled by
1870 @code{\paper} variables, which are discussed in
1871 @ref{Flexible vertical spacing \paper variables}.
1872
1873 @menu
1874 * Within-system spacing properties::
1875 * Spacing of ungrouped staves::
1876 * Spacing of grouped staves::
1877 * Spacing of non-staff lines::
1878 @end menu
1879
1880
1881 @node Within-system spacing properties
1882 @unnumberedsubsubsec Within-system spacing properties
1883
1884 @funindex staff-affinity
1885 @funindex staffgroup-staff-spacing
1886 @funindex staff-staff-spacing
1887 @funindex nonstaff-unrelatedstaff-spacing
1888 @funindex nonstaff-relatedstaff-spacing
1889 @funindex nonstaff-nonstaff-spacing
1890 @funindex default-staff-staff-spacing
1891 @funindex minimum-Y-extent
1892 @funindex extra-offset
1893 @funindex self-alignment-X
1894 @funindex X-offset
1895 @funindex VerticalAxisGroup
1896
1897 The within-system vertical spacing mechanisms are controlled by
1898 two sets of grob properties.  The first set is associated with the
1899 @code{VerticalAxisGroup} grob, which is created by all staves and
1900 non-staff lines.  The second set is associated with the
1901 @code{StaffGrouper} grob, which can be created by staff-groups,
1902 but only if explicitly called.  These properties are described
1903 individually at the end of this section.
1904
1905 The names of these properties (except for @code{staff-affinity})
1906 follow the format @code{@var{item1}-@var{item2}-spacing}, where
1907 @code{@var{item1}} and @code{@var{item2}} are the items to be
1908 spaced.  Note that @code{@var{item2}} is not necessarily below
1909 @code{@var{item1}}; for example,
1910 @code{nonstaff-relatedstaff-spacing} will measure upwards from the
1911 non-staff line if @code{staff-affinity} is @code{UP}.
1912
1913 Each distance is measured between the @emph{reference points} of
1914 the two items.  The reference point for a staff is the vertical
1915 center of its @code{StaffSymbol} (i.e. the middle line if
1916 @code{line-count} is odd; the middle space if @code{line-count} is
1917 even).  The reference points for individual non-staff lines are
1918 given in the following table:
1919
1920 @multitable {Non-staff line} {Reference point}
1921 @headitem Non-staff line @tab Reference point
1922 @item @code{ChordNames}  @tab baseline
1923 @item @code{NoteNames}   @tab baseline
1924 @item @code{Lyrics}      @tab baseline
1925 @item @code{Dynamics}    @tab vertical center
1926 @item @code{FiguredBass} @tab highest point
1927 @item @code{FretBoards}  @tab top line
1928 @end multitable
1929
1930 In the following image, horizontal lines indicate the positions
1931 of these reference points:
1932
1933 @lilypond[quote,noragged-right,line-width=110\mm]
1934 #(define zero-space '((padding . -inf.0) (basic-distance . 0)))
1935
1936 alignToZero = \with {
1937   \override VerticalAxisGroup #'nonstaff-relatedstaff-spacing = #zero-space
1938   \override VerticalAxisGroup #'nonstaff-nonstaff-spacing = #zero-space
1939 }
1940 lowerCaseChords = \with {
1941   chordNameLowercaseMinor = ##t
1942 }
1943 staffAffinityDown = \with {
1944   \override VerticalAxisGroup #'staff-affinity = #DOWN
1945 }
1946 labelContext =
1947 #(define-music-function
1948      (parser location context)
1949      (string?)
1950    #{ s1*0^\markup { \typewriter #context } #})
1951
1952 \layout {
1953   \context { \Dynamics    \alignToZero }
1954   \context { \FiguredBass \alignToZero }
1955   \context { \Lyrics      \alignToZero }
1956   \context { \NoteNames   \alignToZero \staffAffinityDown }
1957   \context { \ChordNames  \alignToZero
1958                           \staffAffinityDown
1959                           \lowerCaseChords }
1960   \context { \FretBoards  \alignToZero \staffAffinityDown }
1961   \context { \Score
1962     \override BarLine #'stencil = ##f
1963     \override DynamicText #'self-alignment-X = #-1
1964     \override FretBoard #'X-offset = #1.75
1965     \override InstrumentName #'minimum-Y-extent = #'(-2 . 2)
1966     \override InstrumentName #'extra-offset = #'(0 . -0.5)
1967     \override TextScript #'minimum-Y-extent = #'(-2 . 3)
1968     \override TimeSignature #'stencil = ##f
1969   }
1970 }
1971
1972 %% These contexts have reference points at the baseline:
1973 %%   ChordNames, NoteNames, and Lyrics
1974 <<
1975   \new ChordNames { \chords { g1:m } }
1976   \new NoteNames { s1 | g1 | }
1977   \new RhythmicStaff {
1978     \set RhythmicStaff.instrumentName = #"baseline "
1979     \textLengthOn
1980     \labelContext "ChordNames " s1 |
1981     \labelContext "NoteNames "  s1 |
1982     \labelContext "Lyrics"     s1 |
1983   }
1984   \new Lyrics { \lyrics { \skip 1*2 | ghijk1 | } }
1985 >>
1986
1987 %% The reference point for Dynamics is its vertical center
1988 <<
1989   \new RhythmicStaff {
1990     \set RhythmicStaff.instrumentName = #"vertical center "
1991     \labelContext "Dynamics" s1*3
1992   }
1993   \new Dynamics { s1\mp s\fp }
1994 >>
1995
1996 %% The reference point for FiguredBass is its highest point
1997 <<
1998   \new RhythmicStaff {
1999     \set RhythmicStaff.instrumentName = #"highest point "
2000     \labelContext "FiguredBass" s1
2001   }
2002   \new FiguredBass { \figuremode { <6 5>1 } }
2003 >>
2004
2005 %% The reference point for FretBoards is the top line
2006 \include "predefined-guitar-fretboards.ly"
2007 <<
2008   \new FretBoards { \chordmode { e1 } }
2009   \new RhythmicStaff {
2010     \set RhythmicStaff.instrumentName = #"top line "
2011     \labelContext "FretBoards " s1
2012   }
2013 >>
2014 @end lilypond
2015
2016 Each of the vertical spacing grob properties (except
2017 @code{staff-affinity}) uses the same alist structure as the
2018 @code{\paper} spacing variables discussed in
2019 @ref{Flexible vertical spacing \paper variables}.  Specific methods
2020 for modifying alists are discussed in @ref{Modifying alists}.
2021 Grob properties should be adjusted with an @code{\override} inside
2022 a @code{\score} or @code{\layout} block, and not inside a
2023 @code{\paper} block.
2024
2025 The following example demonstrates the two ways these alists can
2026 be modified.  The first declaration updates one key-value
2027 individually, and the second completely re-defines the property:
2028
2029 @example
2030 \new Staff \with @{
2031   \override VerticalAxisGroup #'default-staff-staff-spacing
2032        #'basic-distance = #10
2033 @} @{ @dots{} @}
2034
2035 \new Staff \with @{
2036   \override VerticalAxisGroup #'default-staff-staff-spacing =
2037     #'((basic-distance . 10)
2038        (minimum-distance . 9)
2039        (padding . 1)
2040        (stretchability . 10))
2041 @} @{ @dots{} @}
2042 @end example
2043
2044 To change any spacing settings globally, put them in the
2045 @code{\layout} block:
2046
2047 @example
2048 \layout @{
2049   \context @{
2050     \Staff
2051     \override VerticalAxisGroup #'default-staff-staff-spacing
2052          #'basic-distance = #10
2053   @}
2054 @}
2055 @end example
2056
2057 Standard settings for the vertical spacing grob properties are
2058 listed in @rinternals{VerticalAxisGroup} and
2059 @rinternals{StaffGrouper}.  Default overrides for specific types
2060 of non-staff lines are listed in the relevant context descriptions
2061 in @rinternals{Contexts}.
2062
2063
2064 @subsubheading Properties of the @code{VerticalAxisGroup} grob
2065
2066 @code{VerticalAxisGroup} properties are typically adjusted with an
2067 @code{\override} at the @code{Staff} level (or equivalent).
2068
2069 @table @code
2070 @item staff-staff-spacing
2071
2072 Used to determine the distance between the current staff and the
2073 staff just below it in the same system, even if one or more
2074 non-staff lines (such as @code{Lyrics}) are placed between the two
2075 staves.  Does not apply to the bottom staff of a system.
2076
2077 Initially, the @code{staff-staff-spacing} of a
2078 @code{VerticalAxisGroup} is a Scheme function that applies the
2079 properties of the @code{StaffGrouper} if the staff is part of a
2080 group, or the @code{default-staff-staff-spacing} of the staff
2081 otherwise.  This allows staves to be spaced differently when they
2082 are grouped.  For uniform spacing regardless of grouping, this
2083 function may be replaced by a flexible-spacing alist, using the
2084 complete-redefinition form of override shown above.
2085
2086 @item default-staff-staff-spacing
2087 A flexible-spacing alist defining the @code{staff-staff-spacing} used for
2088 ungrouped staves, unless @code{staff-staff-spacing} has been explicitly
2089 set with an @code{\override}.
2090
2091 @item staff-affinity
2092 The direction of the staff to use for spacing the current
2093 non-staff line.  Choices are @code{UP}, @code{DOWN}, and
2094 @code{CENTER}.  If @code{CENTER}, the non-staff line will be
2095 placed equidistant between the two nearest staves on either side,
2096 unless collisions or other spacing constraints prevent this.
2097 Adjacent non-staff lines should have non-increasing
2098 @code{staff-affinity} from top to bottom, e.g. a non-staff line
2099 set to @code{UP} should not immediately follow one that is set to
2100 @code{DOWN}.  Non-staff lines at the top of a system should use
2101 @code{DOWN}; those at the bottom should use @code{UP}.  Setting
2102 @code{staff-affinity} for a staff causes it to be treated as a
2103 non-staff line.  Setting @code{staff-affinity} to @code{#f} causes
2104 a non-staff line to be treated as a staff.  Setting
2105 @code{staff-affinity} to @code{UP}, @code{CENTER}, or @code{DOWN}
2106 causes a staff to be spaced as a non-staff line.
2107
2108 @item nonstaff-relatedstaff-spacing
2109 The distance between the current non-staff line and the nearest
2110 staff in the direction of @code{staff-affinity}, if there are no
2111 non-staff lines between the two, and @code{staff-affinity} is
2112 either @code{UP} or @code{DOWN}.  If @code{staff-affinity} is
2113 @code{CENTER}, then @code{nonstaff-relatedstaff-spacing} is used
2114 for the nearest staves on @emph{both} sides, even if other
2115 non-staff lines appear between the current one and either of the
2116 staves.  This means that the placement of a non-staff line depends
2117 on both the surrounding staves and the surrounding non-staff lines.
2118 Setting the @code{stretchability} of one of these types of spacing to
2119 a small value will make that spacing dominate.  Setting the
2120 @code{stretchability} to a large value will make that spacing have
2121 little effect.
2122
2123 @item nonstaff-nonstaff-spacing
2124 The distance between the current non-staff line and the next
2125 non-staff line in the direction of @code{staff-affinity}, if both
2126 are on the same side of the related staff, and
2127 @code{staff-affinity} is either @code{UP} or @code{DOWN}.
2128
2129 @item nonstaff-unrelatedstaff-spacing
2130 The distance between the current non-staff line and the staff in
2131 the opposite direction from @code{staff-affinity}, if there are no
2132 other non-staff lines between the two, and @code{staff-affinity}
2133 is either @code{UP} or @code{DOWN}.  This can be used, for
2134 example, to require a minimum amount of padding between a
2135 @code{Lyrics} line and the staff to which it does not belong.
2136 @end table
2137
2138
2139 @subsubheading Properties of the @code{StaffGrouper} grob
2140
2141 @code{StaffGrouper} properties are typically adjusted with an
2142 @code{\override} at the @code{StaffGroup} level (or equivalent).
2143
2144 @table @code
2145 @item staff-staff-spacing
2146 The distance between consecutive staves within the current
2147 staff-group.  The @code{staff-staff-spacing} property of an
2148 individual staff's @code{VerticalAxisGroup} grob can be
2149 overriden with different spacing settings for that staff.
2150
2151 @item staffgroup-staff-spacing
2152 The distance between the last staff of the current staff-group and
2153 the staff just below it in the same system, even if one or more
2154 non-staff lines (such as @code{Lyrics}) exist between the two
2155 staves.  Does not apply to the bottom staff of a system.  The
2156 @code{staff-staff-spacing} property of an individual staff's
2157 @code{VerticalAxisGroup} grob can be overriden with different
2158 spacing settings for that staff.
2159 @end table
2160
2161 @seealso
2162 Notation Reference:
2163 @ref{Flexible vertical spacing \paper variables},
2164 @ref{Modifying alists}.
2165
2166 Installed Files:
2167 @file{ly/engraver-init.ly},
2168 @file{scm/define-grobs.scm}.
2169
2170 Internals Reference:
2171 @rinternals{Contexts},
2172 @rinternals{VerticalAxisGroup},
2173 @rinternals{StaffGrouper}.
2174
2175
2176 @node Spacing of ungrouped staves
2177 @unnumberedsubsubsec Spacing of ungrouped staves
2178
2179 @emph{Staves} (such as @code{Staff}, @code{DrumStaff},
2180 @code{TabStaff}, etc.) are contexts that can contain one or more
2181 voice contexts, but cannot contain any other staves.
2182
2183 The following properties affect the spacing of @emph{ungrouped}
2184 staves:
2185
2186 @itemize
2187 @item @code{VerticalAxisGroup} properties:
2188 @itemize
2189 @item @code{default-staff-staff-spacing}
2190 @item @code{staff-staff-spacing}
2191 @end itemize
2192 @end itemize
2193
2194 These grob properties are described individually above; see
2195 @ref{Within-system spacing properties}.
2196
2197 Additional properties are involved for staves that are part of a
2198 staff-group; see @ref{Spacing of grouped staves}.
2199
2200 The following example shows how the @code{default-staff-staff-spacing}
2201 property can affect the spacing of ungrouped staves.
2202 The same overrides applied to @code{staff-staff-spacing} would
2203 have the same effect, but would also apply in cases where the staves
2204 are combined in a group or groups.
2205
2206 @lilypond[verbatim,quote,staffsize=16]
2207 \layout {
2208   \context {
2209     \Staff
2210     \override VerticalAxisGroup #'default-staff-staff-spacing =
2211       #'((basic-distance . 8)
2212          (minimum-distance . 7)
2213          (padding . 1))
2214   }
2215 }
2216
2217 <<
2218   % The very low note here needs more room than 'basic-distance
2219   % can provide, so the distance between this staff and the next
2220   % is determined by 'padding.
2221   \new Staff { b,2 r | }
2222
2223   % Here, 'basic-distance provides enough room, and there is no
2224   % need to compress the space (towards 'minimum-distance) to make
2225   % room for anything else on the page, so the distance between
2226   % this staff and the next is determined by 'basic-distance.
2227   \new Staff { \clef bass g2 r | }
2228
2229   % By setting 'padding to a negative value, staves can be made to
2230   % collide.  The lowest acceptable value for 'basic-distance is 0.
2231   \new Staff \with {
2232     \override VerticalAxisGroup #'default-staff-staff-spacing =
2233       #'((basic-distance . 3.5)
2234          (padding . -10))
2235   } { \clef bass g2 r | }
2236   \new Staff { \clef bass g2 r | }
2237 >>
2238 @end lilypond
2239
2240 @seealso
2241 Installed Files:
2242 @file{scm/define-grobs.scm}.
2243
2244 Snippets:
2245 @rlsr{Spacing}.
2246
2247 Internals Reference:
2248 @rinternals{VerticalAxisGroup}.
2249
2250
2251 @node Spacing of grouped staves
2252 @unnumberedsubsubsec Spacing of grouped staves
2253
2254 In orchestral and other large scores, it is common to place staves
2255 in groups.  The space between groups is typically larger than the
2256 space between staves of the same group.
2257
2258 @emph{Staff-groups} (such as @code{StaffGroup}, @code{ChoirStaff},
2259 etc.) are contexts that can contain one or more staves
2260 simultaneously.
2261
2262 The following properties affect the spacing of staves inside
2263 staff-groups:
2264
2265 @itemize
2266 @item @code{VerticalAxisGroup} properties:
2267 @itemize
2268 @item @code{staff-staff-spacing}
2269 @end itemize
2270 @item @code{StaffGrouper} properties:
2271 @itemize
2272 @item @code{staff-staff-spacing}
2273 @item @code{staffgroup-staff-spacing}
2274 @end itemize
2275 @end itemize
2276
2277 These grob properties are described individually above; see
2278 @ref{Within-system spacing properties}.
2279
2280 The following example shows how properties of the
2281 @code{StaffGrouper} grob can affect the spacing of grouped staves:
2282
2283 @lilypond[verbatim,quote,staffsize=16]
2284 \layout {
2285   \context {
2286     \Score
2287     \override StaffGrouper #'staff-staff-spacing #'padding = #0
2288     \override StaffGrouper #'staff-staff-spacing #'basic-distance = #1
2289   }
2290 }
2291
2292 <<
2293   \new PianoStaff \with {
2294     \override StaffGrouper #'staffgroup-staff-spacing #'basic-distance = #20
2295   } <<
2296     \new Staff { c'1 }
2297     \new Staff { c'1 }
2298   >>
2299
2300   \new StaffGroup <<
2301     \new Staff { c'1 }
2302     \new Staff { c'1 }
2303   >>
2304 >>
2305 @end lilypond
2306
2307 @seealso
2308 Installed Files:
2309 @file{scm/define-grobs.scm}.
2310
2311 Snippets:
2312 @rlsr{Spacing}.
2313
2314 Internals Reference:
2315 @rinternals{VerticalAxisGroup},
2316 @rinternals{StaffGrouper}.
2317
2318
2319 @node Spacing of non-staff lines
2320 @unnumberedsubsubsec Spacing of non-staff lines
2321
2322 @emph{Non-staff lines} (such as @code{Lyrics}, @code{ChordNames},
2323 etc.) are contexts whose layout objects are engraved like staves
2324 (i.e. in horizontal lines within systems).  Specifically,
2325 non-staff lines are non-staff contexts that create the
2326 @code{VerticalAxisGroup} layout object.
2327
2328 The following properties affect the spacing of non-staff lines:
2329
2330 @itemize
2331 @item @code{VerticalAxisGroup} properties:
2332 @itemize
2333 @item @code{staff-affinity}
2334 @item @code{nonstaff-relatedstaff-spacing}
2335 @item @code{nonstaff-nonstaff-spacing}
2336 @item @code{nonstaff-unrelatedstaff-spacing}
2337 @end itemize
2338 @end itemize
2339
2340 These grob properties are described individually above; see
2341 @ref{Within-system spacing properties}.
2342
2343 The following example shows how the
2344 @code{nonstaff-nonstaff-spacing} property can affect the spacing
2345 of consecutive non-staff lines.  Here, by setting the
2346 @code{stretchability} key to a very high value, the lyrics are
2347 able to stretch much more than usual:
2348
2349 @lilypond[verbatim,quote,staffsize=16]
2350 \layout {
2351   \context {
2352     \Lyrics
2353     \override VerticalAxisGroup
2354       #'nonstaff-nonstaff-spacing #'stretchability = #1000
2355   }
2356 }
2357
2358 \new StaffGroup
2359 <<
2360   \new Staff \with {
2361     \override VerticalAxisGroup #'staff-staff-spacing = #'((basic-distance . 30))
2362   } { c'1 }
2363   \new Lyrics \with {
2364     \override VerticalAxisGroup #'staff-affinity = #UP
2365   } \lyricmode { up }
2366   \new Lyrics \with {
2367     \override VerticalAxisGroup #'staff-affinity = #CENTER
2368   } \lyricmode { center }
2369   \new Lyrics \with {
2370     \override VerticalAxisGroup #'staff-affinity = #DOWN
2371   } \lyricmode { down }
2372   \new Staff { c'1 }
2373 >>
2374 @end lilypond
2375
2376 @seealso
2377 Installed Files:
2378 @file{ly/engraver-init.ly},
2379 @file{scm/define-grobs.scm}.
2380
2381 Snippets:
2382 @rlsr{Spacing}.
2383
2384 @c @lsr{spacing,page-spacing.ly},
2385 @c @lsr{spacing,alignment-vertical-spacing.ly}.
2386
2387 Internals Reference:
2388 @rinternals{Contexts},
2389 @rinternals{VerticalAxisGroup}.
2390
2391
2392 @node Explicit staff and system positioning
2393 @subsection Explicit staff and system positioning
2394
2395 One way to understand the flexible vertical spacing mechanisms
2396 explained above is as a collection of settings that control the
2397 amount of vertical padding between staves and systems.
2398
2399 It is possible to approach vertical spacing in a different way
2400 using @code{NonMusicalPaperColumn #'line-break-system-details}.
2401 While the flexible vertical spacing mechanisms specify vertical
2402 padding, @code{NonMusicalPaperColumn #'line-break-system-details}
2403 can specify exact vertical positions on the page.
2404
2405 @code{NonMusicalPaperColumn #'line-break-system-details} accepts
2406 an associative list of three different settings:
2407
2408 @itemize
2409 @item @code{X-offset}
2410 @item @code{Y-offset}
2411 @item @code{alignment-distances}
2412 @end itemize
2413
2414 Grob overrides, including the overrides for @code{NonMusicalPaperColumn}
2415 below, can occur in any of three different places in an input file:
2416
2417 @itemize
2418 @item in the middle of note entry directly
2419 @item in a @code{\context} block
2420 @item in the @code{\with} block
2421 @end itemize
2422
2423 When we override @code{NonMusicalPaperColumn}, we use the usual
2424 @code{\override} command in @code{\context} blocks and in the
2425 @code{\with} block.  On the other hand, when we override
2426 @code{NonMusicalPaperColumn} in the middle of note entry,
2427 use the special @code{\overrideProperty} command.  Here are some
2428 example @code{NonMusicalPaperColumn} overrides with the special
2429 @code{\overrideProperty} command:
2430
2431 @example
2432 \overrideProperty NonMusicalPaperColumn
2433   #'line-break-system-details #'((X-offset . 20))
2434
2435 \overrideProperty NonMusicalPaperColumn
2436   #'line-break-system-details #'((Y-offset . 40))
2437
2438 \overrideProperty NonMusicalPaperColumn
2439   #'line-break-system-details #'((X-offset . 20)
2440                                  (Y-offset . 40))
2441
2442 \overrideProperty NonMusicalPaperColumn
2443   #'line-break-system-details #'((alignment-distances . (15)))
2444
2445 \overrideProperty NonMusicalPaperColumn
2446   #'line-break-system-details #'((X-offset . 20)
2447                                  (Y-offset . 40)
2448                                  (alignment-distances . (15)))
2449 @end example
2450
2451 To understand how each of these different settings work, we begin
2452 by looking at an example that includes no overrides at all.
2453
2454 @c \book { } is required in these examples to ensure the spacing
2455 @c overrides can be seen between systems. -np
2456
2457 @lilypond[verbatim,quote,staffsize=16]
2458 \header { tagline = ##f }
2459 \paper { left-margin = 0\mm }
2460 \book {
2461   \score {
2462     <<
2463       \new Staff <<
2464         \new Voice {
2465           s1*5 \break
2466           s1*5 \break
2467           s1*5 \break
2468         }
2469         \new Voice { \repeat unfold 15 { c'4 c' c' c' } }
2470       >>
2471       \new Staff {
2472         \repeat unfold 15 { d'4 d' d' d' }
2473       }
2474     >>
2475   }
2476 }
2477 @end lilypond
2478
2479 This score isolates line- and page-breaking information in a dedicated
2480 voice.  This technique of creating a breaks voice will help keep layout
2481 separate from music entry as our example becomes more complicated.
2482 See @ref{Using an extra voice for breaks}.
2483
2484 Explicit @code{\breaks} evenly divide the music into six measures per
2485 line.  Vertical spacing results from LilyPond's defaults.  To set
2486 the vertical startpoint of each system explicitly, we can set
2487 the @code{Y-offset} pair in the @code{line-break-system-details}
2488 attribute of the @code{NonMusicalPaperColumn} grob:
2489
2490 @lilypond[verbatim,quote,staffsize=16]
2491 \header { tagline = ##f }
2492 \paper { left-margin = 0\mm }
2493 \book {
2494   \score {
2495     <<
2496       \new Staff <<
2497         \new Voice {
2498           \overrideProperty #"Score.NonMusicalPaperColumn"
2499             #'line-break-system-details #'((Y-offset . 0))
2500           s1*5 \break
2501           \overrideProperty #"Score.NonMusicalPaperColumn"
2502             #'line-break-system-details #'((Y-offset . 40))
2503           s1*5 \break
2504           \overrideProperty #"Score.NonMusicalPaperColumn"
2505             #'line-break-system-details #'((Y-offset . 80))
2506           s1*5 \break
2507         }
2508         \new Voice { \repeat unfold 15 { c'4 c' c' c' } }
2509       >>
2510       \new Staff {
2511         \repeat unfold 15 { d'4 d' d' d' }
2512       }
2513     >>
2514   }
2515 }
2516 @end lilypond
2517
2518 Note that @code{line-break-system-details} takes an associative list of
2519 potentially many values, but that we set only one value here.  Note,
2520 too, that the @code{Y-offset} property here determines the exact vertical
2521 position on the page at which each new system will render.
2522
2523 Now that we have set the vertical startpoint of each system
2524 explicitly, we can also set the vertical distances between staves
2525 within each system manually.  We do this using the @code{alignment-distances}
2526 subproperty of @code{line-break-system-details}.
2527
2528 @lilypond[verbatim,quote,staffsize=16]
2529 \header { tagline = ##f }
2530 \paper { left-margin = 0\mm }
2531 \book {
2532   \score {
2533     <<
2534       \new Staff <<
2535         \new Voice {
2536           \overrideProperty #"Score.NonMusicalPaperColumn"
2537             #'line-break-system-details #'((Y-offset . 20)
2538                                            (alignment-distances . (15)))
2539           s1*5 \break
2540           \overrideProperty #"Score.NonMusicalPaperColumn"
2541             #'line-break-system-details #'((Y-offset . 60)
2542                                            (alignment-distances . (15)))
2543           s1*5 \break
2544           \overrideProperty #"Score.NonMusicalPaperColumn"
2545             #'line-break-system-details #'((Y-offset . 100)
2546                                            (alignment-distances . (15)))
2547           s1*5 \break
2548         }
2549         \new Voice { \repeat unfold 15 { c'4 c' c' c' } }
2550       >>
2551       \new Staff {
2552         \repeat unfold 15 { d'4 d' d' d' }
2553       }
2554     >>
2555   }
2556 }
2557 @end lilypond
2558
2559 Note that here we assign two different values to the
2560 @code{line-break-system-details} attribute of the
2561 @code{NonMusicalPaperColumn} grob.  Though the
2562 @code{line-break-system-details} attribute alist accepts many
2563 additional spacing parameters (including, for example, a corresponding
2564 @code{X-offset} pair), we need only set the @code{Y-offset} and
2565 @code{alignment-distances} pairs to control the vertical startpoint of
2566 every system and every staff.  Finally, note that @code{alignment-distances}
2567 specifies the vertical positioning of staves but not of staff groups.
2568
2569 @lilypond[verbatim,quote,staffsize=16]
2570 \header { tagline = ##f }
2571 \paper { left-margin = 0\mm }
2572 \book {
2573   \score {
2574     <<
2575       \new Staff <<
2576         \new Voice {
2577           \overrideProperty #"Score.NonMusicalPaperColumn"
2578             #'line-break-system-details #'((Y-offset . 0)
2579                                            (alignment-distances . (30 10)))
2580           s1*5 \break
2581           \overrideProperty #"Score.NonMusicalPaperColumn"
2582             #'line-break-system-details #'((Y-offset . 60)
2583                                            (alignment-distances . (10 10)))
2584           s1*5 \break
2585           \overrideProperty #"Score.NonMusicalPaperColumn"
2586             #'line-break-system-details #'((Y-offset . 100)
2587                                            (alignment-distances . (10 30)))
2588           s1*5 \break
2589         }
2590         \new Voice { \repeat unfold 15 { c'4 c' c' c' } }
2591       >>
2592       \new StaffGroup <<
2593         \new Staff { \repeat unfold 15 { d'4 d' d' d' } }
2594         \new Staff { \repeat unfold 15 { e'4 e' e' e' } }
2595       >>
2596     >>
2597   }
2598 }
2599 @end lilypond
2600
2601 Some points to consider:
2602
2603 @itemize
2604 @item When using @code{alignment-distances}, lyrics and other non-staff lines
2605 do not count as a staff.
2606
2607 @item The units of the numbers passed to @code{X-offset},
2608 @code{Y-offset} and @code{alignment-distances} are interpreted as multiples
2609 of the distance between adjacent staff lines.  Positive values move staves
2610 and lyrics up, negative values move staves and lyrics down.
2611
2612 @item Because the @code{NonMusicalPaperColumn #'line-break-system-details}
2613 settings given here allow the positioning of staves and systems anywhere
2614 on the page, it is possible to violate paper or margin boundaries or even
2615 to print staves or systems on top of one another.  Reasonable values
2616 passed to these different settings will avoid this.
2617 @end itemize
2618
2619 @seealso
2620 Snippets:
2621 @rlsr{Spacing}.
2622
2623
2624 @node Vertical collision avoidance
2625 @subsection Vertical collision avoidance
2626
2627 @funindex outside-staff-priority
2628 @funindex outside-staff-padding
2629 @funindex outside-staff-horizontal-padding
2630
2631 Intuitively, there are some objects in musical notation that belong
2632 to the staff and there are other objects that should be placed outside
2633 the staff.  Objects belonging outside the staff include things such as
2634 rehearsal marks, text and dynamic markings (from now on, these will
2635 be called outside-staff objects).  LilyPond's rule for the
2636 vertical placement of outside-staff objects is to place them as close
2637 to the staff as possible but not so close that they collide with
2638 another object.
2639
2640 LilyPond uses the @code{outside-staff-priority} property to determine
2641 whether a grob is an outside-staff object: if @code{outside-staff-priority}
2642 is a number, the grob is an outside-staff object.  In addition,
2643 @code{outside-staff-priority} tells LilyPond in which order the objects
2644 should be placed.
2645
2646 First, LilyPond places all the objects that do not belong outside
2647 the staff.  Then it sorts the outside-staff objects according to their
2648 @code{outside-staff-priority} (in increasing order).  One by one, LilyPond
2649 takes the outside-staff objects and places them so that they do
2650 not collide with any objects that have already been placed.  That
2651 is, if two outside-staff grobs are competing for the same space, the one
2652 with the lower @code{outside-staff-priority} will be placed closer to
2653 the staff.
2654
2655 @lilypond[quote,ragged-right,relative=2,verbatim]
2656 c4_"Text"\pp
2657 r2.
2658 \once \override TextScript #'outside-staff-priority = #1
2659 c4_"Text"\pp % this time the text will be closer to the staff
2660 r2.
2661 % by setting outside-staff-priority to a non-number,
2662 % we disable the automatic collision avoidance
2663 \once \override TextScript #'outside-staff-priority = ##f
2664 \once \override DynamicLineSpanner #'outside-staff-priority = ##f
2665 c4_"Text"\pp % now they will collide
2666 @end lilypond
2667
2668 The vertical padding between an outside-staff object and the
2669 previously-positioned grobs can be controlled with
2670 @code{outside-staff-padding}.
2671
2672 @lilypond[quote,ragged-right,relative=2,verbatim,staffsize=18]
2673 \once \override TextScript #'outside-staff-padding = #0
2674 a'^"This text is placed very close to the note"
2675 \once \override TextScript #'outside-staff-padding = #3
2676 c^"This text is padded away from the previous text"
2677 c^"This text is placed close to the previous text"
2678 @end lilypond
2679
2680
2681 By default, outside-staff objects are placed only to avoid
2682 a horizontal collision with previously-positioned grobs.  This
2683 can lead to situations in which objects are placed very close to each
2684 other horizontally.  The vertical spacing between staves can
2685 also be set so that outside staff objects are interleaved.
2686 Setting @code{outside-staff-horizontal-padding}
2687 causes an object to be offset vertically so that such a situation
2688 doesn't occur.
2689
2690 @lilypond[quote,ragged-right,relative=2,verbatim]
2691 % the markup is too close to the following note
2692 c4^"Text"
2693 c4
2694 c''2
2695 % setting outside-staff-horizontal-padding fixes this
2696 R1
2697 \once \override TextScript #'outside-staff-horizontal-padding = #1
2698 c,,4^"Text"
2699 c4
2700 c''2
2701 @end lilypond
2702
2703 @seealso
2704 Snippets:
2705 @rlsr{Spacing}.
2706
2707
2708 @node Horizontal spacing
2709 @section Horizontal spacing
2710
2711 @cindex horizontal spacing
2712 @cindex spacing, horizontal
2713
2714 @menu
2715 * Horizontal spacing overview::
2716 * New spacing area::
2717 * Changing horizontal spacing::
2718 * Line length::
2719 * Proportional notation::
2720 @end menu
2721
2722
2723 @node Horizontal spacing overview
2724 @subsection Horizontal spacing overview
2725
2726 The spacing engine translates differences in durations into stretchable
2727 distances (@q{springs}) of differing lengths.  Longer durations get
2728 more space, shorter durations get less.  The shortest durations get a
2729 fixed amount of space (which is controlled by
2730 @code{shortest-duration-space} in the @rinternals{SpacingSpanner}
2731 object).  The longer the duration, the more space it gets: doubling a
2732 duration adds a fixed amount (this amount is controlled by
2733 @code{spacing-increment}) of space to the note.
2734
2735 For example, the following piece contains lots of half, quarter, and
2736 8th notes; the eighth note is followed by 1 note head width (NHW).
2737 The quarter note is followed by 2 NHW, the half by 3 NHW, etc.
2738
2739 @lilypond[quote,verbatim,relative=1]
2740 c2 c4. c8 c4. c8 c4. c8 c8
2741 c8 c4 c4 c4
2742 @end lilypond
2743
2744 Normally, @code{spacing-increment} is set to 1.2 staff space, which is
2745 approximately the width of a note head, and
2746 @code{shortest-duration-space} is set to 2.0, meaning that the
2747 shortest note gets 2.4 staff space (2.0 times the
2748 @code{spacing-increment}) of horizontal space.  This space is counted
2749 from the left edge of the symbol, so the shortest notes are generally
2750 followed by one NHW of space.
2751
2752 If one would follow the above procedure exactly, then adding a single
2753 32nd note to a score that uses 8th and 16th notes, would widen up the
2754 entire score a lot.  The shortest note is no longer a 16th, but a 32nd,
2755 thus adding 1 NHW to every note.  To prevent this, the shortest
2756 duration for spacing is not the shortest note in the score, but rather
2757 the one which occurs most frequently.
2758
2759
2760 The most common shortest duration is determined as follows: in every
2761 measure, the shortest duration is determined.  The most common shortest
2762 duration is taken as the basis for the spacing, with the stipulation
2763 that this shortest duration should always be equal to or shorter than
2764 an 8th note.  The shortest duration is printed when you run
2765 @code{lilypond} with the @option{--verbose} option.
2766
2767 These durations may also be customized.  If you set the
2768 @code{common-shortest-duration} in @rinternals{SpacingSpanner}, then
2769 this sets the base duration for spacing.  The maximum duration for this
2770 base (normally an 8th), is set through @code{base-shortest-duration}.
2771
2772 @funindex common-shortest-duration
2773 @funindex base-shortest-duration
2774 @funindex stem-spacing-correction
2775 @funindex spacing
2776
2777 Notes that are even shorter than the common shortest note are
2778 followed by a space that is proportional to their duration relative to
2779 the common shortest note.  So if we were to add only a few 16th notes
2780 to the example above, they would be followed by half a NHW:
2781
2782 @lilypond[quote,verbatim,relative=2]
2783 c2 c4. c8 c4. c16[ c] c4. c8 c8 c8 c4 c4 c4
2784 @end lilypond
2785
2786
2787 In the @emph{Essay on automated music engraving}, it was explained
2788 that stem directions influence spacing (see
2789 @ressay{Optical spacing}).  This is controlled with the
2790 @code{stem-spacing-correction} property in the
2791 @rinternals{NoteSpacing}, object.  These are generated for every
2792 @rinternals{Voice} context.  The @code{StaffSpacing} object
2793 (generated in @rinternals{Staff} context) contains the same
2794 property for controlling the stem/bar line spacing.  The following
2795 example shows these corrections, once with default settings, and
2796 once with exaggerated corrections:
2797
2798 @lilypond[quote,ragged-right]
2799 {
2800   c'4 e''4 e'4 b'4 |
2801   b'4 e''4 b'4 e''4 |
2802   \override Staff.NoteSpacing #'stem-spacing-correction = #1.5
2803   \override Staff.StaffSpacing #'stem-spacing-correction = #1.5
2804   c'4 e''4 e'4 b'4 |
2805   b'4 e''4 b'4 e''4 |
2806 }
2807 @end lilypond
2808
2809 Proportional notation is supported; see @ref{Proportional notation}.
2810
2811 @seealso
2812 Essay on automated music engraving:
2813 @ressay{Optical spacing}.
2814
2815 Snippets:
2816 @rlsr{Spacing}.
2817
2818 Internals Reference:
2819 @rinternals{SpacingSpanner},
2820 @rinternals{NoteSpacing},
2821 @rinternals{StaffSpacing},
2822 @rinternals{NonMusicalPaperColumn}.
2823
2824 @knownissues
2825 There is no convenient mechanism to manually override spacing.  The
2826 following work-around may be used to insert extra space into a score,
2827 adjusting the padding value as necessary.
2828
2829 @example
2830  \override Score.NonMusicalPaperColumn #'padding = #10
2831 @end example
2832
2833 No work-around exists for decreasing the amount of space.
2834
2835
2836 @node New spacing area
2837 @subsection New spacing area
2838
2839 New sections with different spacing parameters can be started with
2840 @code{newSpacingSection}.  This is useful when there are
2841 sections with a different notions of long and short notes.
2842
2843 In the following example, the time signature change introduces a new
2844 section, and hence the 16ths notes are spaced wider.
2845
2846 @lilypond[relative=1,verbatim,quote]
2847 \time 2/4
2848 c4 c8 c
2849 c8 c c4 c16[ c c8] c4
2850 \newSpacingSection
2851 \time 4/16
2852 c16[ c c8]
2853 @end lilypond
2854
2855 The @code{\newSpacingSection} command creates a new
2856 @code{SpacingSpanner} object, and hence new @code{\override}s
2857 may be used in that location.
2858
2859 @seealso
2860 Snippets:
2861 @rlsr{Spacing}.
2862
2863 Internals Reference:
2864 @rinternals{SpacingSpanner}.
2865
2866
2867 @node Changing horizontal spacing
2868 @subsection Changing horizontal spacing
2869
2870 Horizontal spacing may be altered with the
2871 @code{base-shortest-duration} property.  Here
2872 we compare the same music; once without altering
2873 the property, and then altered.  Larger values
2874 of @code{ly:make-moment} will produce smaller
2875 music.  Note that @code{ly:make-moment} constructs
2876 a duration, so @code{1 4} is a longer duration
2877 than @code{1 16}.
2878
2879 @lilypond[verbatim,line-width=12\cm]
2880 \score {
2881   \relative c'' {
2882     g4 e e2 | f4 d d2 | c4 d e f | g4 g g2 |
2883     g4 e e2 | f4 d d2 | c4 e g g | c,1 |
2884     d4 d d d | d4 e f2 | e4 e e e | e4 f g2 |
2885     g4 e e2 | f4 d d2 | c4 e g g | c,1 |
2886   }
2887 }
2888 @end lilypond
2889
2890 @lilypond[verbatim,line-width=12\cm]
2891 \score {
2892   \relative c'' {
2893     g4 e e2 | f4 d d2 | c4 d e f | g4 g g2 |
2894     g4 e e2 | f4 d d2 | c4 e g g | c,1 |
2895     d4 d d d | d4 e f2 | e4 e e e | e4 f g2 |
2896     g4 e e2 | f4 d d2 | c4 e g g | c,1 |
2897   }
2898   \layout {
2899     \context {
2900       \Score
2901       \override SpacingSpanner
2902         #'base-shortest-duration = #(ly:make-moment 1 16)
2903     }
2904   }
2905 }
2906 @end lilypond
2907
2908
2909 @snippets
2910
2911 By default, spacing in tuplets depends on various non-duration
2912 factors (such as accidentals, clef changes, etc).  To disregard
2913 such symbols and force uniform equal-duration spacing, use
2914 @code{Score.SpacingSpanner #'uniform-stretching}.  This
2915 property can only be changed at the beginning of a score,
2916
2917 @lilypond[quote,ragged-right,verbatim]
2918 \score {
2919   <<
2920     \new Staff {
2921       \times 4/5 {
2922         c8 c8 c8 c8 c8
2923       }
2924       c8 c8 c8 c8
2925     }
2926     \new Staff {
2927       c8 c8 c8 c8
2928       \times 4/5 {
2929         c8 c8 c8 c8 c8
2930       }
2931     }
2932   >>
2933   \layout {
2934     \context {
2935       \Score
2936       \override SpacingSpanner #'uniform-stretching = ##t
2937     }
2938   }
2939 }
2940 @end lilypond
2941
2942 When @code{strict-note-spacing} is set, notes are spaced without
2943 regard for clefs, bar lines, and grace notes,
2944
2945 @lilypond[quote,ragged-right,relative=2,verbatim]
2946 \override Score.SpacingSpanner #'strict-note-spacing = ##t
2947 \new Staff { c8[ c \clef alto c \grace { c16[ c] } c8 c c]  c32[ c32] }
2948 @end lilypond
2949
2950 @seealso
2951 Snippets:
2952 @rlsr{Spacing}.
2953
2954
2955 @node Line length
2956 @subsection Line length
2957
2958 @cindex page breaks
2959 @cindex breaking pages
2960
2961 @funindex indent
2962 @funindex line-width
2963 @funindex ragged-right
2964 @funindex ragged-last
2965
2966 @c Although line-width can be set in \layout, it should be set in paper
2967 @c block, to get page layout right.
2968 @c Setting indent in \paper block makes not much sense, but it works.
2969
2970 @c Bit verbose and vague, use examples?
2971 The most basic settings influencing the spacing are @code{indent} and
2972 @code{line-width}.  They are set in the @code{\layout} block.  They
2973 control the indentation of the first line of music, and the lengths of
2974 the lines.
2975
2976 If @code{ragged-right} is set to true in the @code{\layout} block, then
2977 systems ends at their natural horizontal length, instead of being spread
2978 horizontally to fill the whole line.  This is useful for
2979 short fragments, and for checking how tight the natural spacing is.
2980 The normal default setting is false, but if the score has only one
2981 system the default value is true.
2982
2983 @cindex page layout
2984 @cindex vertical spacing
2985
2986 The option @code{ragged-last} is similar to @code{ragged-right}, but
2987 only affects the last line of the piece.  No restrictions are put on
2988 that line.  The result is similar to formatting text paragraphs.  In a
2989 paragraph, the last line simply takes its natural horizontal length.
2990 @c Note that for text there are several options for the last line.
2991 @c While Knuth TeX uses natural length, lead typesetters use the same
2992 @c stretch as the previous line.  eTeX uses \lastlinefit to
2993 @c interpolate between both these solutions.
2994
2995 @example
2996 \layout @{
2997   indent = #0
2998   line-width = #150
2999   ragged-last = ##t
3000 @}
3001 @end example
3002
3003 @seealso
3004 Snippets:
3005 @rlsr{Spacing}.
3006
3007
3008 @node Proportional notation
3009 @subsection Proportional notation
3010
3011 LilyPond supports proportional notation, a type of horizontal spacing
3012 in which each note consumes an amount of horizontal space exactly
3013 equivalent to its rhythmic duration.  This type of proportional spacing
3014 is comparable to horizontal spacing on top of graph paper.  Some late
3015 20th- and early 21st-century scores use proportional notation to
3016 clarify complex rhythmic relationships or to facilitate the placement
3017 of timelines or other graphics directly in the score.
3018
3019 LilyPond supports five different settings for proportional notation,
3020 which may be used together or alone:
3021
3022 @itemize
3023 @item @code{proportionalNotationDuration}
3024 @item @code{uniform-stretching}
3025 @item @code{strict-note-spacing}
3026 @item @code{\remove "Separating_line_group_engraver"}
3027 @item @code{\override PaperColumn #'used = ##t}
3028 @end itemize
3029
3030 In the examples that follow, we explore these five different
3031 proportional notation settings and examine how these settings interact.
3032
3033 We start with the following one-measure example, which uses classical
3034 spacing with ragged-right turned on.
3035
3036 @lilypond[quote,verbatim,ragged-right]
3037 \score {
3038   <<
3039     \new RhythmicStaff {
3040       c'2
3041       c'16 c'16 c'16 c'16
3042       \times 4/5 {
3043         c'16 c'16 c'16 c'16 c'16
3044       }
3045     }
3046   >>
3047 }
3048 @end lilypond
3049
3050 Notice that the half note which begins the measure takes up far less
3051 than half of the horizontal space of the measure.  Likewise, the
3052 sixteenth notes and sixteenth-note quintuplets (or twentieth notes)
3053 which end the measure together take up far more than half the
3054 horizontal space of the measure.
3055
3056 In classical engraving, this spacing may be exactly what we want
3057 because we can borrow horizontal space from the half note and conserve
3058 horizontal space across the measure as a whole.
3059
3060 On the other hand, if we want to insert a measured timeline or other
3061 graphic above or below our score, we need proportional notation.  We
3062 turn proportional notation on with the proportionalNotationDuration
3063 setting.
3064
3065 @lilypond[quote,verbatim,ragged-right]
3066 \score {
3067   <<
3068     \new RhythmicStaff {
3069       c'2
3070       c'16 c'16 c'16 c'16
3071       \times 4/5 {
3072         c'16 c'16 c'16 c'16 c'16
3073       }
3074     }
3075   >>
3076  \layout {
3077     \context {
3078       \Score
3079       proportionalNotationDuration = #(ly:make-moment 1 20)
3080     }
3081   }
3082 }
3083 @end lilypond
3084
3085 The half note at the beginning of the measure and the faster notes in
3086 the second half of the measure now occupy equal amounts of horizontal
3087 space.  We could place a measured timeline or graphic above or below
3088 this example.
3089
3090 The @code{proportionalNotationDuration} setting is a context setting
3091 that lives in @code{Score}.  Remember that context settings can appear
3092 in one of three locations within our input file -- in a @code{\with}
3093 block, in a @code{\context} block, or directly in music entry preceded
3094 by the @code{\set} command.  As with all context settings, users can
3095 pick which of the three different locations they would like to
3096 set @code{proportionalNotationDuration} in to.
3097
3098 The @code{proportionalNotationDuration} setting takes a single argument,
3099 which is the reference duration against that all music will be spaced.
3100 The LilyPond Scheme function @code{make-moment} takes two arguments
3101 -- a numerator and denominator which together express some fraction of
3102 a whole note.  The call @code{(ly:make-moment 1 20)} therefore produces
3103 a reference duration of a twentieth note.  Values such as
3104 @code{(ly:make-moment 1 16)}, @code{(ly:make-moment 1 8)}, and
3105 @code{(ly:make-moment 3 97)} are all possible as well.
3106
3107 How do we select the right reference duration to pass to
3108 @code{proportionalNotationDuration}?  Usually by a process of trial
3109 and error, beginning with a duration close to the fastest (or smallest)
3110 duration in the piece.  Smaller reference durations space music loosely;
3111 larger reference durations space music tightly.
3112
3113 @lilypond[quote,verbatim,ragged-right]
3114 \score {
3115   <<
3116     \new RhythmicStaff {
3117       c'2
3118       c'16 c'16 c'16 c'16
3119       \times 4/5 {
3120         c'16 c'16 c'16 c'16 c'16
3121       }
3122     }
3123   >>
3124   \layout {
3125     \context {
3126       \Score
3127       proportionalNotationDuration = #(ly:make-moment 1 8)
3128     }
3129   }
3130 }
3131
3132 \score {
3133   <<
3134     \new RhythmicStaff {
3135       c'2
3136       c'16 c'16 c'16 c'16
3137       \times 4/5 {
3138         c'16 c'16 c'16 c'16 c'16
3139       }
3140     }
3141   >>
3142   \layout {
3143     \context {
3144       \Score
3145       proportionalNotationDuration = #(ly:make-moment 1 16)
3146     }
3147   }
3148 }
3149
3150 \score {
3151   <<
3152     \new RhythmicStaff {
3153       c'2
3154       c'16 c'16 c'16 c'16
3155       \times 4/5 {
3156         c'16 c'16 c'16 c'16 c'16
3157       }
3158     }
3159   >>
3160   \layout {
3161     \context {
3162       \Score
3163       proportionalNotationDuration = #(ly:make-moment 1 32)
3164     }
3165   }
3166 }
3167 @end lilypond
3168
3169 Note that too large a reference duration -- such as the eighth note,
3170 above -- spaces music too tightly and can cause note head collisions.
3171 Also that proportional notation in general takes up more horizontal
3172 space than classical spacing.  Proportional spacing provides rhythmic
3173 clarity at the expense of horizontal space.
3174
3175 Next we examine how to optimally space overlapping tuplets.
3176
3177 We start by examining what happens to our original example, with
3178 classical spacing, when we add a second staff with a different type of
3179 tuplet.
3180
3181 @lilypond[quote,verbatim,ragged-right]
3182 \score {
3183   <<
3184     \new RhythmicStaff {
3185       c'2
3186       c'16 c'16 c'16 c'16
3187       \times 4/5 {
3188         c'16 c'16 c'16 c'16 c'16
3189       }
3190     }
3191     \new RhythmicStaff {
3192       \times 8/9 {
3193         c'8 c'8 c'8 c'8 c'8 c'8 c'8 c'8 c'8
3194       }
3195     }
3196   >>
3197 }
3198 @end lilypond
3199
3200 The spacing is bad because the evenly spaced notes of the bottom staff
3201 do not stretch uniformly.  Classical engravings include very few complex
3202 triplets and so classical engraving rules can generate this type of
3203 result.  Setting @code{proportionalNotationDuration} fixes this.
3204
3205 @lilypond[quote,verbatim,ragged-right]
3206 \score {
3207   <<
3208     \new RhythmicStaff {
3209       c'2
3210       c'16 c'16 c'16 c'16
3211       \times 4/5 {
3212         c'16 c'16 c'16 c'16 c'16
3213       }
3214     }
3215     \new RhythmicStaff {
3216       \times 8/9 {
3217         c'8 c'8 c'8 c'8 c'8 c'8 c'8 c'8 c'8
3218       }
3219     }
3220   >>
3221   \layout {
3222     \context {
3223       \Score
3224       proportionalNotationDuration = #(ly:make-moment 1 20)
3225     }
3226   }
3227 }
3228 @end lilypond
3229
3230 But if we look very carefully we can see that notes of the second half
3231 of the 9-tuplet space ever so slightly more widely than the notes
3232 of the first half of the 9-tuplet.  To ensure uniform stretching, we
3233 turn on @code{uniform-stretching}, which is a property of
3234 @code{SpacingSpanner}.
3235
3236 @lilypond[quote,verbatim,ragged-right]
3237 \score {
3238   <<
3239     \new RhythmicStaff {
3240       c'2
3241       c'16 c'16 c'16 c'16
3242       \times 4/5 {
3243         c'16 c'16 c'16 c'16 c'16
3244       }
3245     }
3246     \new RhythmicStaff {
3247       \times 8/9 {
3248         c'8 c'8 c'8 c'8 c'8 c'8 c'8 c'8 c'8
3249       }
3250     }
3251   >>
3252   \layout {
3253     \context {
3254       \Score
3255       proportionalNotationDuration = #(ly:make-moment 1 20)
3256       \override SpacingSpanner #'uniform-stretching = ##t
3257     }
3258   }
3259 }
3260 @end lilypond
3261
3262 Our two-staff example now spaces exactly, our rhythmic
3263 relationships are visually clear, and we can include a measured
3264 timeline or graphic if we want.
3265
3266 Note that the LilyPond's proportional notation package expects
3267 that all proportional scores set the SpacingSpanner's
3268 'uniform-stretching attribute to ##t.  Setting
3269 proportionalNotationDuration without also setting the
3270 SpacingSpanner's 'uniform-stretching attribute to ##t will, for
3271 example, cause Skips to consume an incorrect amount of horizontal
3272 space.
3273
3274 The SpacingSpanner is an abstract grob that lives in the Score
3275 context.  As with our settings of proportionalNotationDuration,
3276 overrides to the SpacingSpanner can occur in any of three
3277 different places in our input file â€“ in the Score \with block, in
3278 a Score \context block, or in note entry directly.
3279
3280 There is by default only one @code{SpacingSpanner} per @code{Score}.  This
3281 means that, by default, @code{uniform-stretching} is either turned on for the
3282 entire score or turned off for the entire score.  We can, however,
3283 override this behavior and turn on different spacing features at
3284 different places in the score.  We do this with the command
3285 @code{\newSpacingSection}.  See @ref{New spacing area}, for more info.
3286
3287 Next we examine the effects of the @code{Separating_line_group_engraver} and
3288 see why proportional scores frequently remove this engraver.  The following
3289 example shows that there is a small amount of @qq{prefatory} space
3290 just before the first note in each system.
3291
3292 @lilypond[quote,verbatim,ragged-right]
3293 \paper {
3294   indent = #0
3295 }
3296
3297 \new Staff {
3298   c'1
3299   \break
3300   c'1
3301 }
3302 @end lilypond
3303
3304
3305 The amount of this prefatory space is the same whether after a time
3306 signature, a key signature or a clef.  @code{Separating_line_group_engraver}
3307 is responsible for this space.  Removing @code{Separating_line_group_engraver}
3308 reduces this space to zero.
3309
3310 @lilypond[quote,verbatim,ragged-right]
3311 \paper {
3312   indent = #0
3313 }
3314
3315 \new Staff \with {
3316   \remove "Separating_line_group_engraver"
3317 } {
3318   c'1
3319   \break
3320   c'1
3321 }
3322 @end lilypond
3323
3324 non-musical elements like time signatures, key signatures, clefs and
3325 accidentals are problematic in proportional notation.  None of these
3326 elements has rhythmic duration.  But all of these elements consume
3327 horizontal space.  Different proportional scores approach these
3328 problems differently.
3329
3330 It may be possible to avoid spacing problems with key signatures
3331 simply by not having any.  This is a valid option since most
3332 proportional scores are contemporary music.  The same may be true
3333 of time signatures, especially for those scores
3334 that include a measured timeline or other graphic.  But these scores
3335 are exceptional and most proportional scores include at least some
3336 time signatures.  Clefs and accidentals are even more essential.
3337
3338 So what strategies exist for spacing non-musical elements in a
3339 proportional context?  One good option is the @code{strict-note-spacing}
3340 property of @code{SpacingSpanner}.  Compare the two scores below:
3341
3342 @lilypond[quote,verbatim,ragged-right]
3343 \new Staff {
3344   \set Score.proportionalNotationDuration = #(ly:make-moment 1 16)
3345   c''8
3346   c''8
3347   c''8
3348   \clef alto
3349   d'8
3350   d'2
3351 }
3352
3353 \new Staff {
3354   \set Score.proportionalNotationDuration = #(ly:make-moment 1 16)
3355   \override Score.SpacingSpanner #'strict-note-spacing = ##t
3356   c''8
3357   c''8
3358   c''8
3359   \clef alto
3360   d'8
3361   d'2
3362 }
3363 @end lilypond
3364
3365 Both scores are proportional, but the spacing in the first score
3366 is too loose because of the clef change.  The spacing of the second
3367 score remains strict, however, because strict-note-spacing is
3368 turned on.  Turning on strict-note-spacing causes the width of
3369 time signatures, key signatures, clefs and accidentals to play no
3370 part in the spacing algorithm.
3371
3372 In addition to the settings given here, there are other settings
3373 that frequently appear in proportional scores.  These include:
3374
3375 @itemize
3376 @item @code{\override SpacingSpanner #'strict-grace-spacing = ##t}
3377 @item @code{tupletFullLength = ##t}
3378 @item @code{\override Beam #'breakable = ##t}
3379 @item @code{\override Glissando #'breakable = ##t}
3380 @item @code{\override TextSpanner #'breakable = ##t}
3381 @item @code{\remove "Forbid_line_break_engraver" in the Voice context}
3382 @end itemize
3383
3384 These settings space grace notes strictly, extend tuplet brackets to
3385 mark both rhythmic start- and stop-points, and allow spanning elements
3386 to break across systems and pages.  See the respective parts of the manual
3387 for these related settings.
3388
3389 @seealso
3390 Notation Reference:
3391 @ref{New spacing area}.
3392
3393 Snippets:
3394 @rlsr{Spacing}.
3395
3396
3397 @node Fitting music onto fewer pages
3398 @section Fitting music onto fewer pages
3399
3400 Sometimes you can end up with one or two staves on a second
3401 (or third, or fourth@dots{}) page.  This is annoying, especially
3402 if you look at previous pages and it looks like there is plenty
3403 of room left on those.
3404
3405 When investigating layout issues, @code{annotate-spacing} is an
3406 invaluable tool.  This command prints the values of various layout
3407 spacing variables; for more details see the following section,
3408 @ref{Displaying spacing}.
3409
3410 @menu
3411 * Displaying spacing::
3412 * Changing spacing::
3413 @end menu
3414
3415
3416 @node Displaying spacing
3417 @subsection Displaying spacing
3418
3419 @funindex annotate-spacing
3420 @cindex spacing, display of layout
3421
3422 To graphically display the dimensions of vertical layout variables
3423 that may be altered for page formatting, set
3424 @code{annotate-spacing} in the @code{\paper} block:
3425
3426 @c need to have \book{} otherwise we get the separate systems. -hwn
3427 @lilypond[verbatim,quote]
3428 #(set-default-paper-size "a6" 'landscape)
3429 \book {
3430   \score { { c4 } }
3431   \paper { annotate-spacing = ##t }
3432 }
3433 @end lilypond
3434
3435
3436 @noindent
3437 All layout dimensions are displayed in staff-spaces, regardless
3438 of the units specified in the @code{\paper} or @code{\layout} block.
3439 In the above example, @code{paper-height} has a value of 59.75
3440 @code{staff-spaces}, and the @code{staff-size} is 20 points (the
3441 default value).  Note that:
3442
3443 @multitable {1 staff-space} {staff-size)/4 * (25.4/72.27) mm}
3444
3445 @item 1 point
3446 @tab = (25.4/72.27) mm
3447
3448 @item 1 staff-space
3449 @tab = (@code{staff-size})/4 pts
3450 @item
3451 @tab = (@code{staff-size})/4 * (25.4/72.27) mm
3452
3453 @end multitable
3454
3455 @noindent
3456 In this case, one @code{staff-space} is approximately equal to
3457 1.757mm.  Thus the @code{paper-height} measurement of 59.75
3458 @code{staff-spaces} is equivalent to 105 millimeters, the height
3459 of @code{a6} paper in landscape orientation.  The pairs
3460 (@var{a},@var{b}) are intervals, where @var{a} is the lower
3461 edge and @var{b} the upper edge of the interval.
3462
3463 @seealso
3464 Notation Reference:
3465 @ref{Setting the staff size}.
3466
3467 Snippets:
3468 @rlsr{Spacing}.
3469
3470
3471 @node Changing spacing
3472 @subsection Changing spacing
3473
3474 The output of @code{annotate-spacing} reveals vertical dimensions
3475 in great detail.  For details about modifying margins and other
3476 layout variables, see @ref{Page layout}.
3477
3478 Other than margins, there are a few other options to save space:
3479
3480 @itemize
3481 @item
3482 Force systems to move as close together as possible (to fit as
3483 many systems as possible onto a page) while being spaced so that
3484 there is no blank space at the bottom of the page.
3485
3486 @example
3487 \paper @{
3488   system-system-spacing = #'((basic-distance . 0.1) (padding . 0))
3489   ragged-last-bottom = ##f
3490   ragged-bottom = ##f
3491 @}
3492 @end example
3493
3494 @item
3495 Force the number of systems.  This can help in two ways.  Just
3496 setting a value, even the same value as the number of systems
3497 being typeset by default, will sometimes cause more systems to
3498 be fitted onto each page, as an estimation step is then bypassed,
3499 giving a more accurate fit to each page.  Also, forcing an actual
3500 reduction in the number of systems may save a further page.  For
3501 example, if the default layout has 11 systems, the following
3502 assignment will force a layout with 10 systems.
3503
3504 @example
3505 \paper @{
3506   system-count = #10
3507 @}
3508 @end example
3509
3510 @item
3511 Force the number of pages.  For example, the following
3512 assignment will force a layout with 2 pages.
3513
3514 @example
3515 \paper @{
3516   page-count = #2
3517 @}
3518 @end example
3519
3520 @item
3521 Avoid (or reduce) objects that increase the vertical size of a
3522 system.  For example, volta repeats (or alternate repeats) require
3523 extra space.  If these repeats are spread over two systems, they
3524 will take up more space than one system with the volta repeats and
3525 another system without.  For example, dynamics that @q{stick out} of
3526 a system can be moved closer to the staff:
3527
3528 @lilypond[verbatim,quote,relative=1]
3529 e4 c g\f c
3530 e4 c g-\tweak #'X-offset #-2.7 -\tweak #'Y-offset #2.5 \f c
3531 @end lilypond
3532
3533 @item
3534 Alter the horizontal spacing via @code{SpacingSpanner}.  For more
3535 details, see @ref{Changing horizontal spacing}.  The following
3536 example illustrates the default spacing:
3537
3538 @lilypond[verbatim,quote]
3539 \score {
3540   \relative c'' {
3541     g4 e e2 |
3542     f4 d d2 |
3543     c4 d e f |
3544     g4 g g2 |
3545     g4 e e2 |
3546   }
3547 }
3548 @end lilypond
3549
3550 @noindent
3551 The next example modifies @code{common-shortest-duration} from a
3552 value of @code{1/4} to @code{1/2}.  The quarter note is the most
3553 common and shortest duration in this example, so by making this
3554 duration longer, a @q{squeezing} effect occurs:
3555
3556 @lilypond[verbatim,quote]
3557 \score {
3558   \relative c'' {
3559     g4 e e2 |
3560     f4 d d2 |
3561     c4 d e f |
3562     g4 g g2 |
3563     g4 e e2 |
3564   }
3565   \layout {
3566     \context {
3567       \Score
3568       \override SpacingSpanner
3569         #'common-shortest-duration = #(ly:make-moment 1 2)
3570     }
3571   }
3572 }
3573 @end lilypond
3574
3575 @noindent
3576 The @code{common-shortest-duration} property cannot be modified
3577 dynamically, so it must always be placed in a @code{\context}
3578 block so that it applies to the whole score.
3579
3580 @end itemize
3581
3582 @seealso
3583 Notation Reference:
3584 @ref{Page layout},
3585 @ref{Changing horizontal spacing}.
3586
3587 Snippets:
3588 @rlsr{Spacing}.