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