]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/usage/suggestions.itely
2c6fcaca8fae3331e37f59f23a0dc5c24ae5aefb
[lilypond.git] / Documentation / usage / suggestions.itely
1 @c -*- coding: utf-8; mode: texinfo; -*-
2
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.  For details, see the Contributors'
8     Guide, node Updating translation committishes..
9 @end ignore
10
11 @c \version "2.16.0"
12
13 @node Suggestions for writing files
14 @chapter Suggestions for writing files
15
16 Now you're ready to begin writing larger LilyPond input files --
17 not just the little examples in the tutorial, but whole pieces.
18 But how should you go about doing it?
19
20 As long as LilyPond can understand your input files and produce
21 the output that you want, it doesn't matter what your input files
22 look like.  However, there are a few other things to consider when
23 writing LilyPond input files.
24
25 @itemize
26 @item What if you make a mistake?  The structure of a LilyPond
27 file can make certain errors easier (or harder) to find.
28
29 @item What if you want to share your input files with somebody
30 else?  In fact, what if you want to alter your own input files in
31 a few years?  Some LilyPond input files are understandable at
32 first glance; others may leave you scratching your head
33 for an hour.
34
35 @item What if you want to upgrade your LilyPond file for use
36 with a later version of LilyPond?  The input syntax changes
37 occasionally as LilyPond improves.  Most changes can be
38 done automatically with @code{convert-ly}, but some changes
39 might require manual assistance.  LilyPond input files can be
40 structured in order to be easier (or harder) to update.
41
42 @end itemize
43
44 @menu
45 * General suggestions::
46 * Typesetting existing music::
47 * Large projects::
48 * Troubleshooting::
49 * Make and Makefiles::
50 @end menu
51
52
53 @node General suggestions
54 @section General suggestions
55
56 Here are a few suggestions that can help you to avoid or fix
57 problems:
58
59 @itemize
60 @item @strong{Include @code{\version} numbers in every file}.  Note that all
61 templates contain @code{\version} information.  We
62 highly recommend that you always include the @code{\version}, no matter
63 how small your file is.  Speaking from personal experience, it's
64 quite frustrating to try to remember which version of LilyPond you were
65 using a few years ago.  @command{convert-ly} requires you to declare
66 which version of LilyPond you used.
67
68 @item @strong{Include checks}: @ruser{Bar and bar number checks},
69 @ruser{Octave checks}.  If you include checks every so often, then
70 if you make a mistake, you can pinpoint it quicker.  How often is
71 @q{every so often}?  It depends on the complexity of the music.
72 For very simple music, perhaps just once or twice.  For very
73 complex music, perhaps every bar.
74
75 @item @strong{One bar per line of text}.  If there is anything complicated,
76 either in the music
77 itself or in the output you desire, it's often good to write only one bar
78 per line.  Saving screen space by cramming eight bars per line just isn't
79 worth it if you have to @q{debug} your input files.
80
81 @item @strong{Comment your input files}.  Use either bar numbers
82 (every so often) or
83 references to musical themes (@q{second theme in violins,} @q{fourth
84 variation,} etc.).  You may not need comments when you're writing the piece
85 for the first time, but if you want to go back to change something two or
86 three years later, or if you pass the source over to a friend, it will
87 be much more
88 challenging to determine your intentions or how your file is structured if
89 you didn't comment the file.
90
91 @item @strong{Indent your braces}.  A lot of problems are caused by an
92 imbalance
93 in the number of @code{@{} and @code{@}}.
94
95 @item @strong{Explicitly add durations} at the beginnings of sections
96 and variables.  If you specify @code{c4 d e} at the beginning of a
97 phrase (instead of just @code{c d e}) you can save yourself some
98 problems if you rearrange your music later.
99
100 @item @strong{Separate tweaks} from music definitions.  See
101 @rlearning{Saving typing with variables and functions}, and
102 @rlearning{Style sheets}.
103
104 @end itemize
105
106
107 @node Typesetting existing music
108 @section Typesetting existing music
109
110 If you are entering music from an existing score (i.e., typesetting a
111 piece of existing sheet music),
112
113 @itemize
114
115 @item Enter the manuscript (the physical copy of the music) into
116 LilyPond one system at a time (but still only one bar per line of text),
117 and check each system when you finish it.  You may use the
118 @code{showLastLength} or @code{showFirstLength} properties to speed up
119 processing -- see @ruser{Skipping corrected music}.
120
121 @item Define @code{mBreak = @{ \break @}} and insert @code{\mBreak}
122 in the input file whenever the manuscript has a line break.  This
123 makes it much easier to compare the LilyPond music to the original
124 music.  When you are finished proofreading your score, you may
125 define @code{mBreak = @{ @}} to remove all those line breaks.  This
126 will allow LilyPond to place line breaks wherever it feels are
127 best.
128
129 @item When entering a part for a transposing instrument into a
130 variable, it is recommended that the notes are wrapped in
131
132 @example
133 \transpose c natural-pitch @{@dots{}@}
134 @end example
135
136 @noindent
137 (where @code{natural-pitch} is the open pitch of the instrument) so
138 that the music in the variable is effectively in C. You can transpose
139 it back again when the variable is used, if required, but you might
140 not want to (e.g., when printing a score in concert pitch,
141 converting a trombone part from treble to bass clef, etc.)
142 Mistakes in transpositions are less likely if all the music in
143 variables is at a consistent pitch.
144
145 Also, only ever transpose to/from C. That means that the only other
146 keys you will use are the natural pitches of the instruments - bes
147 for a B-flat trumpet, aes for an A-flat clarinet, etc.
148
149 @end itemize
150
151
152 @node Large projects
153 @section Large projects
154
155 When working on a large project, having a clear structure to your
156 lilypond input files becomes vital.
157
158 @itemize
159
160 @item @strong{Use a variable for each voice}, with a minimum of
161 structure inside the definition.  The structure of the
162 @code{\score} section is the most likely thing to change;
163 the @code{violin} definition is extremely unlikely to change
164 in a new version of LilyPond.
165
166 @example
167 violin = \relative c'' @{
168 g4 c'8. e16
169 @}
170 @dots{}
171 \score @{
172   \new GrandStaff @{
173     \new Staff @{
174       \violin
175     @}
176   @}
177 @}
178 @end example
179
180 @item @strong{Separate tweaks from music definitions}.  This point was
181 made previously, but for large projects it is absolutely vital.  We
182 might need to change the definition of @code{fthenp}, but then we only
183 need to do this once, and we can still avoid touching anything inside
184 @code{violin}.
185
186 @example
187 fthenp = _\markup@{
188   \dynamic f \italic \small @{ 2nd @} \hspace #0.1 \dynamic p @}
189 violin = \relative c'' @{
190 g4\fthenp c'8. e16
191 @}
192 @end example
193
194 @end itemize
195
196
197 @node Troubleshooting
198 @section Troubleshooting
199
200 Sooner or later, you will write a file that LilyPond cannot
201 compile.  The messages that LilyPond gives may help
202 you find the error, but in many cases you need to do some
203 investigation to determine the source of the problem.
204
205 The most powerful tools for this purpose are the
206 single line comment (indicated by @code{%}) and the block
207 comment (indicated by @code{%@{@dots{}%@}}).  If you don't
208 know where a problem is, start commenting out huge portions
209 of your input file.  After you comment out a section, try
210 compiling the file again.  If it works, then the problem
211 must exist in the portion you just commented.  If it doesn't
212 work, then keep on commenting out material until you have
213 something that works.
214
215 In an extreme case, you might end up with only
216
217 @example
218 \score @{
219   <<
220     % \melody
221     % \harmony
222     % \bass
223   >>
224   \layout@{@}
225 @}
226 @end example
227
228 @noindent
229 (in other words, a file without any music)
230
231 If that happens, don't give up.  Uncomment a bit -- say,
232 the bass part -- and see if it works.  If it doesn't work,
233 then comment out all of the bass music (but leave
234 @code{\bass} in the @code{\score} uncommented.
235
236 @example
237 bass = \relative c' @{
238 %@{
239   c4 c c c
240   d d d d
241 %@}
242 @}
243 @end example
244
245 Now start slowly uncommenting more and more of the
246 @code{bass} part until you find the problem line.
247
248 Another very useful debugging technique is constructing
249 @rweb{Tiny examples}.
250
251
252 @node Make and Makefiles
253 @section Make and Makefiles
254
255 @cindex makefiles
256 @cindex make
257
258 Pretty well all the platforms Lilypond can run on support a software
259 facility called @code{make}.  This software reads a special file called a
260 @code{Makefile} that defines what files depend on what others and what
261 commands you need to give the operating system to produce one file from
262 another.  For example the makefile would spell out how to produce
263 @file{ballad.pdf} and @file{ballad.midi} from @file{ballad.ly} by
264 running Lilypond.
265
266 There are times when it is a good idea to create a @code{Makefile}
267 for your project, either for your own convenience or
268 as a courtesy to others who might have access to your source files.
269 This is true for very large projects with many included files and
270 different output options (e.g. full score, parts, conductor's
271 score, piano reduction, etc.), or for projects that
272 require difficult commands to build them (such as
273 @code{lilypond-book} projects).  Makefiles vary greatly in
274 complexity and flexibility, according to the needs and skills of
275 the authors.  The program GNU Make comes installed on GNU/Linux
276 distributions and on MacOS X, and it is also available for Windows.
277
278 See the @strong{GNU Make Manual} for full details on using
279 @code{make}, as what follows here gives only a glimpse of what it
280 can do.
281
282 The commands to define rules in a makefile differ
283 according to platform; for instance the various forms of GNU/Linux and
284 MacOS use @code{bash}, while Windows uses @code{cmd}.  Note that on
285 MacOS X, you need to configure the system to use the command-line
286 interpreter.  Here are some example makefiles, with versions for both
287 GNU/Linux/MacOS and Windows.
288
289 The first example is for an orchestral work in four
290 movements with a directory structure as follows:
291
292 @example
293 Symphony/
294 |-- MIDI/
295 |-- Makefile
296 |-- Notes/
297 |   |-- cello.ily
298 |   |-- figures.ily
299 |   |-- horn.ily
300 |   |-- oboe.ily
301 |   |-- trioString.ily
302 |   |-- viola.ily
303 |   |-- violinOne.ily
304 |   `-- violinTwo.ily
305 |-- PDF/
306 |-- Parts/
307 |   |-- symphony-cello.ly
308 |   |-- symphony-horn.ly
309 |   |-- symphony-oboes.ly
310 |   |-- symphony-viola.ly
311 |   |-- symphony-violinOne.ly
312 |   `-- symphony-violinTwo.ly
313 |-- Scores/
314 |   |-- symphony.ly
315 |   |-- symphonyI.ly
316 |   |-- symphonyII.ly
317 |   |-- symphonyIII.ly
318 |   `-- symphonyIV.ly
319 `-- symphonyDefs.ily
320 @end example
321
322 The @file{.ly} files in the @file{Scores} and
323 @file{Parts} directories get their notes from @file{.ily}
324 files in the @file{Notes} directory:
325
326 @example
327 %%% top of file "symphony-cello.ly"
328 \include ../symphonyDefs.ily
329 \include ../Notes/cello.ily
330 @end example
331
332 The makefile will have targets of @code{score} (entire piece in
333 full score), @code{movements} (individual movements in full score),
334 and @code{parts} (individual parts for performers).  There
335 is also a target @code{archive} that will create a tarball of
336 the source files, suitable for sharing via web or email.  Here is
337 the makefile for GNU/Linux or MacOS X.  It should be saved with the
338 name @code{Makefile} in the top directory of the project:
339
340 @warning{When a target or pattern rule is defined, the
341 subsequent lines must begin with tabs, not spaces.}
342
343 @example
344 # the name stem of the output files
345 piece = symphony
346 # determine how many processors are present
347 CPU_CORES=`cat /proc/cpuinfo | grep -m1 "cpu cores" | sed s/".*: "//`
348 # The command to run lilypond
349 LILY_CMD = lilypond -ddelete-intermediate-files \
350                     -dno-point-and-click -djob-count=$(CPU_CORES)
351
352 # The suffixes used in this Makefile.
353 .SUFFIXES: .ly .ily .pdf .midi
354
355 # Input and output files are searched in the directories listed in
356 # the VPATH variable.  All of them are subdirectories of the current
357 # directory (given by the GNU make variable `CURDIR').
358 VPATH = \
359   $(CURDIR)/Scores \
360   $(CURDIR)/PDF \
361   $(CURDIR)/Parts \
362   $(CURDIR)/Notes
363
364 # The pattern rule to create PDF and MIDI files from a LY input file.
365 # The .pdf output files are put into the `PDF' subdirectory, and the
366 # .midi files go into the `MIDI' subdirectory.
367 %.pdf %.midi: %.ly
368         $(LILY_CMD) $<; \           # this line begins with a tab
369         if test -f "$*.pdf"; then \
370             mv "$*.pdf" PDF/; \
371         fi; \
372         if test -f "$*.midi"; then \
373             mv "$*.midi" MIDI/; \
374         fi
375
376 notes = \
377   cello.ily \
378   horn.ily \
379   oboe.ily \
380   viola.ily \
381   violinOne.ily \
382   violinTwo.ily
383
384 # The dependencies of the movements.
385 $(piece)I.pdf: $(piece)I.ly $(notes)
386 $(piece)II.pdf: $(piece)II.ly $(notes)
387 $(piece)III.pdf: $(piece)III.ly $(notes)
388 $(piece)IV.pdf: $(piece)IV.ly $(notes)
389
390 # The dependencies of the full score.
391 $(piece).pdf: $(piece).ly $(notes)
392
393 # The dependencies of the parts.
394 $(piece)-cello.pdf: $(piece)-cello.ly cello.ily
395 $(piece)-horn.pdf: $(piece)-horn.ly horn.ily
396 $(piece)-oboes.pdf: $(piece)-oboes.ly oboe.ily
397 $(piece)-viola.pdf: $(piece)-viola.ly viola.ily
398 $(piece)-violinOne.pdf: $(piece)-violinOne.ly violinOne.ily
399 $(piece)-violinTwo.pdf: $(piece)-violinTwo.ly violinTwo.ily
400
401 # Type `make score' to generate the full score of all four
402 # movements as one file.
403 .PHONY: score
404 score: $(piece).pdf
405
406 # Type `make parts' to generate all parts.
407 # Type `make foo.pdf' to generate the part for instrument `foo'.
408 # Example: `make symphony-cello.pdf'.
409 .PHONY: parts
410 parts: $(piece)-cello.pdf \
411        $(piece)-violinOne.pdf \
412        $(piece)-violinTwo.pdf \
413        $(piece)-viola.pdf \
414        $(piece)-oboes.pdf \
415        $(piece)-horn.pdf
416
417 # Type `make movements' to generate files for the
418 # four movements separately.
419 .PHONY: movements
420 movements: $(piece)I.pdf \
421            $(piece)II.pdf \
422            $(piece)III.pdf \
423            $(piece)IV.pdf
424
425 all: score parts movements
426
427 archive:
428         tar -cvvf stamitz.tar \       # this line begins with a tab
429         --exclude=*pdf --exclude=*~ \
430         --exclude=*midi --exclude=*.tar \
431         ../Stamitz/*
432 @end example
433
434
435 There are special complications on the Windows platform.  After
436 downloading and installing GNU Make for Windows, you must set the
437 correct path in the system's environment variables so that the
438 DOS shell can find the Make program.  To do this, right-click on
439 "My Computer," then choose @code{Properties} and
440 @code{Advanced}.  Click @code{Environment Variables}, and then
441 in the @code{System Variables} pane, highlight @code{Path}, click
442 @code{edit}, and add the path to the GNU Make executable file, which
443  will look something like this:
444
445 @example
446 C:\Program Files\GnuWin32\bin
447 @end example
448
449 The makefile itself has to be altered to handle different shell
450 commands and to deal with spaces that are present
451 in some default system directories.  The @code{archive} target
452 is eliminated since Windows does not have the @code{tar} command,
453 and Windows also has a different default extension for midi files.
454
455
456 @example
457 ## WINDOWS VERSION
458 ##
459 piece = symphony
460 LILY_CMD = lilypond -ddelete-intermediate-files \
461                     -dno-point-and-click \
462                     -djob-count=$(NUMBER_OF_PROCESSORS)
463
464 #get the 8.3 name of CURDIR (workaround for spaces in PATH)
465 workdir = $(shell for /f "tokens=*" %%b in ("$(CURDIR)") \
466           do @@echo %%~sb)
467
468 .SUFFIXES: .ly .ily .pdf .mid
469
470 VPATH = \
471   $(workdir)/Scores \
472   $(workdir)/PDF \
473   $(workdir)/Parts \
474   $(workdir)/Notes
475
476 %.pdf %.mid: %.ly
477         $(LILY_CMD) $<      # this line begins with a tab
478         if exist "$*.pdf"  move /Y "$*.pdf"  PDF/ # begin with tab
479         if exist "$*.mid" move /Y "$*.mid" MIDI/  # begin with tab
480
481 notes = \
482   cello.ily \
483   figures.ily \
484   horn.ily \
485   oboe.ily \
486   trioString.ily \
487   viola.ily \
488   violinOne.ily \
489   violinTwo.ily
490
491 $(piece)I.pdf: $(piece)I.ly $(notes)
492 $(piece)II.pdf: $(piece)II.ly $(notes)
493 $(piece)III.pdf: $(piece)III.ly $(notes)
494 $(piece)IV.pdf: $(piece)IV.ly $(notes)
495
496 $(piece).pdf: $(piece).ly $(notes)
497
498 $(piece)-cello.pdf: $(piece)-cello.ly cello.ily
499 $(piece)-horn.pdf: $(piece)-horn.ly horn.ily
500 $(piece)-oboes.pdf: $(piece)-oboes.ly oboe.ily
501 $(piece)-viola.pdf: $(piece)-viola.ly viola.ily
502 $(piece)-violinOne.pdf: $(piece)-violinOne.ly violinOne.ily
503 $(piece)-violinTwo.pdf: $(piece)-violinTwo.ly violinTwo.ily
504
505 .PHONY: score
506 score: $(piece).pdf
507
508 .PHONY: parts
509 parts: $(piece)-cello.pdf \
510        $(piece)-violinOne.pdf \
511        $(piece)-violinTwo.pdf \
512        $(piece)-viola.pdf \
513        $(piece)-oboes.pdf \
514        $(piece)-horn.pdf
515
516 .PHONY: movements
517 movements: $(piece)I.pdf \
518            $(piece)II.pdf \
519            $(piece)III.pdf \
520            $(piece)IV.pdf
521
522 all: score parts movements
523 @end example
524
525
526 The next Makefile is for a @command{lilypond-book} document done in
527 LaTeX.  This project has an index, which requires that the
528 @command{latex} command be run twice to update links.  Output files are
529 all stored in the @code{out} directory for .pdf output and in the
530 @code{htmlout} directory for the html output.
531
532 @example
533 SHELL=/bin/sh
534 FILE=myproject
535 OUTDIR=out
536 WEBDIR=htmlout
537 VIEWER=acroread
538 BROWSER=firefox
539 LILYBOOK_PDF=lilypond-book --output=$(OUTDIR) --pdf $(FILE).lytex
540 LILYBOOK_HTML=lilypond-book --output=$(WEBDIR) $(FILE).lytex
541 PDF=cd $(OUTDIR) && pdflatex $(FILE)
542 HTML=cd $(WEBDIR) && latex2html $(FILE)
543 INDEX=cd $(OUTDIR) && makeindex $(FILE)
544 PREVIEW=$(VIEWER) $(OUTDIR)/$(FILE).pdf &
545
546 all: pdf web keep
547
548 pdf:
549         $(LILYBOOK_PDF)  # begin with tab
550         $(PDF)           # begin with tab
551         $(INDEX)         # begin with tab
552         $(PDF)           # begin with tab
553         $(PREVIEW)       # begin with tab
554
555 web:
556         $(LILYBOOK_HTML) # begin with tab
557         $(HTML)          # begin with tab
558         cp -R $(WEBDIR)/$(FILE)/ ./  # begin with tab
559         $(BROWSER) $(FILE)/$(FILE).html &  # begin with tab
560
561 keep: pdf
562         cp $(OUTDIR)/$(FILE).pdf $(FILE).pdf  # begin with tab
563
564 clean:
565         rm -rf $(OUTDIR) # begin with tab
566
567 web-clean:
568         rm -rf $(WEBDIR) # begin with tab
569
570 archive:
571         tar -cvvf myproject.tar \ # begin this line with tab
572         --exclude=out/* \
573         --exclude=htmlout/* \
574         --exclude=myproject/* \
575         --exclude=*midi \
576         --exclude=*pdf \
577         --exclude=*~ \
578         ../MyProject/*
579 @end example
580
581 TODO: make this thing work on Windows
582
583 The previous makefile does not work on Windows.  An alternative
584 for Windows users would be to create a simple batch file
585 containing the build commands.  This will not
586 keep track of dependencies the way a makefile does, but it at
587 least reduces the build process to a single command.  Save the
588 following code as @command{build.bat} or @command{build.cmd}.
589 The batch file can be run at the DOS prompt or by simply
590 double-clicking its icon.
591
592 @example
593 lilypond-book --output=out --pdf myproject.lytex
594 cd out
595 pdflatex myproject
596 makeindex myproject
597 pdflatex myproject
598 cd ..
599 copy out\myproject.pdf MyProject.pdf
600 @end example
601
602
603 @seealso
604
605 This manual:
606 @ref{Command-line usage},
607 @ref{lilypond-book}
608