1 @c -*- coding: utf-8; mode: texinfo; -*-
2 @c This file is part of lilypond-learning.tely
4 Translation of GIT committish: FILL-IN-HEAD-COMMITTISH
6 When revising a translation, copy the HEAD committish of the
7 version that you are working on. See TRANSLATION for details.
12 @node Working on LilyPond projects
13 @chapter Working on LilyPond projects
15 This section explains how to solve or avoid certain common
16 problems. If you have programming experience, many of these
17 tips may seem obvious, but it is still advisable to read
22 * Suggestions for writing LilyPond input files::
23 * When things don't work::
28 @node Suggestions for writing LilyPond input files
29 @section Suggestions for writing LilyPond input files
31 Now you're ready to begin writing larger LilyPond input files --
32 not just the little examples in the tutorial, but whole pieces.
33 But how should you go about doing it?
35 As long as LilyPond can understand your input files and produce
36 the output that you want, it doesn't matter what your input files
37 look like. However, there are a few other things to consider when
38 writing LilyPond input files.
41 @item What if you make a mistake? The structure of a LilyPond
42 file can make certain errors easier (or harder) to find.
44 @item What if you want to share your input files with somebody
45 else? In fact, what if you want to alter your own input files in
46 a few years? Some LilyPond input files are understandable at
47 first glance; others may leave you scratching your head
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 input files can be
55 structured in order to be easier (or harder) to update.
60 * General suggestions::
61 * Typesetting existing music::
63 * Saving typing with variables and functions::
68 @node General suggestions
69 @subsection General suggestions
71 Here are a few suggestions that can help you to avoid or fix
75 @item @strong{Include @code{\version} numbers in every file}. Note that all
76 templates contain @code{\version} information. We
77 highly recommend that you always include the @code{\version}, no matter
78 how small your file is. Speaking from personal experience, it's
79 quite frustrating to try to remember which version of LilyPond you were
80 using a few years ago. @command{convert-ly} requires you to declare
81 which version of LilyPond you used.
83 @item @strong{Include checks}: @ruser{Bar and bar number checks},
84 @ruser{Octave checks}. If you include checks every so often, then
85 if you make a mistake, you can pinpoint it quicker. How often is
86 @q{every so often}? It depends on the complexity of the music.
87 For very simple music, perhaps just once or twice. For very
88 complex music, perhaps every bar.
90 @item @strong{One bar per line of text}. If there is anything complicated,
92 itself or in the output you desire, it's often good to write only one bar
93 per line. Saving screen space by cramming eight bars per line just isn't
94 worth it if you have to @q{debug} your input files.
96 @item @strong{Comment your input files}. Use either bar numbers
98 references to musical themes (@q{second theme in violins,} @q{fourth
99 variation,} etc.). You may not need comments when you're writing the piece
100 for the first time, but if you want to go back to change something two or
101 three years later, or if you pass the source over to a friend, it will
103 challenging to determine your intentions or how your file is structured if
104 you didn't comment the file.
106 @item @strong{Indent your braces}. A lot of problems are caused by an
108 in the number of @code{@{} and @code{@}}.
110 @item @strong{Explicitly add durations} at the beginnings of sections
111 and variables. If you specify @code{c4 d e} at the beginning of a
112 phrase (instead of just @code{c d e}) you can save yourself some
113 problems if you rearrange your music later.
115 @item @strong{Separate tweaks} from music definitions. See
116 @ref{Saving typing with variables and functions}, and
122 @node Typesetting existing music
123 @subsection Typesetting existing music
125 If you are entering music from an existing score (i.e., typesetting a
126 piece of existing sheet music),
130 @item Enter one manuscript (the physical copy) system at a time (but still
131 only one bar per line of text), and
132 check each system when you finish it. You may use the
133 @code{showLastLength} or @code{showFirstLength} properties to speed up
134 processing -- see @ruser{Skipping corrected music}.
136 @item Define @code{mBreak = @{ \break @}} and insert @code{\mBreak}
137 in the input file whenever the manuscript has a line break. This
138 makes it much easier to compare the LilyPond music to the original
139 music. When you are finished proofreading your score, you may
140 define @code{mBreak = @{ @}} to remove all those line breaks. This
141 will allow LilyPond to place line breaks wherever it feels are
144 @item When entering a part for a transposing instrument into a
145 variable, it is recommended that the notes are wrapped in
148 \transpose c natural-pitch @{...@}
150 (where @code{natural-pitch} is the open pitch of the instrument) so
151 that the music in the variable is effectively in C. You can transpose
152 it back again when the variable is used, if required, but you might
153 not want to (e.g., when printing a score in concert pitch,
154 converting a trombone part from treble to bass clef, etc.)
155 Mistakes in transpositions are less likely if all the music in
156 variables is at a consistent pitch.
158 Also, only ever transpose to/from C. That means that the only other
159 keys you will use are the natural pitches of the instruments - bes
160 for a B-flat trumpet, aes for an A-flat clarinet, etc.
166 @subsection Large projects
168 When working on a large project, having a clear structure to your
169 lilypond input files becomes vital.
173 @item @strong{Use an variable for each voice}, with a minimum of
174 structure inside the definition. The structure of the
175 @code{\score} section is the most likely thing to change;
176 the @code{violin} definition is extremely unlikely to change
177 in a new version of LilyPond.
180 violin = \relative c'' @{
193 @item @strong{Separate tweaks from music definitions}. This
194 point was made in previously, but for large
195 projects it is absolutely vital. We might need to change
196 the definition of @code{fthenp}, but then we only need
197 to do this once, and we can still avoid touching anything
198 inside @code{violin}.
202 \dynamic f \italic \small @{ 2nd @} \hspace #0.1 \dynamic p @}
203 violin = \relative c'' @{
211 @node Saving typing with variables and functions
212 @subsection Saving typing with variables and functions
217 By this point, you've seen this kind of thing:
219 @lilypond[quote,verbatim,ragged-right]
220 hornNotes = \relative c'' { c4 b dis c }
228 You may even realize that this could be useful in minimalist music:
230 @lilypond[quote,verbatim,ragged-right]
231 fragmentA = \relative c'' { a4 a8. b16 }
232 fragmentB = \relative c'' { a8. gis16 ees4 }
233 violin = \new Staff { \fragmentA \fragmentA \fragmentB \fragmentA }
241 However, you can also use these variables (also known as
242 variables, macros, or (user-defined) command) for tweaks:
244 @lilypond[quote,verbatim,ragged-right]
245 dolce = \markup{ \italic \bold dolce }
246 padText = { \once \override TextScript #'padding = #5.0 }
247 fthenp=_\markup{ \dynamic f \italic \small { 2nd } \hspace #0.1 \dynamic p }
248 violin = \relative c'' {
250 c4._\dolce b8 a8 g a b |
252 c4.^"hi there!" d8 e' f g d |
253 c,4.\fthenp b8 c4 c-. |
260 \layout{ragged-right=##t}
264 These variables are obviously useful for saving
265 typing. But they're worth considering even if you
266 only use them once -- they reduce complexity. Let's
267 look at the previous example without any
268 variables. It's a lot harder to read, especially
272 violin = \relative c'' @{
274 c4._\markup@{ \italic \bold dolce @} b8 a8 g a b |
275 \once \override TextScript #'padding = #5.0
276 c4.^"hi there!" d8 e' f g d |
277 c,4.\markup@{ \dynamic f \italic \small @{ 2nd @}
278 \hspace #0.1 \dynamic p @} b8 c4 c-. |
283 @c TODO Replace the following with a better example -td
284 @c Skylining handles this correctly without padText
286 So far we've seen static substitution -- when LilyPond
287 sees @code{\padText}, it replaces it with the stuff that
288 we've defined it to be (ie the stuff to the right of
291 LilyPond can handle non-static substitution, too (you
292 can think of these as functions).
294 @lilypond[quote,verbatim,ragged-right]
296 #(define-music-function (parser location padding) (number?)
298 \once \override TextScript #'padding = #$padding
306 c4^"piu mosso" fis a g
310 Using variables is also a good way to reduce work if the
311 LilyPond input syntax changes (see @ref{Updating old input files}). If
312 you have a single definition (such as @code{\dolce}) for all your
313 input files (see @ref{Style sheets}), then if the syntax changes, you
314 only need to update your single @code{\dolce} definition,
315 instead of making changes throughout every @code{.ly} file.
319 @subsection Style sheets
321 The output that LilyPond produces can be heavily modified; see
322 @ref{Tweaking output}, for details. But what if you have many
323 input files that you want to apply your tweaks to? Or what if you
324 simply want to separate your tweaks from the actual music? This
327 Let's look at an example. Don't worry if you don't understand
328 the parts with all the @code{#()}. This is explained in
329 @ref{Advanced tweaks with Scheme}.
331 @lilypond[quote,verbatim,ragged-right]
332 mpdolce = #(make-dynamic-script (markup #:hspace 0 #:translate '(5 . 0)
333 #:line(#:dynamic "mp" #:text #:italic "dolce" )))
335 inst = #(define-music-function (parser location string) (string?)
339 'text (markup #:bold (#:box string))))
343 a4.\mpdolce d8 cis4--\glissando a | b4 bes a2
345 cis4.\< d8 e4 fis | g8(\! fis)-. e( d)-. cis2
349 There are some problems with overlapping output; we'll fix those using
350 the techniques in @ref{Moving objects}. But let's also
351 do something about the @code{mpdolce} and @code{inst}
352 definitions. They produce the output we desire, but we might want
353 to use them in another piece. We could simply copy-and-paste them
354 at the top of every file, but that's an annoyance. It also leaves
355 those definitions in our input files, and I personally find all
356 the @code{#()} somewhat ugly. Let's hide them in another file:
359 %%% save this to a file called "definitions.ly"
360 mpdolce = #(make-dynamic-script (markup #:hspace 0 #:translate '(5 . 0)
361 #:line(#:dynamic "mp" #:text #:italic "dolce" )))
363 inst = #(define-music-function (parser location string) (string?)
367 'text (markup #:bold (#:box string))))
370 Now let's modify our music (let's save this file as @file{"music.ly"}).
372 @c We have to do this awkward example/lilypond-non-verbatim
373 @c because we can't do the \include stuff in the manual.
376 \include "definitions.ly"
380 a4.\mpdolce d8 cis4--\glissando a | b4 bes a2
382 cis4.\< d8 e4 fis | g8(\! fis)-. e( d)-. cis2
386 @lilypond[quote,ragged-right]
387 mpdolce = #(make-dynamic-script (markup #:hspace 0 #:translate '(5 . 0)
388 #:line(#:dynamic "mp" #:text #:italic "dolce" )))
390 inst = #(define-music-function (parser location string) (string?)
394 'text (markup #:bold (#:box string))))
398 a4.\mpdolce d8 cis4--\glissando a | b4 bes a2
400 cis4.\< d8 e4 fis | g8(\! fis)-. e( d)-. cis2
404 That looks better, but let's make a few changes. The glissando is hard
405 to see, so let's make it thicker and closer to the note heads. Let's
406 put the metronome marking above the clef, instead of over the first
407 note. And finally, my composition professor hates @q{C} time signatures,
408 so we'd better make that @q{4/4} instead.
410 Don't change @file{music.ly}, though. Replace our @file{definitions.ly}
415 mpdolce = #(make-dynamic-script (markup #:hspace 0 #:translate '(5 . 0)
416 #:line( #:dynamic "mp" #:text #:italic "dolce" )))
418 inst = #(define-music-function (parser location string) (string?)
422 'text (markup #:bold (#:box string))))
426 \override MetronomeMark #'extra-offset = #'(-9 . 0)
427 \override MetronomeMark #'padding = #'3
430 \override TimeSignature #'style = #'numbered
433 \override Glissando #'thickness = #3
434 \override Glissando #'gap = #0.1
439 @lilypond[quote,ragged-right]
440 mpdolce = #(make-dynamic-script (markup #:hspace 0 #:translate '(5 . 0)
441 #:line( #:dynamic "mp" #:text #:italic "dolce" )))
443 inst = #(define-music-function (parser location string) (string?)
447 'text (markup #:bold (#:box string))))
451 \override MetronomeMark #'extra-offset = #'(-9 . 0)
452 \override MetronomeMark #'padding = #'3
455 \override TimeSignature #'style = #'numbered
458 \override Glissando #'thickness = #3
459 \override Glissando #'gap = #0.1
465 a4.\mpdolce d8 cis4--\glissando a | b4 bes a2
467 cis4.\< d8 e4 fis | g8(\! fis)-. e( d)-. cis2
471 That looks nicer! But now suppose that I want to publish this
472 piece. My composition professor doesn't like @q{C} time
473 signatures, but I'm somewhat fond of them. Let's copy the
474 current @file{definitions.ly} to @file{web-publish.ly} and
475 modify that. Since this music is aimed at producing a pdf which
476 will be displayed on the screen, we'll also increase the
477 overall size of the output.
481 mpdolce = #(make-dynamic-script (markup #:hspace 0 #:translate '(5 . 0)
482 #:line( #:dynamic "mp" #:text #:italic "dolce" )))
484 inst = #(define-music-function (parser location string) (string?)
488 'text (markup #:bold (#:box string))))
490 #(set-global-staff-size 23)
493 \override MetronomeMark #'extra-offset = #'(-9 . 0)
494 \override MetronomeMark #'padding = #'3
499 \override Glissando #'thickness = #3
500 \override Glissando #'gap = #0.1
505 @lilypond[quote,ragged-right]
506 mpdolce = #(make-dynamic-script (markup #:hspace 0 #:translate '(5 . 0)
507 #:line( #:dynamic "mp" #:text #:italic "dolce" )))
509 inst = #(define-music-function (parser location string) (string?)
513 'text (markup #:bold (#:box string))))
515 #(set-global-staff-size 23)
518 \override MetronomeMark #'extra-offset = #'(-9 . 0)
519 \override MetronomeMark #'padding = #'3
522 \override Glissando #'thickness = #3
523 \override Glissando #'gap = #0.1
529 a4.\mpdolce d8 cis4--\glissando a | b4 bes a2
531 cis4.\< d8 e4 fis | g8(\! fis)-. e( d)-. cis2
535 Now in our music, I simply replace
536 @code{\include "definitions.ly"} with
537 @code{\include "web-publish.ly"}. Of course, we could make this
538 even more convenient. We could make a @file{definitions.ly} file which
539 contains only the definitions of @code{mpdolce} and @code{inst}, a
540 @file{web-publish.ly} file which contains only the @code{\layout}
541 section listed above, and a @file{university.ly} file which
542 contains only the tweaks to produce the output that my professor
543 prefers. The top of @file{music.ly} would then look like this:
546 \include "definitions.ly"
548 %%% Only uncomment one of these two lines!
549 \include "web-publish.ly"
550 %\include "university.ly"
553 This approach can be useful even if you are only producing
554 one set of parts. I use half a dozen different
555 @q{style sheet} files for my projects. I begin every music
556 file with @code{\include "../global.ly"}, which contains
560 \version @w{"@version{}"}
561 #(ly:set-option 'point-and-click #f)
562 \include "../init/init-defs.ly"
563 \include "../init/init-layout.ly"
564 \include "../init/init-headers.ly"
565 \include "../init/init-paper.ly"
569 @node When things don't work
570 @section When things don't work
573 * Updating old input files::
574 * Troubleshooting (taking it all apart)::
578 @node Updating old input files
579 @subsection Updating old input files
581 The LilyPond input syntax occasionally changes. As LilyPond itself
582 improves, the syntax (input language) is modified accordingly. Sometimes
583 these changes are made to make the input easier to read and write or
584 sometimes the changes are made to accommodate new features of LilyPond.
586 LilyPond comes with a file that makes this updating easier:
587 @code{convert-ly}. For details about how to run this program, see
588 @rprogram{Updating files with convert-ly}.
590 Unfortunately, @code{convert-ly} cannot handle all input changes. It
591 takes care of simple search-and-replace changes (such as @code{raggedright}
592 becoming @code{ragged-right}), but some changes are too
593 complicated. The syntax changes that @code{convert-ly} cannot handle
594 are listed in @rprogram{Updating files with convert-ly}.
596 For example, in LilyPond 2.4 and earlier, accents and non-English
597 letters were entered using LaTeX -- for example,
598 @code{No\"el} (this would print the French word for
599 @q{Christmas}). In LilyPond 2.6 and above, the special
600 @code{ë} must be entered directly into the LilyPond file as an
601 UTF-8 character. @code{convert-ly} cannot change all the LaTeX
602 special characters into UTF-8 characters; you must manually update
603 your old LilyPond input files.
606 @node Troubleshooting (taking it all apart)
607 @subsection Troubleshooting (taking it all apart)
609 Sooner or later, you will write a file that LilyPond cannot
610 compile. The messages that LilyPond gives may help
611 you find the error, but in many cases you need to do some
612 investigation to determine the source of the problem.
614 The most powerful tools for this purpose are the
615 single line comment (indicated by @code{%}) and the block
616 comment (indicated by @code{%@{ ... %@}}). If you don't
617 know where a problem is, start commenting out huge portions
618 of your input file. After you comment out a section, try
619 compiling the file again. If it works, then the problem
620 must exist in the portion you just commented. If it doesn't
621 work, then keep on commenting out material until you have
622 something that works.
624 In an extreme case, you might end up with only
638 (in other words, a file without any music)
640 If that happens, don't give up. Uncomment a bit -- say,
641 the bass part -- and see if it works. If it doesn't work,
642 then comment out all of the bass music (but leave
643 @code{\bass} in the @code{\score} uncommented.
646 bass = \relative c' @{
654 Now start slowly uncommenting more and more of the
655 @code{bass} part until you find the problem line.
657 Another very useful debugging technique is constructing
658 @ref{Minimal examples}.
661 @node Minimal examples
662 @subsection Minimal examples
664 A minimal example is an example which is as small as possible. These
665 examples are much easier to understand than long examples. Minimal
666 examples are used for
670 @item Sending a help request to mailing lists
671 @item Adding an example to the @uref{http://lsr.dsi.unimi.it/,
672 LilyPond Snippet Repository}
675 To construct an example which is as small as possible, the rule is
676 quite simple: remove anything which is not necessary. When trying to
677 remove unnecessary parts of a file, it is a very good idea to comment
678 out lines instead of deleting them. That way, if you discover that you
679 actually @emph{do} need some lines, you can uncomment them, instead of
680 typing them in from scratch.
682 There are two exceptions to the @qq{as small as possible} rule:
685 @item Include the @code{\version} number.
686 @item If possible, use @code{\paper@{ ragged-right=##t @}} at the
690 The whole point of a minimal example is to make it easy to read:
693 @item Avoid using complicated notes, keys, or time signatures, unless you
694 wish to demonstrate something is about the behavior of those items.
695 @item Do not use @code{\override} commands unless that is the point of the
701 @node Scores and parts
702 @section Scores and parts
704 TODO: this is really old stuff from the really old tutorial.
705 Rewrite, fix, etc. Or maybe delete entirely. -gp
706 Include section on tags -td
707 and then move to section 5. Working ... -td
709 In orchestral music, all notes are printed twice. Once in a part for
710 the musicians, and once in a full score for the conductor. Variables can
711 be used to avoid double work. The music is entered once, and stored in
712 a variable. The contents of that variable is then used to generate
713 both the part and the full score.
715 It is convenient to define the notes in a special file. For example,
716 suppose that the file @file{horn-music.ly} contains the following part
717 of a horn/@/bassoon duo
720 hornNotes = \relative c @{
727 Then, an individual part is made by putting the following in a file
730 \include "horn-music.ly"
732 instrument = "Horn in F"
736 \transpose f c' \hornNotes
743 \include "horn-music.ly"
747 substitutes the contents of @file{horn-music.ly} at this position in
748 the file, so @code{hornNotes} is defined afterwards. The command
749 @code{\transpose f@tie{}c'} indicates that the argument, being
750 @code{\hornNotes}, should be transposed by a fifth upwards. Sounding
751 @code{f} is denoted by notated @code{c'}, which corresponds with the
752 tuning of a normal French Horn in@tie{}F. The transposition can be seen
753 in the following output
755 @lilypond[quote,ragged-right]
756 \transpose f c' \relative c {
762 In ensemble pieces, one of the voices often does not play for many
763 measures. This is denoted by a special rest, the multi-measure
764 rest. It is entered with a capital @code{R} followed by a duration
765 (@code{1}@tie{}for a whole note, @code{2}@tie{}for a half note,
766 etc.). By multiplying the
767 duration, longer rests can be constructed. For example, this rest
768 takes 3@tie{}measures in 2/4 time
774 When printing the part, multi-rests
775 must be condensed. This is done by setting a run-time variable
778 \set Score.skipBars = ##t
782 This command sets the property @code{skipBars} in the
783 @code{Score} context to true (@code{##t}). Prepending the rest and
784 this option to the music above, leads to the following result
786 @lilypond[quote,ragged-right]
787 \transpose f c' \relative c {
789 \set Score.skipBars = ##t
796 The score is made by combining all of the music together. Assuming
797 that the other voice is in @code{bassoonNotes} in the file
798 @file{bassoon-music.ly}, a score is made with
801 \include "bassoon-music.ly"
802 \include "horn-music.ly"
805 \new Staff \hornNotes
806 \new Staff \bassoonNotes
813 @lilypond[quote,ragged-right]
821 r4 d,8 f | gis4 c | b bes |
822 a8 e f4 | g d | gis f