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