]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/user/spacing.itely
Merge branch 'master' of git://git.sv.gnu.org/lilypond
[lilypond.git] / Documentation / user / spacing.itely
1 @c -*- coding: utf-8; mode: texinfo; -*-
2 @c This file is part of lilypond.tely
3
4 @c A menu is needed before every deeper *section nesting of @node's; run
5 @c     M-x texinfo-all-menus-update
6 @c to automatically fill in these menus before saving changes
7
8 @node Spacing issues
9 @chapter Spacing issues
10
11 The global paper layout is determined by three factors: the page layout, the
12 line breaks, and the spacing.  These all influence each other.  The
13 choice of spacing determines how densely each system of music is set.
14 This influences where line breaks are chosen, and thus ultimately, how
15 many pages a piece of music takes.
16
17 Globally speaking, this procedure happens in four steps: first,
18 flexible distances (``springs'') are chosen, based on durations.  All
19 possible line breaking combinations are tried, and a ``badness'' score
20 is calculated for each. Then the height of each possible system is
21 estimated. Finally, a page breaking and line breaking combination is chosen
22 so that neither the horizontal nor the vertical spacing is too cramped
23 or stretched.
24
25 @menu
26 * Paper and pages::             
27 * Music layout::                
28 * Vertical spacing::            
29 * Horizontal spacing::          
30 * Breaks::                      
31 * Displaying spacing::          
32 * Vertical collision avoidance::  
33 @end menu
34
35
36 @node Paper and pages
37 @section Paper and pages
38
39 This section deals with the boundaries that define the area
40 that music can be printed inside.
41
42 @menu
43 * Paper size::                  
44 * Page formatting::             
45 @end menu
46
47
48 @node Paper size
49 @subsection Paper size
50
51 @cindex paper size
52 @cindex page size
53 @funindex papersize
54
55 To change the paper size, there are two commands,
56 @example
57 #(set-default-paper-size "a4")
58 \paper @{
59   #(set-paper-size "a4")
60 @}
61 @end example
62
63 The first command sets the size of all pages.  The second command sets the
64 size
65 of the pages that the @code{\paper} block applies to -- if the @code{\paper}
66 block is at the top of the file, then it will apply to all pages.  If the
67 @code{\paper} block is inside a @code{\book}, then the paper size will only
68 apply to that book.
69
70 Support for the following paper sizes are included by default,
71 @code{a6}, @code{a5}, @code{a4}, @code{a3}, @code{legal}, @code{letter},
72 @code{11x17} (also known as tabloid).
73
74 Extra sizes may be added by editing the definition for
75 @code{paper-alist} in the initialization file @file{scm/paper.scm}.
76
77 @cindex orientation
78 @cindex landscape
79
80 If the symbol @code{landscape} is supplied as an argument to
81 @code{set-default-paper-size}, the pages will be rotated by 90 degrees,
82 and wider line widths will be set correspondingly.
83
84 @example
85 #(set-default-paper-size "a6" 'landscape)
86 @end example
87
88 Setting the paper size will adjust a number of @code{\paper} variables
89 (such as margins).  To use a particular paper size with altered
90 @code{\paper} variables, set the paper size before setting the variables.
91
92
93 @node Page formatting
94 @subsection Page formatting
95
96 @cindex page formatting
97 @cindex margins
98 @cindex header, page
99 @cindex footer, page
100
101 LilyPond will do page layout, set margins, and add headers and
102 footers to each page.
103
104 The default layout responds to the following settings in the
105 @code{\paper} block.
106
107 @funindex \paper
108
109 @quotation
110 @table @code
111 @funindex first-page-number
112 @item first-page-number
113 The value of the page number of the first page.  Default is@tie{}1.
114
115 @funindex printfirst-page-number
116 @item print-first-page-number
117 If set to true, will print the page number in the first page.  Default is
118 false.
119
120 @funindex print-page-number
121 @item print-page-number
122 If set to false, page numbers will not be printed. Default is true.
123
124 @funindex paper-width
125 @item paper-width
126 The width of the page. The default is taken from the current paper size,
127 see @ref{Paper size}. 
128
129 @funindex paper-height
130 @item paper-height
131 The height of the page. The default is taken from the current paper size,
132 see @ref{Paper size}. 
133
134 @funindex top-margin
135 @item top-margin
136 Margin between header and top of the page. Default is@tie{}5mm.
137
138 @funindex bottom-margin
139 @item bottom-margin
140 Margin between footer and bottom of the page. Default is@tie{}6mm.
141
142 @funindex left-margin
143 @item left-margin
144 Margin between the left side of the page and the beginning of the
145 music. Unset by default, which means that the margins is determined
146 based on the @code{paper-width} and @code{line-width} to center the
147 score on the paper. 
148
149 @funindex line-width
150 @item line-width
151 The length of the systems. Default is @code{paper-width} minus @tie{}20mm.
152
153 @funindex head-separation
154 @item head-separation
155 Distance between the top-most music system and the page header. Default
156 is@tie{}4mm. 
157
158 @funindex foot-separation
159 @item foot-separation
160 Distance between the bottom-most music system and the page
161 footer. Default is@tie{}4mm.
162
163 @funindex page-top-space
164 @item page-top-space
165 Distance from the top of the printable area to the center of the first
166 staff. This only works for staves which are vertically small. Big staves
167 are set with the top of their bounding box aligned to the top of the
168 printable area. Default is@tie{}12mm.
169
170 @funindex ragged-bottom
171 @item ragged-bottom
172 If set to true, systems will not be spread vertically across the page.  This
173 does not affect the last page. Default is false.
174
175 This should be set to true for pieces that have only two or three
176 systems per page, for example orchestral scores.
177
178 @funindex ragged-last-bottom
179 @item ragged-last-bottom
180 If set to false, systems will be spread vertically to fill the last
181 page. Default is true.
182
183 Pieces that amply fill two pages or more should have this set to
184 true.
185
186 @funindex system-count
187 @item system-count
188 This variable, if set, specifies into how many lines a score should be
189 broken. Unset by default.
190
191 @funindex between-system-space
192 @item between-system-space
193 This dimensions determines the distance between systems.  It is the
194 ideal distance between the center of the bottom staff of one system
195 and the center of the top staff of the next system. Default is@tie{}20mm.
196
197 Increasing this will provide a more even appearance of the page at the
198 cost of using more vertical space.
199
200 @funindex between-system-padding
201 @item between-system-padding
202 This dimension is the minimum amount of white space that will always
203 be present between the bottom-most symbol of one system, and the
204 top-most of the next system. Default is@tie{}4mm.
205
206 Increasing this will put systems whose bounding boxes almost touch
207 farther apart.
208
209
210 @funindex horizontal-shift
211 @item horizontal-shift
212 All systems (including titles and system separators) are shifted by
213 this amount to the right. Page markup, such as headers and footers are
214 not affected by this. The purpose of this variable is to make space
215 for instrument names at the left. Default is@tie{}0.
216
217 @funindex after-title-space
218 @item after-title-space
219 Amount of space between the title and the first system. Default is@tie{}5mm.
220
221 @funindex before-title-space
222 @item before-title-space
223 Amount of space between the last system of the previous piece and the
224 title of the next. Default is@tie{}10mm.
225
226 @funindex between-title-space
227 @item between-title-space
228 Amount of space between consecutive titles (e.g., the title of the
229 book and the title of a piece). Default is@tie{}2mm.
230
231 @funindex printallheaders
232 @item printallheaders
233 Setting this to #t will print all headers for each \score in the
234 output.  Normally only the piece and opus \headers are printed.
235
236 @funindex systemSeparatorMarkup
237 @item systemSeparatorMarkup
238 This contains a markup object, which will be inserted between
239 systems.  This is often used for orchestral scores. Unset by default.
240
241 The markup command @code{\slashSeparator} is provided as a sensible
242 default,  for example
243
244 @lilypond[ragged-right]
245 #(set-default-paper-size "a6" 'landscape)
246 \book {
247   \score {
248     \relative { c1 \break c1 }
249   }
250   \paper {
251     systemSeparatorMarkup = \slashSeparator
252   }
253 }
254 @end lilypond
255
256 @funindex blank-page-force
257 @item blank-page-force
258 The penalty for having a blank page in the middle of a
259 score. This is not used by @code{ly:optimal-breaking} since it will
260 never consider blank pages in the middle of a score. Default value
261 is 10.
262
263 @funindex blank-last-page-force
264 @item blank-last-page-force
265 The penalty for ending the score on an odd-numbered page.
266 Default value is 0.
267
268 @funindex page-spacing-weight
269 @item page-spacing-weight
270 The relative importance of page (vertical) spacing and line (horizontal)
271 spacing. High values will make page spacing more important. Default
272 value is 10.
273
274 @funindex auto-first-page-number
275 @item auto-first-page-number
276 The page breaking algorithm is affected by the first page number being
277 odd or even.  If this variable is set to #t, the page breaking algorithm
278 will decide whether to start with an odd or even number.  This will 
279 result in the first page number remaining as is or being increased by one.
280
281 @end table
282 @end quotation
283
284 Example:
285
286 @example
287 \paper@{
288   paper-width = 2\cm
289   top-margin = 3\cm
290   bottom-margin = 3\cm
291   ragged-last-bottom = ##t
292 @}
293 @end example
294
295 You can also define these values in Scheme.  In that case @code{mm},
296 @code{in}, @code{pt}, and @code{cm} are variables defined in
297 @file{paper-defaults.ly} with values in millimeters.  That is why the
298 value must be multiplied in the example
299
300 @example
301 \paper @{
302   #(define bottom-margin (* 2 cm))
303 @}
304 @end example
305
306 The header and footer are created by the functions @code{make-footer}
307 and @code{make-header}, defined in @code{\paper}.  The default
308 implementations are in @file{ly/@/paper@/-defaults@/.ly} and
309 @file{ly/@/titling@/-init@/.ly}.
310
311 The page layout itself is done by two functions in the
312 @code{\paper} block, @code{page-music-height} and
313 @code{page-make-stencil}.  The former tells the line-breaking algorithm
314 how much space can be spent on a page, the latter creates the actual
315 page given the system to put on it.
316
317
318 @refbugs
319
320 The option right-margin is defined but doesn't set the right margin
321 yet.  The value for the right margin has to be defined adjusting the
322 values of @code{left-margin} and @code{line-width}.
323
324 The default page header puts the page number and the @code{instrument}
325 field from the @code{\header} block on a line.
326
327 The titles (from the @code{\header@{@}} section) are treated as a
328 system, so @code{ragged-bottom} and @code{ragged-last-bottom} will
329 add space between the titles and the first system of the score.
330
331
332 @node Music layout
333 @section Music layout
334
335 @menu
336 * Setting the staff size::   
337 * Score layout::                
338 @end menu
339
340
341 @node Setting the staff size
342 @subsection Setting the staff size
343
344 @cindex font size, setting
345 @cindex staff size, setting
346 @funindex layout file
347
348 To set the staff size globally for all scores in a file (or
349 in a @code{book} block, to be precise), use @code{set-global-staff-size}.
350
351 @example
352 #(set-global-staff-size 14)
353 @end example
354
355 @noindent
356 This sets the global default size to 14pt staff height and scales all
357 fonts accordingly.
358
359 To set the staff size individually for each score, use 
360 @example
361 \score@{
362   ...
363   \layout@{
364   #(layout-set-staff-size 15)
365   @}
366 @}
367 @end example
368
369 The Feta font provides musical symbols at eight different
370 sizes.  Each font is tuned for a different staff size: at a smaller size
371 the font becomes heavier, to match the relatively heavier staff lines.
372 The recommended font sizes are listed in the following table:
373
374 @quotation
375 @multitable @columnfractions .15 .2 .22 .2
376
377 @item @b{font name}
378 @tab @b{staff height (pt)}
379 @tab @b{staff height (mm)}
380 @tab @b{use}
381
382 @item feta11
383 @tab 11.22
384 @tab 3.9
385 @tab pocket scores
386
387 @item feta13
388 @tab 12.60
389 @tab 4.4
390 @tab
391
392 @item feta14
393 @tab 14.14
394 @tab 5.0
395 @tab
396
397 @item feta16
398 @tab 15.87
399 @tab 5.6
400 @tab
401
402 @item feta18
403 @tab 17.82
404 @tab 6.3
405 @tab song books
406
407 @item feta20
408 @tab 20
409 @tab 7.0
410 @tab standard parts
411
412 @item feta23
413 @tab 22.45
414 @tab 7.9
415 @tab
416
417 @item feta26
418 @tab 25.2
419 @tab 8.9
420 @tab
421 @c modern rental material?
422
423 @end multitable
424 @end quotation
425
426 These fonts are available in any sizes.  The context property
427 @code{fontSize} and the layout property @code{staff-space} (in
428 @internalsref{StaffSymbol}) can be used to tune the size for individual
429 staves.  The sizes of individual staves are relative to the global size.
430
431 @example
432
433 @end example
434
435 @seealso
436
437 This manual: @ref{Selecting notation font size}.
438
439
440 @node Score layout
441 @subsection Score layout
442
443 @funindex \layout
444
445 While @code{\paper} contains settings that relate to the page formatting
446 of the whole document, @code{\layout} contains settings for score-specific
447 layout.
448
449 @example
450 \layout @{
451   indent = 2.0\cm
452   \context @{ \Staff
453     \override VerticalAxisGroup #'minimum-Y-extent = #'(-6 . 6)
454   @}
455   \context @{ \Voice
456     \override TextScript #'padding = #1.0
457     \override Glissando #'thickness = #3
458   @}
459 @}
460 @end example
461
462
463 @seealso
464
465 This manual: @ref{Changing context default settings}
466
467
468 @node Vertical spacing
469 @section Vertical spacing
470
471 @cindex vertical spacing
472 @cindex spacing, vertical
473
474 Vertical spacing is controlled by three things: the amount of
475 space available (i.e., paper size and margins), the amount of
476 space between systems, and the amount of space between
477 staves inside a system.
478
479 @menu
480 * Vertical spacing inside a system::  
481 * Vertical spacing of piano staves::  
482 * Vertical spacing between systems::  
483 * Controlling spacing of individual systems::  
484 * Two-pass vertical spacing::
485 @end menu
486
487
488 @node Vertical spacing inside a system
489 @subsection Vertical spacing inside a system
490
491 @cindex distance between staves
492 @cindex staff distance
493 @cindex space between staves
494 @cindex space inside systems
495
496 The height of each system is determined automatically.  To prevent
497 staves from bumping into each other, some minimum distances are set.
498 By changing these, you can put staves closer together.  This
499 reduces the amount of space each system requires, and may result
500 in having more systems per page.
501
502 Normally staves are stacked vertically.  To make staves maintain a
503 distance, their vertical size is padded.  This is done with the
504 property @code{minimum-Y-extent}.  When applied to a
505 @internalsref{VerticalAxisGroup}, it controls the size of a horizontal
506 line, such as a staff or a line of lyrics.  @code{minimum-Y-extent}
507 takes a pair of numbers, so
508 if you want to make it smaller than its default @code{#'(-4 . 4)}
509 then you could set
510
511 @example
512 \override Staff.VerticalAxisGroup #'minimum-Y-extent = #'(-3 . 3)
513 @end example
514
515 @noindent
516 This sets the vertical size of the current staff to 3 staff spaces on
517 either side of the center staff line.  The value @code{(-3 . 3)} is
518 interpreted as an interval, where the center line is the 0, so the
519 first number is generally negative.  The numbers need not match;
520 for example, the staff can be made larger at the bottom by setting
521 it to @code{(-6 . 4)}.
522
523
524 @seealso
525
526 Internals: Vertical alignment of staves is handled by the
527 @internalsref{VerticalAlignment} object. The context parameters
528 specifying the vertical extent are described in connection with
529 the @internalsref{Axis_group_engraver}.
530
531 Example files: @inputfileref{input/regression/,page-spacing.ly},
532 @inputfileref{input/regression/,alignment-vertical-spacing.ly}.
533
534
535 @node Vertical spacing of piano staves
536 @subsection Vertical spacing of piano staves
537
538 The distance between staves of a @internalsref{PianoStaff} cannot be
539 computed during formatting.  Rather, to make cross-staff beaming work
540 correctly, that distance has to be fixed beforehand.
541
542 The distance of staves in a @code{PianoStaff} is set with the
543 @code{forced-distance} property of the
544 @internalsref{VerticalAlignment} object, created in
545 @internalsref{PianoStaff}.
546
547 It can be adjusted as follows
548 @example
549 \new PianoStaff \with @{
550   \override VerticalAlignment #'forced-distance = #7
551 @} @{
552   ...
553 @}
554 @end example
555
556 @noindent
557 This would bring the staves together at a distance of 7 staff spaces,
558 measured from the center line of each staff.
559
560 The difference is demonstrated in the following example,
561 @lilypond[quote,verbatim]
562 \relative c'' <<
563   \new PianoStaff \with {
564     \override VerticalAlignment #'forced-distance = #7
565   } <<
566     \new Staff { c1 }
567     \new Staff { c }
568   >>
569   \new PianoStaff <<
570     \new Staff { c }
571     \new Staff { c }
572   >>
573 >>
574 @end lilypond
575
576
577 @seealso
578
579 Example files: @inputfileref{input/regression/,alignment-vertical-spacing.ly}.
580
581
582 @node Vertical spacing between systems
583 @subsection Vertical spacing between systems
584
585 Space between systems are controlled by four @code{\paper} variables,
586
587 @example
588 \paper @{
589   between-system-space = 1.5\cm
590   between-system-padding = #1
591   ragged-bottom=##f
592   ragged-last-bottom=##f
593 @}
594 @end example
595
596
597 @node Controlling spacing of individual systems
598 @subsection Controlling spacing of individual systems
599
600 It is also possible to change the distance between for each system
601 individually.  This is done by including the command
602
603 @example
604 \overrideProperty
605 #"Score.NonMusicalPaperColumn"
606 #'line-break-system-details
607 #'((fixed-alignment-extra-space . 15))
608 @end example
609
610 @noindent
611 at the line break before the system to be changed. The distance
612 @code{15} is distributed over all staves that have a fixed distance
613 alignment.  For example,
614
615 @lilypond[ragged-right, fragment, relative=2, staffsize=13]
616 \new PianoStaff <<
617   \new Staff {
618     c1\break
619   
620     \overrideProperty
621     #"Score.NonMusicalPaperColumn"
622     #'line-break-system-details
623     #'((fixed-alignment-extra-space . 15))
624
625     c\break
626   }
627   \new Staff { c c }
628 >>
629 @end lilypond
630
631 The distance for @code{fixed-alignment-extra-space} may also be
632 negative.
633
634
635 @node Two-pass vertical spacing
636 @subsection Two-pass vertical spacing
637
638 In order to automatically stretch systems so that they should fill the
639 space left on a page, a two-pass technique can be used:
640
641 @enumerate
642 @item In the first pass, the amount of vertical space used to increase
643 the height of each system is computed and dumped to a file.
644 @item In the second pass, spacing inside the systems are
645 stretched according to the data in the page layout file.
646 @end enumerate
647
648 The @code{ragged-bottom} property adds space between systems, while
649 the two-pass technique adds space between staffs inside a system.
650
651 To allow this behaviour, a @code{tweak-key} variable has to be set in
652 each score @code{\layout} block, and the tweaks included in each score
653 music, using the @code{\scoreTweak} music function.
654
655 @quotation
656 @verbatim
657 %% include the generated page layout file:
658 \includePageLayoutFile
659
660 \score {
661   \new StaffGroup <<
662     \new Staff <<
663       %% Include this score tweaks:
664       \scoreTweak "scoreA"
665       { \clef french c''1 \break c''1 } 
666     >>
667     \new Staff { \clef soprano g'1 g'1 }
668     \new Staff { \clef mezzosoprano e'1 e'1 }
669     \new Staff { \clef alto g1 g1 }
670     \new Staff { \clef bass c1 c1 }
671   >>
672   \header {
673     piece = "Score with tweaks"
674   }
675   %% Define how to name the tweaks for this score:
676   \layout { #(define tweak-key "scoreA") }
677 }
678 @end verbatim
679 @end quotation
680
681 For the first pass, the @code{dump-tweaks} option should be set to
682 generate the page layout file.
683
684 @example
685 lilypond -b null -d dump-tweaks <file>.ly
686 lilypond <file>.ly
687 @end example
688
689 @node Horizontal spacing
690 @section Horizontal Spacing
691
692 @cindex horizontal spacing
693 @cindex spacing, horizontal
694
695 @menu
696 * Horizontal spacing overview::
697 * New spacing area::
698 * Changing horizontal spacing::
699 * Line length::
700 @end menu
701
702
703 @node Horizontal spacing overview
704 @subsection Horizontal spacing overview
705
706 The spacing engine translates differences in durations into stretchable
707 distances (``springs'') of differring lengths.  Longer durations get
708 more space, shorter durations get less.  The shortest durations get a
709 fixed amount of space (which is controlled by
710 @code{shortest-duration-space} in the @internalsref{SpacingSpanner}
711 object).  The longer the duration, the more space it gets: doubling a
712 duration adds a fixed amount (this amount is controlled by
713 @code{spacing-increment}) of space to the note.
714
715 For example, the following piece contains lots of half, quarter, and
716 8th notes; the eighth note is followed by 1 note head width (NHW).
717 The quarter note is followed by 2 NHW, the half by 3 NHW, etc.
718
719 @lilypond[quote,fragment,verbatim,relative=1]
720 c2 c4. c8 c4. c8 c4. c8 c8
721 c8 c4 c4 c4
722 @end lilypond
723
724 Normally, @code{spacing-increment} is set to 1.2 staff space, which is
725 approximately the width of a note head, and
726 @code{shortest-duration-space} is set to 2.0, meaning that the
727 shortest note gets 2.4 staff space (2.0 times the
728 @code{spacing-increment}) of horizontal space.  This space is counted
729 from the left edge of the symbol, so the shortest notes are generally
730 followed by one NHW of space.
731
732 If one would follow the above procedure exactly, then adding a single
733 32nd note to a score that uses 8th and 16th notes, would widen up the
734 entire score a lot.  The shortest note is no longer a 16th, but a 32nd,
735 thus adding 1 NHW to every note.  To prevent this, the shortest
736 duration for spacing is not the shortest note in the score, but rather
737 the one which occurs most frequently.
738
739
740 The most common shortest duration is determined as follows: in every
741 measure, the shortest duration is determined.  The most common shortest
742 duration is taken as the basis for the spacing, with the stipulation
743 that this shortest duration should always be equal to or shorter than
744 an 8th note.  The shortest duration is printed when you run
745 @code{lilypond} with the @code{--verbose} option.
746
747 These durations may also be customized.  If you set the
748 @code{common-shortest-duration} in @internalsref{SpacingSpanner}, then
749 this sets the base duration for spacing.  The maximum duration for this
750 base (normally an 8th), is set through @code{base-shortest-duration}.
751
752 @funindex common-shortest-duration
753 @funindex base-shortest-duration
754 @funindex stem-spacing-correction
755 @funindex spacing
756
757 Notes that are even shorter than the common shortest note are
758 followed by a space that is proportional to their duration relative to
759 the common shortest note.  So if we were to add only a few 16th notes
760 to the example above, they would be followed by half a NHW:
761
762 @lilypond[quote,fragment,verbatim,relative=2]
763 c2 c4. c8 c4. c16[ c] c4. c8 c8 c8 c4 c4 c4
764 @end lilypond
765
766
767 In the introduction (see @ref{Engraving}), it was explained that stem
768 directions influence spacing.  This is controlled with the
769 @code{stem-spacing-correction} property in the
770 @internalsref{NoteSpacing}, object.  These are generated for every
771 @internalsref{Voice} context.  The @code{StaffSpacing} object
772 (generated in @internalsref{Staff} context) contains the same property
773 for controlling the stem/bar line spacing.  The following example shows
774 these corrections, once with default settings, and once with
775 exaggerated corrections:
776
777 @lilypond[quote,ragged-right]
778 {
779   c'4 e''4 e'4 b'4 |
780   b'4 e''4 b'4 e''4|
781   \override Staff.NoteSpacing #'stem-spacing-correction = #1.5
782   \override Staff.StaffSpacing #'stem-spacing-correction = #1.5
783   c'4 e''4 e'4 b'4 |
784   b'4 e''4 b'4 e''4|
785 }
786 @end lilypond
787
788 Proportional notation is supported; see @ref{Proportional notation}.
789
790
791 @seealso
792
793 Internals: @internalsref{SpacingSpanner}, @internalsref{NoteSpacing},
794 @internalsref{StaffSpacing}, @internalsref{SeparationItem}, and
795 @internalsref{SeparatingGroupSpanner}.
796
797
798 @refbugs
799
800 There is no convenient mechanism to manually override spacing.  The
801 following work-around may be used to insert extra space into a score.
802 @example
803  \once \override Score.SeparationItem #'padding = #1
804 @end example
805
806 No work-around exists for decreasing the amount of space.
807
808
809 @node New spacing area
810 @subsection New spacing area
811
812 New sections with different spacing parameters can be started with
813 @code{newSpacingSection}.  This is useful when there are  
814 sections with a different notions of long and short notes.
815
816 In the following example, the time signature change introduces a new
817 section, and hence the 16ths notes are spaced wider.
818
819 @lilypond[relative,fragment,verbatim,quote]
820 \time 2/4
821 c4 c8 c 
822 c8 c c4 c16[ c c8] c4
823 \newSpacingSection
824 \time 4/16
825 c16[ c c8]
826 @end lilypond
827
828
829 The @code{\newSpacingSection} command creates a new
830 @internalsref{SpacingSpanner} object, and hence new @code{\override}s
831 may be used in that location.
832
833
834 @node Changing horizontal spacing
835 @subsection Changing horizontal spacing
836
837 Horizontal spacing may be altered with the
838 @code{base-shortest-duration} property.  Here
839 we compare the same music; once without altering
840 the property, and then altered.  Larger values
841 of @code{ly:make-moment} will produce smaller
842 music.  Note that @code{ly:make-moment} constructs
843 a duration, so @code{1 4} is a longer duration
844 than @code{1 16}.
845
846 @lilypond[relative,verbatim,line-width=12\cm]
847 \score {
848   \relative c'' {
849     g4 e e2 | f4 d d2 | c4 d e f | g4 g g2 |
850     g4 e e2 | f4 d d2 | c4 e g g | c,1 |
851     d4 d d d | d4 e f2 | e4 e e e | e4 f g2 |
852     g4 e e2 | f4 d d2 | c4 e g g | c,1 |
853   }
854 }
855 @end lilypond
856
857 @lilypond[relative,verbatim,line-width=12\cm]
858 \score {
859   \relative c'' {
860     g4 e e2 | f4 d d2 | c4 d e f | g4 g g2 |
861     g4 e e2 | f4 d d2 | c4 e g g | c,1 |
862     d4 d d d | d4 e f2 | e4 e e e | e4 f g2 |
863     g4 e e2 | f4 d d2 | c4 e g g | c,1 |
864   }
865   \layout {
866     \context {
867       \Score
868       \override SpacingSpanner
869                 #'base-shortest-duration = #(ly:make-moment 1 16)
870     }
871   }
872 }
873 @end lilypond
874
875
876 @commonprop
877
878 By default, spacing in tuplets depends on various non-duration
879 factors (such as accidentals, clef changes, etc).  To disregard
880 such symbols and force uniform equal-duration spacing, use
881 @code{Score.SpacingSpanner #'uniform-stretching}.  This
882 property can only be changed at the beginning of a score,
883
884 @lilypond[quote,ragged-right,relative=2,fragment,verbatim]
885 \new Score \with {
886   \override SpacingSpanner #'uniform-stretching = ##t
887 } <<
888   \new Staff{
889     \times 4/5 {
890       c8 c8 c8 c8 c8
891     }
892     c8 c8 c8 c8
893   }
894   \new Staff{
895     c8 c8 c8 c8
896     \times 4/5 {
897       c8 c8 c8 c8 c8
898     }
899   }
900 >>
901 @end lilypond
902
903
904 When @code{strict-note-spacing} is set, notes are spaced without
905 regard for clefs, bar lines, and grace notes,
906
907 @lilypond[quote,ragged-right,relative=2,fragment,verbatim]
908 \override Score.SpacingSpanner #'strict-note-spacing = ##t
909 \new Staff { c8[ c \clef alto c \grace { c16[ c] } c8 c c]  c32[ c32] }
910 @end lilypond
911
912
913 @node Line length
914 @subsection Line length
915
916 @cindex page breaks
917 @cindex breaking pages
918
919 @funindex indent
920 @funindex line-width
921 @funindex ragged-right
922 @funindex ragged-last
923
924 @c Although line-width can be set in \layout, it should be set in paper
925 @c block, to get page layout right.
926 @c Setting indent in \paper block makes not much sense, but it works.
927
928 @c Bit verbose and vague, use examples?
929 The most basic settings influencing the spacing are @code{indent} and
930 @code{line-width}.  They are set in the @code{\layout} block.  They
931 control the indentation of the first line of music, and the lengths of
932 the lines.
933
934 If @code{ragged-right} is set to true in the @code{\layout} block, then
935 systems ends at their natural horizontal length, instead of being spread
936 horizontally to fill the whole line.  This is useful for
937 short fragments, and for checking how tight the natural spacing is.
938
939 @cindex page layout
940 @cindex vertical spacing
941
942 The option @code{ragged-last} is similar to @code{ragged-right}, but
943 only affects the last line of the piece.  No restrictions are put on
944 that line.  The result is similar to formatting text paragraphs.  In a
945 paragraph, the last line simply takes its natural horizontal length.
946 @c Note that for text there are several options for the last line.
947 @c While Knuth TeX uses natural length, lead typesetters use the same
948 @c stretch as the previous line.  eTeX uses \lastlinefit to
949 @c interpolate between both these solutions.
950
951 @example
952 \layout @{
953   indent = #0
954   line-width = #150
955   ragged-last = ##t
956 @}
957 @end example
958
959
960 @node Breaks
961 @section Breaks
962
963 @menu
964 * Line breaking::               
965 * Page breaking::               
966 * Optimal page breaking::       
967 * Optimal page turning::        
968 @end menu
969
970 @node Line breaking
971 @subsection Line breaking
972
973 @cindex line breaks
974 @cindex breaking lines
975
976 Line breaks are normally computed automatically.  They are chosen so
977 that lines look neither cramped nor loose, and that consecutive lines
978 have similar density.
979
980 Occasionally you might want to override the automatic breaks; you can
981 do this by specifying @code{\break}.  This will force a line break at
982 this point.  Line breaks can only occur at places where there are bar
983 lines.  If you want to have a line break where there is no bar line,
984 you can force an invisible bar line by entering @code{\bar
985 ""}.  Similarly, @code{\noBreak} forbids a line break at a
986 point.
987
988
989 @cindex regular line breaks
990 @cindex four bar music.
991
992 For line breaks at regular intervals use @code{\break} separated by
993 skips and repeated with @code{\repeat}:
994 @example
995 << \repeat unfold 7 @{
996          s1 \noBreak s1 \noBreak
997          s1 \noBreak s1 \break @}
998    @emph{the real music}
999 >>
1000 @end example
1001
1002 @noindent
1003 This makes the following 28 measures (assuming 4/4 time) be broken every
1004 4 measures, and only there.
1005
1006 @refcommands
1007
1008 @code{\break}, and @code{\noBreak}.
1009 @funindex \break
1010 @funindex \noBreak
1011
1012 @seealso
1013
1014 Internals: @internalsref{LineBreakEvent}.
1015
1016 A linebreaking configuration can now be saved as a @code{.ly} file
1017 automatically.  This allows vertical alignments to be stretched to
1018 fit pages in a second formatting run.  This is fairly new and
1019 complicated; see @inputfileref{input/regression/,page-layout-twopass.ly}
1020 for details.
1021
1022 @refbugs
1023
1024 Line breaks can only occur if there is a ``proper'' bar line.  A note
1025 which is hanging over a bar line is not proper, such as
1026
1027 @lilypond[quote,ragged-right,relative=2,fragment,verbatim]
1028 c4 c2 c2 \break   % this does nothing
1029 c2 c4 |           % a break here would work
1030 c4 c2 c4 ~ \break % as does this break
1031 c4 c2 c4
1032 @end lilypond
1033
1034 To allow line breaks on such bar lines, the
1035 @code{Forbid_line_break_engraver} can be removed from @code{Voice}
1036 context, like so
1037
1038
1039 @lilypond[quote,ragged-right,relative=2,fragment,verbatim]
1040 \new Voice \with {
1041   \remove "Forbid_line_break_engraver"
1042 } {
1043   c4 c2 c2 \break   % now the break is allowed
1044   c2 c4
1045 }
1046 @end lilypond
1047
1048
1049
1050 @node Page breaking
1051 @subsection Page breaking
1052
1053 The default page breaking may be overriden by inserting
1054 @code{\pageBreak} or @code{\noPageBreak} commands.  These commands are
1055 analogous to @code{\break} and @code{\noBreak}.  They should be
1056 inserted at a bar line.  These commands force and forbid a page-break
1057 from happening.  Of course, the @code{\pageBreak} command also forces
1058 a line break.
1059
1060 Page breaks are computed by the @code{page-breaking} function.
1061 LilyPond provides two algorithms for computing page
1062 breaks, @code{ly:optimal-breaking} and @code{ly:page-turn-breaking}. The
1063 default is @code{ly:optimal-breaking}, but the value can be changed in
1064 the @code{\paper} block:
1065
1066 @example
1067 \paper@{
1068   #(define page-breaking ly:page-turn-breaking)
1069 @}
1070 @end example
1071
1072 The old page breaking algorithm is called
1073 @code{optimal-page-breaks}. If you are having trouble with the new page
1074 breakers, you can enable the old one as a workaround.
1075
1076 @refcommands
1077
1078 @funindex \pageBreak
1079 @code{\pageBreak}
1080 @funindex \noPageBreak
1081 @code{\noPageBreak}
1082
1083
1084 @node Optimal page breaking
1085 @subsection Optimal page breaking
1086
1087 @funindex ly:optimal-breaking
1088
1089 The @code{ly:optimal-breaking} function is LilyPond's default method of
1090 determining page breaks. It attempts to find a page breaking that minimizes
1091 cramping and stretching, both horizontally and vertically. Unlike
1092 @code{ly:page-turn-breaking}, it has no concept of page turns.
1093
1094
1095 @node Optimal page turning
1096 @subsection Optimal page turning
1097
1098 @funindex ly:page-turn-breaking
1099
1100 Often it is necessary to find a page breaking configuration so that there is
1101 a rest at the end of every second page. This way, the musician can turn the
1102 page without having to miss notes. The @code{ly:page-turn-breaking} function
1103 attempts to find a page breaking minimizing cramping and stretching, but with
1104 the additional restriction that it is only allowed to introduce page turns
1105 in specified places.
1106
1107 There are two steps to using this page breaking function. First, you must
1108 enable it in the @code{\paper} block. Then, you must tell the function
1109 where you would like to allow page breaks.
1110
1111 There are two ways to achieve the second step. First, you can specify each
1112 potential page turn manually, by inserting @code{\allowPageTurn} into your
1113 input file at the appropriate places.
1114
1115 If this is too tedious, you can add a @code{Page_turn_engraver} to a Staff or
1116 Voice context. The @code{Page_turn_engraver} will scan the context for
1117 sections without notes (note that it does not scan for rests; it scans for
1118 the absence of notes. This is so that single-staff polyphony with rests in one
1119 of the parts does not throw off the @code{Page_turn_engraver}). When it finds
1120 a sufficiently long section without notes, the @code{Page_turn_engraver} will
1121 insert an @code{\allowPageTurn} at the final barline in that section, unless
1122 there is a ``special'' barline (such as a double bar), in which case the
1123 @code{\allowPageTurn} will be inserted at the final ``special'' barline in
1124 the section.
1125
1126 @funindex minimumPageTurnLength
1127 The @code{Page_turn_engraver} reads the context property
1128 @code{minimumPageTurnLength} to determine how long a note-free section must
1129 be before a page turn is considered. The default value for
1130 @code{minimumPageTurnLength} is @code{#(ly:make-moment 1 1)}. If you want
1131 to disable page turns, you can set it to something very large.
1132
1133 @example
1134 \new Staff \with @{ \consists "Page_turn_engraver" @}
1135 @{
1136   a4 b c d |
1137   R1 | % a page turn will be allowed here
1138   a4 b c d |
1139   \set Staff.minimumPageTurnLength = #(ly:make-moment 5 2)
1140   R1 | % a page turn will not be allowed here
1141   a4 b r2 |
1142   R1*2 | % a page turn will be allowed here
1143   a1
1144 @}
1145 @end example
1146
1147 @funindex minimumRepeatLengthForPageTurn
1148 The @code{Page_turn_engraver} detects volta repeats. It will only allow a page
1149 turn during the repeat if there is enough time at the beginning and end of the
1150 repeat to turn the page back. The @code{Page_turn_engraver} can also disable
1151 page turns if the repeat is very short. If you set the context property
1152 @code{minimumRepeatLengthForPageTurn} then the @code{Page_turn_engraver} will
1153 only allow turns in repeats whose duration is longer than this value.
1154
1155 @refbugs
1156
1157 There should only be one @code{Page_turn_engraver} in a score. If there is more
1158 than one, they will interfere with each other.
1159
1160
1161 @node Displaying spacing
1162 @section Displaying spacing
1163
1164 @funindex annotate-spacing
1165 @cindex Spacing, display of properties
1166
1167 To graphically display the dimensions of vertical properties that may
1168 be altered for page formatting, set @code{annotate-spacing} in the
1169 @code{\paper} block, like this
1170
1171
1172 @lilypond[verbatim]
1173 #(set-default-paper-size "a6" 'landscape)
1174
1175 \book {
1176   \score { { c4 } }
1177   \paper { annotate-spacing = ##t }
1178 }
1179 @end lilypond
1180
1181 @c need to have \book{} otherwise we get  the separate systems. -hwn
1182
1183 @noindent
1184 @c  FIXME: really bad vagueness due to bug in annotate-spacing.  -gp
1185 Some unit dimensions are measured in staff spaces, while others
1186 are measured in millimeters.
1187 The pairs
1188 (@var{a},@var{b}) are intervals, where @var{a} is the lower edge and
1189 @var{b} the upper edge of the interval.
1190
1191
1192 @node Vertical collision avoidance
1193 @section Vertical collision avoidance
1194
1195 @funindex outside-staff-priority
1196 @funindex outside-staff-padding
1197 @funindex outside-staff-horizontal-padding
1198
1199 Intuitively, there are some objects in musical notation that belong
1200 to the staff and there are other objects that should be placed outside
1201 the staff.  Objects belonging outside the staff include things such as
1202 rehearsal marks, text and dynamic markings (from now on, these will
1203 be called outside-staff objects).  LilyPond's rule for the
1204 vertical placement of outside-staff objects is to place them as close
1205 to the staff as possible but not so close that they collide with
1206 another object.
1207
1208 LilyPond uses the @code{outside-staff-priority} property to determine
1209 whether a grob is an outside-staff object: if @code{outside-staff-priority}
1210 is a number, the grob is an outside-staff object.  In addition,
1211 @code{outside-staff-priority} tells LilyPond in which order the objects
1212 should be placed.
1213
1214 First, LilyPond places all the objects that do not belong outside
1215 the staff.  Then it sorts the outside-staff objects according to their
1216 @code{outside-staff-priority} (in increasing order).  One by one, LilyPond
1217 takes the outside-staff objects and places them so that they do
1218 not collide with any objects that have already been placed.  That
1219 is, if two outside-staff grobs are competing for the same space, the one
1220 with the lower @code{outside-staff-priority} will be placed closer to
1221 the staff.
1222
1223 @lilypond[quote,ragged-right,relative=2,fragment,verbatim]
1224 c4_"Text"\pp
1225 r2.
1226 \once \override TextScript #'outside-staff-priority = #1
1227 c4_"Text"\pp % this time the text will be closer to the staff
1228 r2.
1229 % by setting outside-staff-priority to a non-number, we
1230 % disable the automatic collision avoidance
1231 \once \override TextScript #'outside-staff-priority = ##f
1232 \once \override DynamicLineSpanner #'outside-staff-priority = ##f
1233 c4_"Text"\pp % now they will collide
1234 @end lilypond
1235
1236 The vertical padding between an outside-staff object and the
1237 previously-positioned grobs can be controlled with
1238 @code{outside-staff-padding}.
1239
1240 @lilypond[quote,ragged-right,relative=2,fragment,verbatim]
1241 \once \override TextScript #'outside-staff-padding = #0
1242 a'^"This text is placed very close to the note"
1243 \once \override TextScript #'outside-staff-padding = #3
1244 c^"This text is padded away from the previous text"
1245 c^"This text is placed close to the previous text"
1246 @end lilypond
1247
1248 By default, outside-staff objects are placed without regard to
1249 their horizontal distance from the previously-posititioned grobs.  This
1250 can lead to situations in which objects are placed very close to each
1251 other horizontally.  Setting @code{outside-staff-horizontal-padding}
1252 causes an object to be offset vertically so that such a situation
1253 doesn't occur.
1254
1255 @lilypond[quote,ragged-right,relative=2,fragment,verbatim]
1256 % the markup is too close to the following note
1257 c2^"Text"
1258 c''2
1259 % setting outside-staff-horizontal-padding fixes this
1260 R1
1261 \once \override TextScript #'outside-staff-horizontal-padding = #1
1262 c,,2^"Text"
1263 c''2
1264 @end lilypond