]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/learning/tutorial.itely
Doc: LM: More nitpicks in tutorial.
[lilypond.git] / Documentation / learning / tutorial.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 @include included/generating-output.itexi
12
13 @c \version "2.12.0"
14
15 @node Tutorial
16 @chapter Tutorial
17
18 This chapter gives a basic introduction to working with LilyPond.
19
20 @menu
21 * Compiling a file::
22 * How to write input files::
23 * Dealing with errors::
24 * How to read the manuals::
25 @end menu
26
27 @node Compiling a file
28 @section Compiling a file
29
30 This section introduces @qq{compiling}---the processing of
31 LilyPond input files (written by you) to produce output files.
32
33 @menu
34 * Entering input::
35 * MacOS X::
36 * Windows::
37 * Command-line::
38 @end menu
39
40 @node Entering input
41 @subsection Entering input
42
43 @cindex compiling
44 @cindex first example
45 @cindex example, first
46 @cindex case sensitive
47
48 @qq{Compiling} is the term used for processing an input file in
49 LilyPond format to produce output file(s).  Output files are
50 generally PDF (for printing or viewing), MIDI (for playing), and
51 PNG (for online use).  LilyPond input files are simple text files.
52
53 This example shows a simple input file:
54
55 @example
56 \version "@w{@version{}}"
57 @{
58   c' e' g' e'
59 @}
60 @end example
61
62 The graphical output is:
63
64 @c  in this case we don't want verbatim
65 @lilypond[quote]
66 {
67   c' e' g' e'
68 }
69 @end lilypond
70
71 @warning{Notes and lyrics in LilyPond input must always be
72 surrounded by @w{@strong{@{ curly braces @}}}.  The braces
73 should also be surrounded by a space unless they are at the
74 beginning or end of a line to avoid ambiguities.  They may
75 be omitted in some examples in this manual, but don't forget them
76 in your own music!  For more information about the display of
77 examples in the manual, see @ref{How to read the manuals}.}
78
79 In addition, LilyPond input is @strong{case sensitive}.
80 @w{@samp{@{ c d e @}}} is valid input; @w{@samp{@{ C D E @}}} will
81 produce an error message.
82
83
84 @smallspace
85
86 @subheading Producing output
87
88 @cindex PDF file
89 @cindex viewing music
90 @cindex text editors
91
92 The method of producing output depends on your operating system
93 and the program(s) you use.
94
95 @itemize
96
97 @item
98 @ref{MacOS X, @sourceimage{logo-macosx,,,}}
99 @ref{MacOS X, MacOS X} (graphical)
100
101 @item
102 @ref{Windows, @sourceimage{logo-windows,,,}}
103 @ref{Windows, Microsoft Windows} (graphical)
104
105 @item
106 @ref{Command-line, @sourceimage{logo-linux,,,}
107 @sourceimage{logo-freebsd,,,}
108 @sourceimage{logo-macosx,,,}
109 @sourceimage{logo-windows,,,}
110 }
111 @ref{Command-line, All operating systems} (command-line)
112
113 @end itemize
114
115 There are several other text editors available with specific
116 support for LilyPond.  For more information, see
117 @rweb{Easier editing}.
118
119 @warning{The first time you ever run LilyPond, it may take a
120 minute or two because all of the system fonts have to be analyzed
121 first.  After this, LilyPond will be much faster!}
122
123
124 @node MacOS X
125 @subsection MacOS X
126
127 @cindex running LilyPond under MacOS X
128 @cindex MacOS X, running LilyPond
129
130 @lilypadOSX
131
132
133 @node Windows
134 @subsection Windows
135
136 @cindex running LilyPond under Windows
137 @cindex Windows, running LilyPond
138
139 @lilypadWindows
140
141
142 @node Command-line
143 @subsection Command-line
144
145 @cindex running LilyPond under Unix
146 @cindex Unix, running LilyPond
147
148 @lilypadCommandLine
149
150 @node How to write input files
151 @section How to write input files
152
153 This section introduces some basic LilyPond syntax to help get you
154 started writing input files.
155
156 @menu
157 * Simple notation::
158 * Working on input files::
159 @end menu
160
161
162 @node Simple notation
163 @subsection Simple notation
164
165 @cindex simple notation
166 @cindex notation, simple
167
168 LilyPond will add some notation elements automatically.  In the
169 next example, we have only specified four pitches, but LilyPond
170 has added a clef, time signature, and rhythms.
171
172 @lilypond[verbatim,quote]
173 {
174   c' e' g' e'
175 }
176 @end lilypond
177
178 @noindent
179 This behavior may be altered, but in most cases these automatic
180 values are useful.
181
182
183 @subheading Pitches
184
185 @cindex pitches
186 @cindex relative mode
187 @cindex quote, single
188 @cindex comma
189 @cindex accidentals and relative mode
190 @cindex relative mode, and accidentals
191
192 @funindex \relative
193 @funindex relative
194 @funindex '
195 @funindex ,
196
197 Music Glossary: @rglos{pitch}, @rglos{interval},
198 @rglos{scale}, @rglos{middle C}, @rglos{octave},
199 @rglos{accidental}.
200
201 The easiest way to enter notes is by using @code{\relative} mode.
202 In this mode, the octave is chosen automatically by assuming the
203 following note is always to be placed closest to the previous
204 note, i.e., it is to be placed in the octave which is within three
205 staff spaces of the previous note.  We begin by entering the most
206 elementary piece of music, a @notation{scale}, in which every note
207 is within just one staff space of the previous note.
208
209 @lilypond[verbatim,quote]
210 % set the starting point to middle C
211 \relative c' {
212   c d e f
213   g a b c
214 }
215 @end lilypond
216
217 The initial note is @notation{middle C}.  Each successive note is
218 placed closest to the previous note -- in other words, the first
219 @code{c} is the closest C to middle C.  This is followed by the
220 closest D to the previous note.  We can create melodies which have
221 larger intervals, still using only @code{\relative} mode:
222
223 @lilypond[verbatim,quote]
224 \relative c' {
225   d f a g
226   c b f d
227 }
228 @end lilypond
229
230 @noindent
231 It is not necessary for the first note of the melody to start on
232 the note which specifies the starting pitch.  In the previous
233 example, the first note -- the @code{d} -- is the closest D to
234 middle C.
235
236 By adding (or removing) quotes @code{'} or commas @code{,} from
237 the @q{@w{@code{@bs{}relative c'}}} command, we can change the
238 starting octave:
239
240 @lilypond[verbatim,quote]
241 % one octave above middle C
242 \relative c'' {
243   e c a c
244 }
245 @end lilypond
246
247 Relative mode can be confusing initially, but is the easiest way
248 to enter most melodies.  Let us see how this relative calculation
249 works in practice.  Starting from a B, which is on the middle line
250 in a treble clef, you can reach a C, D and E within 3 staff spaces
251 going up, and an A, G and F within 3 staff spaces going down.  So
252 if the note following a B is a C, D or E it will be assumed to be
253 above the B, and an A, G or F will be assumed to be below.
254
255 @lilypond[verbatim,quote]
256 \relative c'' {
257   b c  % c is 1 staff space up, so is the c above
258   b d  % d is 2 up or 5 down, so is the d above
259   b e  % e is 3 up or 4 down, so is the e above
260   b a  % a is 6 up or 1 down, so is the a below
261   b g  % g is 5 up or 2 down, so is the g below
262   b f  % f is 4 up or 3 down, so is the f below
263 }
264 @end lilypond
265
266 Exactly the same happens even when any of these notes are
267 sharpened or flattened.  @notation{Accidentals} are
268 @strong{totally ignored} in the calculation of relative position.
269 Precisely the same staff space counting is done from a note at any
270 other position on the staff.
271
272 To add intervals that are larger than three staff spaces, we can
273 raise the @notation{octave} by adding a single quote @code{'} (or
274 apostrophe) to the note name.  We can lower the octave by adding a
275 comma @code{,} to the note name.
276
277 @lilypond[verbatim,quote]
278 \relative c'' {
279   a a, c' f,
280   g g'' a,, f'
281 }
282 @end lilypond
283
284 @noindent
285 To change a note by two (or more!) octaves, we use multiple
286 @code{''} or @code{,,} -- but be careful that you use two single
287 quotes @code{''} and not one double quote @code{"}@tie{}!
288 @c " - keeps quotes in order for context-sensitive editor -td
289
290 @subheading Durations (rhythms)
291
292 @cindex note durations
293 @cindex durations
294 @cindex rhythms
295 @cindex whole note
296 @cindex half note
297 @cindex quarter note
298 @cindex dotted note
299 @cindex notating durations
300
301 Music Glossary: @rglos{beam}, @rglos{duration},
302 @rglos{whole note}, @rglos{half note}, @rglos{quarter note},
303 @rglos{dotted note}.
304
305 The @notation{duration} of a note is specified by a number after
306 the note name:  @code{1} for a @notation{whole note}, @code{2} for
307 a @notation{half note}, @code{4} for a @notation{quarter note} and
308 so on.  @notation{Beams} are added automatically.
309
310 If you do not specify a duration, the previous duration is used
311 for the next note.  The duration of the first note defaults to a
312 quarter note.
313
314 @lilypond[verbatim,quote]
315 \relative c'' {
316   a1
317   a2 a4 a8 a
318   a16 a a a a32 a a a a64 a a a a a a a a2
319 }
320 @end lilypond
321
322 To create @notation{dotted notes}, add a dot @code{.} to the
323 duration number.  The duration of a dotted note must be stated
324 explicitly (i.e., with a number).
325
326 @lilypond[verbatim,quote]
327 \relative c'' {
328   a a a4. a8
329   a8. a16 a a8. a8 a4.
330 }
331 @end lilypond
332
333
334 @subheading Rests
335
336 @cindex rest
337 @cindex notating rests
338
339 Music Glossary: @rglos{rest}.
340
341 A @notation{rest} is entered just like a note with the name
342 @code{r}@tie{}:
343
344 @lilypond[verbatim,quote]
345 \relative c'' {
346   a r r2
347   r8 a r4 r4. r8
348 }
349 @end lilypond
350
351
352 @subheading Time signature
353
354 @cindex time signature
355
356 @funindex \time
357 @funindex time
358
359 Music Glossary: @rglos{time signature}.
360
361 The @notation{time signature} can be set with the @code{\time}
362 command:
363
364 @lilypond[verbatim,quote]
365 \relative c'' {
366   \time 3/4
367   a4 a a
368   \time 6/8
369   a4. a
370   \time 4/4
371   a4 a a a
372 }
373 @end lilypond
374
375
376 @subheading Clef
377
378 @cindex clef
379 @cindex treble
380 @cindex alto
381 @cindex tenor
382 @cindex bass
383
384 @funindex \clef
385 @funindex clef
386
387 Music Glossary: @rglos{clef}.
388
389 The @notation{clef} can be set using the @code{\clef} command:
390
391 @lilypond[verbatim,quote]
392 \relative c' {
393   \clef treble
394   c1
395   \clef alto
396   c1
397   \clef tenor
398   c1
399   \clef bass
400   c1
401 }
402 @end lilypond
403
404
405 @subheading All together
406
407 Here is a small example showing all these elements together:
408
409 @lilypond[verbatim,quote]
410 \relative c, {
411   \time 3/4
412   \clef bass
413   c2 e8 c' g'2.
414   f4 e d c4 c, r4
415 }
416 @end lilypond
417
418
419 @seealso
420 Notation Reference: @ruser{Writing pitches},
421 @ruser{Writing rhythms}, @ruser{Writing rests},
422 @ruser{Time signature}, @ruser{Clef}.
423
424
425 @node Working on input files
426 @subsection Working on input files
427
428 @cindex curly braces
429 @cindex braces, curly
430 @cindex comments
431 @cindex line comment
432 @cindex comment, line
433 @cindex block comment
434 @cindex comment, line
435 @cindex case sensitive
436 @cindex whitespace insensitive
437 @cindex expressions
438 @cindex versioning
439 @cindex version
440 @cindex version number
441
442 @funindex \version
443 @funindex { ... }
444 @funindex %
445 @funindex %@{ ... %@}
446
447 LilyPond input files are similar to source files in many common
448 programming languages.  They contain a version statement, are case
449 sensitive, and white-space is generally ignored.  Expressions are
450 formed with curly braces @w{@code{@{ @}}}, and comments are
451 denoted with @code{%} or @w{@code{%@{ @dots{} %@}}}@tie{}.
452
453 If the previous sentences sound like nonsense, don't worry!  We'll
454 explain what all these terms mean:
455
456 @itemize
457
458 @item
459 @strong{Version statement}:
460 Every LilyPond file should contain a version statement.  A version
461 statement is a line that describes the version of LilyPond for which
462 the file was written, as in the following example:
463
464 @example
465 \version "@w{@version{}}"
466 @end example
467
468 By convention, the version statement is placed at the top of the
469 LilyPond file.
470
471 The version statement is important for at least two reasons.  First,
472 it allows automatic updating of the input file as LilyPond syntax
473 changes.  Second, it describes the version of LilyPond needed to
474 compile the file.
475
476 If the version statement is omitted from an input file, LilyPond will print
477 a warning during the compilation of the file.
478
479 @item
480 @strong{Case sensitive}:
481 it matters whether you enter a letter in lower case (e.g.
482 @w{@code{a, b, s, t}}) or upper case (e.g. @w{@code{A, B, S, T}}).
483 Notes are lower case: @w{@samp{@{ c d e @}}} is valid input;
484 @w{@samp{@{ C D E @}}} will produce an error message.
485
486 @item
487 @strong{Whitespace insensitive}:
488 it does not matter how many spaces (or tabs or new lines) you add.
489 @w{@samp{@{ c d e @}}} means the same thing as
490 @w{@samp{@{ c @tie{} @tie{} @tie{} d e @}}} and:
491
492 @example
493 @{ c                        d
494                    e   @}
495 @end example
496
497 @noindent
498 Of course, the previous example is hard to read.  A good rule of
499 thumb is to indent code blocks with either a tab or two spaces:
500
501 @example
502 @{
503   c d e
504 @}
505 @end example
506
507 However, whitespace @emph{is} required to separate many
508 syntactical elements from others.  In other words, whitespace can
509 always be @emph{added}, but not always @emph{eliminated}.  Since
510 missing whitespace can give rise to strange errors, it is
511 advisable to always insert whitespace before and after every
512 syntactic element, for example, before and after every curly
513 brace.
514
515 @item
516 @strong{Expressions}:
517 every piece of LilyPond input needs to have
518 @strong{@{@tie{}curly@tie{}braces@tie{}@}} placed around the
519 input.  These braces tell LilyPond that the input is a single
520 music expression, just like parentheses @code{()} in mathematics.
521 The braces should be surrounded by a space unless they are at the
522 beginning or end of a line to avoid ambiguities.
523
524 A LilyPond command followed by a simple expression in braces (such
525 as @q{@w{@code{@bs{}relative c' @{ @dots{} @}}}}) also counts as a
526 single music expression.
527
528 @cindex comments
529 @cindex line comment
530 @cindex block comment
531 @item
532 @strong{Comments}:
533 a comment is a remark for the human reader of the music input; it
534 is ignored while parsing, so it has no effect on the printed
535 output.  There are two types of comments.  The percent symbol
536 @code{%} introduces a line comment; anything after @code{%} on
537 that line is ignored.  By convention, a line comment is placed
538 @emph{above} the code it refers to.
539
540 @example
541 a4 a a a
542 % this comment refers to the Bs
543 b2 b
544 @end example
545
546 A block comment marks a whole section of music input as a comment.
547 Anything that is enclosed in @code{%@{} and @code{%@}} is ignored.
548 However, block comments do not @q{nest}.  This means that you
549 cannot place a block comment inside another block comment.  If you
550 try, the first @code{%@}} will terminate @emph{both} block
551 comments.  The following fragment shows possible uses for
552 comments:
553
554 @example
555 % notes for twinkle twinkle follow
556   c4 c g' g a a g2
557
558 %@{
559   This line, and the notes below are ignored,
560   since they are in a block comment.
561
562   f f e e d d c2
563 %@}
564 @end example
565
566 @end itemize
567
568
569 @node Dealing with errors
570 @section Dealing with errors
571
572 @cindex troubleshooting
573
574 Sometimes LilyPond doesn't produce the output you expect.  This
575 section provides some links to help you solve the problems you
576 might encounter.
577
578
579 @menu
580 * General troubleshooting tips::
581 * Some common errors::
582 @end menu
583
584 @node General troubleshooting tips
585 @subsection General troubleshooting tips
586
587 Troubleshooting LilyPond problems can be challenging for
588 people who are used to a graphical interface, because invalid
589 input files can be created.  When this happens, a logical approach
590 is the best way to identify and solve the problem.  Some guidelines
591 to help you learn to do this are provided in @rprogram{Troubleshooting}.
592
593
594 @node Some common errors
595 @subsection Some common errors
596
597 @cindex common errors
598 @cindex errors, common
599
600 There are a few common errors that are difficult to troubleshoot
601 based simply on the error messages that are displayed.  These are
602 described in @rprogram{Common errors}.
603
604
605
606 @node How to read the manuals
607 @section How to read the manuals
608
609 This section shows how to read the documentation efficiently, and
610 also introduces some useful interactive features available in the
611 online version.
612
613 @menu
614 * Omitted material::
615 * Clickable examples::
616 * Overview of manuals::
617 @end menu
618
619
620 @node Omitted material
621 @subsection Omitted material
622
623
624 @cindex how to read the manual
625 @cindex manual, reading
626 @cindex reading the manual
627 @cindex examples, clickable
628 @cindex clickable examples
629 @cindex tips for constructing files
630 @cindex templates
631 @cindex constructing files, tips
632 @cindex files, tips for constructing
633
634 LilyPond input must be surrounded by @code{@{ @}} marks or a
635 @q{@w{@code{@bs{}relative c'' @{ @dots{} @}}}}, as we saw in
636 @ref{Working on input files}.  For the rest of this manual, most
637 examples will omit this.  To replicate the examples, you may copy
638 and paste the displayed input, but you @strong{must} add the
639 @q{@w{@code{@bs{}relative c'' @{ @dots{} @}}}} like this:
640
641 @example
642 \relative c'' @{
643   @dots{}example goes here@dots{}
644 @}
645 @end example
646
647 Why omit the braces?  Most examples in this manual can be inserted
648 into the middle of a longer piece of music.  For these examples,
649 it does not make sense to add
650 @q{@w{@code{@bs{}relative c'' @{ @dots{} @}}}} -- you should not
651 place a @code{\relative} inside another @code{\relative}!  If we
652 included @q{@w{@code{@bs{}relative c'' @{ @dots{} @}}}} around
653 every example, you would not be able to copy a small documentation
654 example and paste it inside a longer piece of your own.  Most
655 people want to add material to an existing piece, so we format the
656 manual this way.
657
658 Also, remember that every LilyPond file should have a
659 @code{\version} statement.  Because the examples in the manuals
660 are snippets, not files, the @code{\version} statement is omitted.
661 But you should make a practice of including them in your files.
662
663 @node Clickable examples
664 @subsection Clickable examples
665
666 @warning{This feature is only available in the HTML manuals.}
667
668 Many people learn programs by trying and fiddling around with the
669 program.  This is also possible with LilyPond.  If you click on a
670 picture in the HTML version of this manual, you will see the exact
671 LilyPond input that was used to generate that image.  Try it on
672 this image:
673
674 @c no verbatim here
675 @lilypond[quote]
676 \relative c'' {
677   c-\markup { \bold \huge { Click here. } }
678 }
679 @end lilypond
680
681 By cutting and pasting everything in the @qq{ly snippet} section,
682 you have a starting template for experiments.  To see exactly the
683 same output (line-width and all), copy everything from @qq{Start
684 cut-&-pastable section} to the bottom of the file.
685
686
687 @ignore
688 This is item 825
689
690 @n ode Keyboard navigation
691 @s ubsection Keyboard navigation
692
693 @warning{This features is only available in the HTML manuals.}
694
695 @c TODO: once this is figured out, insert it here.
696
697 We are currently working on this feature.
698 @end ignore
699
700 @node Overview of manuals
701 @subsection Overview of manuals
702
703 There is a lot of documentation for LilyPond.  New users are
704 sometimes confused about what part(s) they should read, and
705 occasionally skip over reading vital portions.
706
707 @warning{Please do not skip over important parts of the
708 documentation.  You will find it much harder to understand later
709 sections.}
710
711 @itemize
712
713 @item
714 @strong{Before trying to do @emph{anything}}: read the Learning
715 manual's @ref{Tutorial}, and @ref{Common notation}.  If you
716 encounter musical terms which you do not recognize, please look
717 them up in the @rglosnamed{Top, Glossary}.
718
719 @item
720 @strong{Before trying to write a complete piece of music}: read
721 the Learning manual's @ref{Fundamental concepts}.  After that, you
722 may want to look in relevant sections of the
723 @rusernamed{Top, Notation reference}.
724
725 @item
726 @strong{Before trying to change the default output}: read the
727 Learning manual's @ref{Tweaking output}.
728
729 @item
730 @strong{Before undertaking a large project}: read the Usage
731 document's @rprogram{Suggestions for writing files}.
732
733 @end itemize
734
735
736