1 @c -*- coding: utf-8; mode: texinfo; -*-
2 @node Working on LilyPond projects
3 @chapter Working on LilyPond projects
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
12 * Suggestions for writing LilyPond files::
13 * Saving typing with identifiers and functions::
15 * Updating old files::
16 * Troubleshooting (taking it all apart)::
20 @node Suggestions for writing LilyPond files
21 @section Suggestions for writing LilyPond files
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
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.
32 @item What if you make a mistake? The structure of a lilypond
33 file can make certain errors easier (or harder) to find.
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
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.
50 * General suggestions::
51 * Typesetting existing music::
56 @node General suggestions
57 @subsection General suggestions
59 Here are a few suggestions that can help you to avoid or fix
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.
71 @item @strong{Include checks}: @ref{Bar check} and @ref{Octave check}. If
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.
78 @item @strong{One bar per line of text}. If there is anything complicated,
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.
84 @item @strong{Comment your files}. Use either bar numbers (every so often)
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
91 challenging to determine your intentions or how your file is structured if
92 you didn't comment the file.
94 @item @strong{Indent your braces}. A lot of problems are caused by an
96 in the number of @code{@{} and @code{@}}.
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.
103 @item @strong{Separate tweaks} from music definitions. See
104 @ref{Saving typing with identifiers and functions} and
110 @node Typesetting existing music
111 @subsection Typesetting existing music
113 If you are entering music from an existing score (i.e., typesetting a
114 piece of existing sheet music),
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}.
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
136 @subsection Large projects
138 When working on a large project, having a clear structure to your
139 lilypond files becomes vital.
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.
150 violin = \relative c'' @{
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}.
172 \dynamic f \italic \small @{ 2nd @} \hspace #0.1 \dynamic p @}
173 violin = \relative c'' @{
181 @node Saving typing with identifiers and functions
182 @section Saving typing with identifiers and functions
184 By this point, you've seen this kind of thing:
186 @lilypond[quote,verbatim,ragged-right]
187 hornNotes = \relative c'' { c4 b dis c }
195 You may even realize that this could be useful in minimalist music:
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 }
208 However, you can also use these identifiers (also known as
209 variables, macros, or (user-defined) command) for tweaks:
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'' {
217 c4._\dolce b8 a8 g a b |
219 c4.^"hi there!" d8 e' f g d |
220 c,4.\fthenp b8 c4 c-. |
227 \layout{ragged-right=##t}
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
239 violin = \relative c'' @{
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-. |
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
255 LilyPond can handle non-static substitution, too (you
256 can think of these as functions).
258 @lilypond[quote,verbatim,ragged-right]
260 #(define-music-function (parser location padding) (number?)
262 \once \override TextScript #'padding = #$padding
270 c4^"piu mosso" fis a g
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.
283 @section Style sheets
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
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}.
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?)
300 \once \override Score . RehearsalMark #'self-alignment-X = #left
301 \once \override Score . RehearsalMark #'extra-spacing-width = #'(+inf.0 . -inf.0)
302 \mark \markup { \bold $markp }
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
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:
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?)
328 \once \override Score . RehearsalMark #'self-alignment-X = #left
329 \once \override Score . RehearsalMark #'extra-spacing-width = #'(+inf.0 . -inf.0)
330 \mark \markup @{ \bold $markp @}
334 Now let's modify our music (let's save this file as @file{"music.ly"}).
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.
340 \include "definitions.ly"
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
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?)
356 \once \override Score . RehearsalMark #'self-alignment-X = #left
357 \once \override Score . RehearsalMark #'extra-spacing-width = #'(+inf.0 . -inf.0)
358 \mark \markup { \bold $markp }
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
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.
376 Don't change @file{music.ly}, though. Replace our @file{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?)
385 \once \override Score . RehearsalMark #'self-alignment-X = #left
386 \once \override Score . RehearsalMark #'extra-spacing-width = #'(+inf.0 . -inf.0)
387 \mark \markup @{ \bold $markp @}
392 \override MetronomeMark #'extra-offset = #'(-9 . 0)
393 \override MetronomeMark #'padding = #'3
396 \override TimeSignature #'style = #'numbered
399 \override Glissando #'thickness = #3
400 \override Glissando #'gap = #0.1
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?)
410 \once \override Score . RehearsalMark #'self-alignment-X = #left
411 \once \override Score . RehearsalMark #'extra-spacing-width = #'(+inf.0 . -inf.0)
412 \mark \markup { \bold $markp }
417 \override MetronomeMark #'extra-offset = #'(-9 . 0)
418 \override MetronomeMark #'padding = #'3
421 \override TimeSignature #'style = #'numbered
424 \override Glissando #'thickness = #3
425 \override Glissando #'gap = #0.1
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
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.
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?)
452 \once \override Score . RehearsalMark #'self-alignment-X = #left
453 \once \override Score . RehearsalMark #'extra-spacing-width = #'(+inf.0 . -inf.0)
454 \mark \markup @{ \bold $markp @}
457 #(set-global-staff-size 23)
460 \override MetronomeMark #'extra-offset = #'(-9 . 0)
461 \override MetronomeMark #'padding = #'3
466 \override Glissando #'thickness = #3
467 \override Glissando #'gap = #0.1
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?)
477 \once \override Score . RehearsalMark #'self-alignment-X = #left
478 \once \override Score . RehearsalMark #'extra-spacing-width = #'(+inf.0 . -inf.0)
479 \mark \markup { \bold $markp }
482 #(set-global-staff-size 23)
485 \override MetronomeMark #'extra-offset = #'(-9 . 0)
486 \override MetronomeMark #'padding = #'3
489 \override Glissando #'thickness = #3
490 \override Glissando #'gap = #0.1
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
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:
514 \include "definitions.ly"
516 %%% Only uncomment one of these two lines!
517 \include "web-publish.ly"
518 %\include "university.ly"
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
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"
537 @node Updating old files
538 @section Updating old files
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.
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}.
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}.
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.
565 @node Troubleshooting (taking it all apart)
566 @section Troubleshooting (taking it all apart)
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.
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.
583 In an extreme case, you might end up with only
597 (in other words, a file without any music)
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.
605 bass = \relative c' @{
613 Now start slowly uncommenting more and more of the
614 @code{bass} part until you find the problem line.