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