]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/user/working.itely
Typo.
[lilypond.git] / Documentation / user / working.itely
1 @c -*- coding: utf-8; mode: texinfo; -*-
2 @c This file is part of lilypond-learning.tely
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.  See TRANSLATION for details.
8 @end ignore
9
10 @node Working on LilyPond projects
11 @chapter Working on LilyPond projects
12
13 This section explains how to solve or avoid certain common
14 problems.  If you have programming experience, many of these
15 tips may seem obvious, but it is still advisable to read
16 this chapter.
17
18
19 @menu
20 * Suggestions for writing LilyPond files::  
21 * When things don't work::      
22 @end menu
23
24
25 @node Suggestions for writing LilyPond files
26 @section Suggestions for writing LilyPond files
27
28 Now you're ready to begin writing larger LilyPond files -- not just the
29 little examples in the tutorial, but whole pieces.  But how should you
30 go about doing it?
31
32 As long as LilyPond can understand your files and produces the output
33 that you want, it doesn't matter what your files look like.  However,
34 there are a few other things to consider when writing lilypond files.
35
36 @itemize
37 @item What if you make a mistake?  The structure of a lilypond
38 file can make certain errors easier (or harder) to find.
39
40 @item What if you want to share your files with somebody
41 else?  In fact, what if you want to alter your own files in
42 a few years?  Some lilypond files are understandable at
43 first glance; other files may leave you scratching your head
44 for an hour.
45
46 @item What if you want to upgrade your lilypond file for use
47 with a later version of lilypond?  The input syntax changes
48 occasionally as lilypond improves.  Most changes can be
49 done automatically with @code{convert-ly}, but some changes
50 might require manual assistance.  Lilypond files can be
51 structured in order to be easier (or harder) to update.
52 @end itemize
53
54 @menu
55 * General suggestions::         
56 * Typesetting existing music::  
57 * Large projects::              
58 * Saving typing with variables and functions::  
59 * Style sheets::                
60 @end menu
61
62
63 @node General suggestions
64 @subsection General suggestions
65
66 Here are a few suggestions that can help you to avoid or fix
67 problems:
68
69 @itemize
70 @item @strong{Include @code{\version} numbers in every file}.  Note that all
71 templates contain @code{\version} information.  We
72 highly recommend that you always include the @code{\version}, no matter
73 how small your file is.  Speaking from personal experience, it's
74 quite frustrating to try to remember which version of LilyPond you were
75 using a few years ago.  @code{convert-ly} requires you to declare
76 which version of LilyPond you used.
77
78 @item @strong{Include checks}: @ruser{Bar and barnumber checks},
79 @ruser{Octave check}.  If you include checks every so often, then
80 if you make a mistake, you can pinpoint it quicker.  How often is
81 @q{every so often}?  It depends on the complexity of the music.
82 For very simple music, perhaps just once or twice.  For very
83 complex music, perhaps every bar.
84
85 @item @strong{One bar per line of text}.  If there is anything complicated,
86 either in the music
87 itself or in the output you desire, it's often good to write only one bar
88 per line.  Saving screen space by cramming eight bars per line just isn't
89 worth it if you have to @q{debug} your files.
90
91 @item @strong{Comment your files}.  Use either bar numbers (every so often)
92 or
93 references to musical themes (@q{second theme in violins,} @q{fourth
94 variation,} etc.).  You may not need comments when you're writing the piece
95 for the first time, but if you want to go back to change something two or
96 three years later, or if you pass the source over to a friend, it will
97 be much more
98 challenging to determine your intentions or how your file is structured if
99 you didn't comment the file.
100
101 @item @strong{Indent your braces}.  A lot of problems are caused by an
102 imbalance
103 in the number of @code{@{} and @code{@}}.
104
105 @item @strong{Explicitly add durations} at the beginnings of sections
106 and variables.  If you specify @code{c4 d e} at the beginning of a
107 phrase (instead of just @code{c d e}) you can save yourself some
108 problems if you rearrange your music later.
109
110 @item @strong{Separate tweaks} from music definitions.  See
111 @ref{Saving typing with variables and functions}, and
112 @ref{Style sheets}.
113
114 @end itemize
115
116
117 @node Typesetting existing music
118 @subsection Typesetting existing music
119
120 If you are entering music from an existing score (i.e., typesetting a
121 piece of existing sheet music),
122
123 @itemize
124
125 @item Enter one manuscript (the physical copy) system at a time (but still
126 only one bar per line of text), and
127 check each system when you finish it.  You may use the
128 @code{showLastLength} command to speed up processing -- see
129 @ruser{Skipping corrected music}.
130
131 @item Define @code{mBreak = @{ \break @}} and insert @code{\mBreak}
132 in the input file whenever the manuscript has a line break.  This
133 makes it much easier to compare the LilyPond music to the original
134 music.  When you are finished proofreading your score, you may
135 define @code{mBreak = @{ @}} to remove all those line breaks.  This
136 will allow LilyPond to place line breaks wherever it feels are
137 best.
138
139 @end itemize
140
141
142 @node Large projects
143 @subsection Large projects
144
145 When working on a large project, having a clear structure to your
146 lilypond files becomes vital.
147
148 @itemize
149
150 @item @strong{Use an variable for each voice}, with a minimum of
151 structure inside the definition.  The structure of the
152 @code{\score} section is the most likely thing to change;
153 the @code{violin} definition is extremely unlikely to change
154 in a new version of LilyPond.
155
156 @example
157 violin = \relative c'' @{
158 g4 c'8. e16
159 @}
160 ...
161 \score @{
162   \new GrandStaff @{
163     \new Staff @{
164       \violin
165     @}
166   @}
167 @}
168 @end example
169
170 @item @strong{Separate tweaks from music definitions}.  This
171 point was made in previously, but for large
172 projects it is absolutely vital.  We might need to change
173 the definition of @code{fthenp}, but then we only need
174 to do this once, and we can still avoid touching anything
175 inside @code{violin}.
176
177 @example
178 fthenp = _\markup@{
179   \dynamic f \italic \small @{ 2nd @} \hspace #0.1 \dynamic p @}
180 violin = \relative c'' @{
181 g4\fthenp c'8. e16
182 @}
183 @end example
184
185 @end itemize
186
187
188 @node Saving typing with variables and functions
189 @subsection Saving typing with variables and functions
190
191 @cindex variables
192 @cindex variables
193
194 By this point, you've seen this kind of thing:
195
196 @lilypond[quote,verbatim,ragged-right]
197 hornNotes = \relative c'' { c4 b dis c }
198 \score {
199   {
200     \hornNotes
201   }
202 }
203 @end lilypond
204
205 You may even realize that this could be useful in minimalist music:
206
207 @lilypond[quote,verbatim,ragged-right]
208 fragA = \relative c'' { a4 a8. b16 }
209 fragB = \relative c'' { a8. gis16 ees4 }
210 violin = \new Staff { \fragA \fragA \fragB \fragA }
211 \score {
212   {
213     \violin
214   }
215 }
216 @end lilypond
217
218 However, you can also use these variables (also known as
219 variables, macros, or (user-defined) command) for tweaks:
220
221 @lilypond[quote,verbatim,ragged-right]
222 dolce = \markup{ \italic \bold dolce }
223 padText = { \once \override TextScript #'padding = #5.0 }
224 fthenp=_\markup{ \dynamic f \italic \small { 2nd } \hspace #0.1 \dynamic p }
225 violin = \relative c'' {
226   \repeat volta 2 {
227     c4._\dolce b8 a8 g a b |
228     \padText
229     c4.^"hi there!" d8 e' f g d |
230     c,4.\fthenp b8 c4 c-. |
231   }
232 }
233 \score {
234   {
235     \violin
236   }
237 \layout{ragged-right=##t}
238 }
239 @end lilypond
240
241 These variables are obviously useful for saving
242 typing.  But they're worth considering even if you
243 only use them once -- they reduce complexity.  Let's
244 look at the previous example without any
245 variables.  It's a lot harder to read, especially
246 the last line.
247
248 @example
249 violin = \relative c'' @{
250   \repeat volta 2 @{
251     c4._\markup@{ \italic \bold dolce @} b8 a8 g a b |
252     \once \override TextScript #'padding = #5.0
253     c4.^"hi there!" d8 e' f g d |
254     c,4.\markup@{ \dynamic f \italic \small @{ 2nd @}
255       \hspace #0.1 \dynamic p @} b8 c4 c-. |
256   @}
257 @}
258 @end example
259
260 So far we've seen static substitution -- when LilyPond
261 sees @code{\padText}, it replaces it with the stuff that
262 we've defined it to be (ie the stuff to the right of
263 @code{padtext=}).
264
265 LilyPond can handle non-static substitution, too (you
266 can think of these as functions).
267
268 @lilypond[quote,verbatim,ragged-right]
269 padText =
270 #(define-music-function (parser location padding) (number?)
271   #{
272     \once \override TextScript #'padding = #$padding
273   #})
274
275 \relative c''' {
276   c4^"piu mosso" b a b
277   \padText #1.8
278   c4^"piu mosso" d e f
279   \padText #2.6
280   c4^"piu mosso" fis a g
281 }
282 @end lilypond
283
284 Using variables is also a good way to reduce work if the
285 LilyPond input syntax changes (see @ref{Updating old files}).  If
286 you have a single definition (such as @code{\dolce}) for all your
287 files (see @ref{Style sheets}), then if the syntax changes, you
288 only need to update your single @code{\dolce} definition,
289 instead of making changes throughout every @code{.ly} file.
290
291
292 @node Style sheets
293 @subsection Style sheets
294
295 The output that LilyPond produces can be heavily modified; see
296 @ref{Tweaking output}, for details.  But what if you have many
297 files that you want to apply your tweaks to?  Or what if you
298 simply want to separate your tweaks from the actual music?  This
299 is quite easy to do.
300
301 Let's look at an example.  Don't worry if you don't understand
302 the parts with all the @code{#()}.  This is explained in
303 @ref{Advanced tweaks with Scheme}.
304
305 @lilypond[quote,verbatim,ragged-right]
306 mpdolce = #(make-dynamic-script (markup #:hspace 1 #:translate (cons 5 0)
307   #:line(#:dynamic "mp" #:text #:italic "dolce" )))
308 tempoMark = #(define-music-function (parser location markp) (string?)
309 #{
310   \once \override Score . RehearsalMark #'self-alignment-X = #left
311   \once \override Score . RehearsalMark #'extra-spacing-width = #'(+inf.0 . -inf.0)
312   \mark \markup { \bold $markp }
313 #})
314
315 \relative c'' {
316   \tempo 4=50
317   a4.\mpdolce d8 cis4--\glissando a | b4 bes a2
318   \tempoMark "Poco piu mosso"
319   cis4.\< d8 e4 fis | g8(\! fis)-. e( d)-. cis2
320 }
321 @end lilypond
322
323 There are some problems with overlapping output; we'll fix those using
324 the techniques in @ref{Moving objects}.  But let's also
325 do something about the @code{mpdolce} and @code{tempoMark}
326 definitions.  They produce the output we desire, but we might want
327 to use them in another piece.  We could simply copy-and-paste them
328 at the top of every file, but that's an annoyance.  It also leaves
329 those definitions in our music files, and I personally find all
330 the @code{#()} somewhat ugly.  Let's hide them in another file:
331
332 @example
333 %%% save this to a file called "definitions.ly"
334 mpdolce = #(make-dynamic-script (markup #:hspace 1 #:translate (cons 5 0)
335   #:line(#:dynamic "mp" #:text #:italic "dolce" )))
336 tempoMark = #(define-music-function (parser location markp) (string?)
337 #@{
338   \once \override Score . RehearsalMark #'self-alignment-X = #left
339   \once \override Score . RehearsalMark #'extra-spacing-width = #'(+inf.0 . -inf.0)
340   \mark \markup @{ \bold $markp @}
341 #@})
342 @end example
343
344 Now let's modify our music (let's save this file as @file{"music.ly"}).
345
346 @c  We have to do this awkward example/lilypond-non-verbatim
347 @c  because we can't do the \include stuff in the manual.
348
349 @example
350 \include "definitions.ly"
351
352 \relative c'' @{
353   \tempo 4=50
354   a4.\mpdolce d8 cis4--\glissando a | b4 bes a2
355   \once \override Score.RehearsalMark #'padding = #2.0
356   \tempoMark "Poco piu mosso"
357   cis4.\< d8 e4 fis | g8(\! fis)-. e( d)-. cis2
358 @}
359 @end example
360
361 @lilypond[quote,ragged-right]
362 mpdolce = #(make-dynamic-script (markup #:hspace 1 #:translate (cons 5 0)
363   #:line(#:dynamic "mp" #:text #:italic "dolce" )))
364 tempoMark = #(define-music-function (parser location markp) (string?)
365 #{
366   \once \override Score . RehearsalMark #'self-alignment-X = #left
367   \once \override Score . RehearsalMark #'extra-spacing-width = #'(+inf.0 . -inf.0)
368   \mark \markup { \bold $markp }
369 #})
370
371 \relative c'' {
372   \tempo 4=50
373   a4.\mpdolce d8 cis4--\glissando a | b4 bes a2
374   \once \override Score.RehearsalMark #'padding = #2.0
375   \tempoMark "Poco piu mosso"
376   cis4.\< d8 e4 fis | g8(\! fis)-. e( d)-. cis2
377 }
378 @end lilypond
379
380 That looks better, but let's make a few changes.  The glissando is hard
381 to see, so let's make it thicker and closer to the noteheads.  Let's
382 put the metronome marking above the clef, instead of over the first
383 note.  And finally, my composition professor hates @q{C} time signatures,
384 so we'd better make that @q{4/4} instead.
385
386 Don't change @file{music.ly}, though.  Replace our @file{definitions.ly}
387 with this:
388
389 @example
390 %%%  definitions.ly
391 mpdolce = #(make-dynamic-script (markup #:hspace 1 #:translate (cons 5 0)
392   #:line( #:dynamic "mp" #:text #:italic "dolce" )))
393 tempoMark = #(define-music-function (parser location markp) (string?)
394 #@{
395   \once \override Score . RehearsalMark #'self-alignment-X = #left
396   \once \override Score . RehearsalMark #'extra-spacing-width = #'(+inf.0 . -inf.0)
397   \mark \markup @{ \bold $markp @}
398 #@})
399
400 \layout@{
401   \context @{ \Score
402     \override MetronomeMark #'extra-offset = #'(-9 . 0)
403     \override MetronomeMark #'padding = #'3
404   @}
405   \context @{ \Staff
406     \override TimeSignature #'style = #'numbered
407   @}
408   \context @{ \Voice
409     \override Glissando #'thickness = #3
410     \override Glissando #'gap = #0.1
411   @}
412 @}
413 @end example
414
415 @lilypond[quote,ragged-right]
416 mpdolce = #(make-dynamic-script (markup #:hspace 1 #:translate (cons 5 0)
417   #:line( #:dynamic "mp" #:text #:italic "dolce" )))
418 tempoMark = #(define-music-function (parser location markp) (string?)
419 #{
420   \once \override Score . RehearsalMark #'self-alignment-X = #left
421   \once \override Score . RehearsalMark #'extra-spacing-width = #'(+inf.0 . -inf.0)
422   \mark \markup { \bold $markp }
423 #})
424
425 \layout{
426   \context { \Score
427     \override MetronomeMark #'extra-offset = #'(-9 . 0)
428     \override MetronomeMark #'padding = #'3
429   }
430   \context { \Staff
431     \override TimeSignature #'style = #'numbered
432   }
433   \context { \Voice
434     \override Glissando #'thickness = #3
435     \override Glissando #'gap = #0.1
436   }
437 }
438
439 \relative c'' {
440   \tempo 4=50
441   a4.\mpdolce d8 cis4--\glissando a | b4 bes a2
442   \once \override Score.RehearsalMark #'padding = #2.0
443   \tempoMark "Poco piu mosso"
444   cis4.\< d8 e4 fis | g8(\! fis)-. e( d)-. cis2
445 }
446 @end lilypond
447
448 That looks nicer!  But now suppose that I want to publish this
449 piece.  My composition professor doesn't like @q{C} time
450 signatures, but I'm somewhat fond of them.  Let's copy the
451 current @file{definitions.ly} to @file{web-publish.ly} and
452 modify that.  Since this music is aimed at producing a pdf which
453 will be displayed on the screen, we'll also increase the
454 overall size of the output.
455
456 @example
457 %%%  definitions.ly
458 mpdolce = #(make-dynamic-script (markup #:hspace 1 #:translate (cons 5 0)
459   #:line( #:dynamic "mp" #:text #:italic "dolce" )))
460 tempoMark = #(define-music-function (parser location markp) (string?)
461 #@{
462   \once \override Score . RehearsalMark #'self-alignment-X = #left
463   \once \override Score . RehearsalMark #'extra-spacing-width = #'(+inf.0 . -inf.0)
464   \mark \markup @{ \bold $markp @}
465 #@})
466
467 #(set-global-staff-size 23)
468 \layout@{
469   \context @{ \Score
470     \override MetronomeMark #'extra-offset = #'(-9 . 0)
471     \override MetronomeMark #'padding = #'3
472   @}
473   \context @{ \Staff
474   @}
475   \context @{ \Voice
476     \override Glissando #'thickness = #3
477     \override Glissando #'gap = #0.1
478   @}
479 @}
480 @end example
481
482 @lilypond[quote,ragged-right]
483 mpdolce = #(make-dynamic-script (markup #:hspace 1 #:translate (cons 5 0)
484   #:line( #:dynamic "mp" #:text #:italic "dolce" )))
485 tempoMark = #(define-music-function (parser location markp) (string?)
486 #{
487   \once \override Score . RehearsalMark #'self-alignment-X = #left
488   \once \override Score . RehearsalMark #'extra-spacing-width = #'(+inf.0 . -inf.0)
489   \mark \markup { \bold $markp }
490 #})
491
492 #(set-global-staff-size 23)
493 \layout{
494   \context { \Score
495     \override MetronomeMark #'extra-offset = #'(-9 . 0)
496     \override MetronomeMark #'padding = #'3
497   }
498   \context { \Voice
499     \override Glissando #'thickness = #3
500     \override Glissando #'gap = #0.1
501   }
502 }
503
504 \relative c'' {
505   \tempo 4=50
506   a4.\mpdolce d8 cis4--\glissando a | b4 bes a2
507   \once \override Score.RehearsalMark #'padding = #2.0
508   \tempoMark "Poco piu mosso"
509   cis4.\< d8 e4 fis | g8(\! fis)-. e( d)-. cis2
510 }
511 @end lilypond
512
513 Now in our music, I simply replace
514 @code{\include "definitions.ly"} with
515 @code{\include "web-publish.ly"}.  Of course, we could make this
516 even more convenient.  We could make a @file{definitions.ly} file which
517 contains only the definitions of @code{mpdolce} and @code{tempoMark}, a
518 @file{web-publish.ly} file which contains only the @code{\layout}
519 section listed above, and a @file{university.ly} file which
520 contains only the tweaks to produce the output that my professor
521 prefers.  The top of @file{music.ly} would then look like this:
522
523 @example
524 \include "definitions.ly"
525
526 %%%  Only uncomment one of these two lines!
527 \include "web-publish.ly"
528 %\include "university.ly"
529 @end example
530
531 This approach can be useful even if you are only producing
532 one set of parts.  I use half a dozen different
533 @q{style sheet} files for my projects.  I begin every music
534 file with @code{\include "../global.ly"}, which contains
535
536 @example
537 %%%   global.ly
538 \version "2.11.23"
539 #(ly:set-option 'point-and-click #f)
540 \include "../init/init-defs.ly"
541 \include "../init/init-layout.ly"
542 \include "../init/init-headers.ly"
543 \include "../init/init-paper.ly"
544 @end example
545
546
547 @node When things don't work
548 @section When things don't work
549
550 @menu
551 * Updating old files::          
552 * Troubleshooting (taking it all apart)::  
553 * Minimal examples::            
554 @end menu
555
556 @node Updating old files
557 @subsection Updating old files
558
559 The LilyPond input syntax occasionally changes.  As LilyPond itself
560 improves, the syntax (input language) is modified accordingly.  Sometimes
561 these changes are made to make the input easier to read and write or
562 sometimes the changes are made to accomodate new features of LilyPond.
563
564 LilyPond comes with a file that makes this updating easier:
565 @code{convert-ly}.  For details about how to run this program, see
566 @rprogram{Updating files with convert-ly}.
567
568 Unfortunately, @code{convert-ly} cannot handle all input changes.  It
569 takes care of simple search-and-replace changes (such as @code{raggedright}
570 becoming @code{ragged-right}), but some changes are too
571 complicated.  The syntax changes that @code{convert-ly} cannot handle
572 are listed in @rprogram{Updating files with convert-ly}.
573
574 For example, in LilyPond 2.4 and earlier, accents and non-English
575 letters were entered using LaTeX -- for example,
576 @samp{No\"el} (this would print the French word for
577 @q{Christmas}).  In LilyPond 2.6 and above, the special
578 @samp{ë} must be entered directly into the LilyPond file as an
579 UTF-8 character.  @code{convert-ly} cannot change all the LaTeX
580 special characters into UTF-8 characters; you must manually update
581 your old LilyPond files.
582
583
584 @node Troubleshooting (taking it all apart)
585 @subsection Troubleshooting (taking it all apart)
586
587 Sooner or later, you will write a file that LilyPond cannot
588 compile.  The messages that LilyPond gives may help
589 you find the error, but in many cases you need to do some
590 investigation to determine the source of the problem.
591
592 The most powerful tools for this purpose are the
593 single line comment (indicated by @code{%}) and the block
594 comment (indicated by @code{%@{ ... %@}}).  If you don't
595 know where a problem is, start commenting out huge portions
596 of your input file.  After you comment out a section, try
597 compiling the file again.  If it works, then the problem
598 must exist in the portion you just commented.  If it doesn't
599 work, then keep on commenting out material until you have
600 something that works.
601
602 In an extreme case, you might end up with only
603
604 @example
605 \score @{
606   <<
607     % \melody
608     % \harmony
609     % \bass
610   >>
611   \layout@{@}
612 @}
613 @end example
614
615 @noindent
616 (in other words, a file without any music)
617
618 If that happens, don't give up.  Uncomment a bit -- say,
619 the bass part -- and see if it works.  If it doesn't work,
620 then comment out all of the bass music (but leave
621 @code{\bass} in the @code{\score} uncommented.
622
623 @example
624 bass = \relative c' @{
625 %@{
626   c4 c c c
627   d d d d
628 %@}
629 @}
630 @end example
631
632 Now start slowly uncommenting more and more of the
633 @code{bass} part until you find the problem line.
634
635 Another very useful debugging technique is constructing
636 @ref{Minimal examples}.
637
638
639 @node Minimal examples
640 @subsection Minimal examples
641
642 A minimal example is an example which is as small as possible.  These
643 examples are much easier to understand than long examples.  Minimal
644 examples are used for
645
646 @itemize
647 @item Bug reports
648 @item Sending a help request to mailists
649 @item Adding an example to the @uref{http://lsr@/.dsi@/.unimi@/.it/,LilyPond
650 Snippet Repository}
651 @end itemize
652
653 To construct an example which is as small as possible, the rule is
654 quite simple: remove anything which is not necessary.  When trying to
655 remove unnecessary parts of a file, it is a very good idea to comment
656 out lines instead of deleting them.  That way, if you discover that you
657 actually @emph{do} need some lines, you can uncomment them, instead of
658 typing them in from scratch.
659
660 There are two exceptions to the @qq{as small as possible} rule:
661
662 @itemize
663 @item Include the @code{\version} number.
664 @item If possible, use @code{\paper@{ ragged-right=##t @}} at the
665 top of your example.
666 @end itemize
667
668 The whole point of a minimal example is to make it easy to read:
669
670 @itemize
671 @item Avoid using complicated notes, keys, or time signatures, unless you
672 wish to demonstrate something is about the behavior of those items.
673 @item Do not use @code{\override} commands unless that is the point of the
674 example.
675 @end itemize
676
677