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