]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/user/tutorial.itely
patch::: 1.3.124.jcn1
[lilypond.git] / Documentation / user / tutorial.itely
1 @c -*-texinfo-*-
2
3 @node Tutorial
4 @chapter Tutorial
5
6 @menu
7 * Introduction::                   Introduction
8 * The first tune::                 The first tune
9 * Lyrics and chords::              Lyrics and chords
10 * More movements::                 More than one movement in a file
11 * Piano music::                    Piano music
12 * end of tutorial::                The end
13 @end menu
14
15 @node Introduction
16 @section Introduction
17
18   
19 LilyPond prints music from a specification that you, the user, supply.
20 You have to give that specification using a @emph{language}.  This
21 document is a gentle introduction to that language, which is called
22 Lilypond, an acronym of Music Definition Language.
23
24 This tutorial will demonstrate how to use Lilypond by presenting
25 examples of input along with resulting output.  We will use English
26 terms for notation.  In case you are not familiar with those, you may
27 consult the glossary that is distributed with LilyPond.
28
29 The examples discussed are included in the distribution, in the
30 subdirectory @file{input/tutorial/}.  It is recommended that you
31 experiment with writing Lilypond input yourself, to get a feel for
32 how LilyPond behaves.
33
34 @node The first tune
35 @section The first tune
36
37
38 To demonstrate what LilyPond input looks like, we start off with a
39 full fledged, yet simple example. It is a convoluted version
40 of the famous menuet in J. S. Bach's @emph{Klavierbuechlein}.
41
42 @lilypond[verbatim]
43 % lines preceded by a percent are comments which
44 % are ignored by Lilypond.
45 \include "paper16.ly"
46 \score {
47     \notes                        
48     \relative c'' \sequential{                
49             \time 3/4;                
50             \key g \major;
51
52         \repeat "volta" 2 {
53             d4 g,8 a b c d4 g, g |
54             e'4 c8 d e fis g4 g, g |
55             c4 d8()c b a( )b4 c8 b a g |
56             a4 [b8 a] [g fis] g2.  |
57         }
58
59         b'4 g8 a b g
60         a4 d,8 e fis d |
61         g4 e8 fis g d cis4 b8 cis a4 |
62         a8-. b-. cis-. d-. e-. fis-.
63         g4 fis e |
64         fis a,  r8 cis8
65         d2.-\fermata
66         \bar "|.";
67     }
68     \paper {
69        % standard settings are too wide for a book
70        linewidth = 14.0 \cm;
71    }
72 }
73 @end lilypond
74
75 Enter it (or copy it, the filename is @file{menuet.ly}), compile it
76 with LilyPond and view the output.  Details of this procedure may vary
77 from system to system.  To create the output, one would issue the
78 command `@code{ly2dvi menuet}'.  @file{ly2dvi} is a program that does
79 the job of running LilyPond and @TeX{}, handling of titles and
80 adjusting of page margins.
81
82 If all goes well, the file @file{menuet.dvi} will be created.
83 To view this output, issue the command `@code{xdvi menuet}'.
84
85 Now that we are familiar with the procedure of producing output, we
86 will analyse the input, line by line.
87 @example
88         % lines preceded by a percent are comments which
89         % are ignored by Lilypond.
90 @end example 
91 The percent sign, `@code{%}', introduces a line comment.  If you want to
92 make larger comments, you can use block comments. These are delimited
93 by `@code{%@{}' and `@code{%@}}'
94 @example 
95
96         \input "paper16.ly"
97  
98 @end example 
99 By default, LilyPond will use definitions for a 20
100 point@footnote{A point is the standard measure of length for
101 printing.  One point is 1/72.27 inch.} high staff.  We want smaller
102 output (16 point staff height), so we must import the settings for
103 that size, which is done.
104 @example 
105
106         \score @{
107  
108 @end example 
109   A lilypond file combines music with directions for outputting that
110 music.  The music is combined with the output directions by putting
111 them into a @code{\score} block.
112 @example 
113
114         \notes                
115  
116 @end example 
117  This makes LilyPond ready for accepting notes.
118 @example 
119
120         \relative c''
121  
122 @end example 
123  As we will see, pitches are combinations of octave, note name and
124 chromatic alteration.  In this scheme, the octave is indicated by
125 using raised quotes (`@code{'}') and ``lowered'' quotes (commas:
126 `@code{,}').  The central C is denoted by @code{c'}.  The C one octave
127 higher is @code{c''}.  One and two octaves below the central C is
128 denoted by @code{c} and @code{c,} respectively.
129
130 For pitches in a long piece you might have to type many quotes.  To
131 remedy this, LilyPond has a ``relative'' octave entry mode.  In this
132 mode, octaves of notes without quotes are chosen such that a note is
133 as close as possible (graphically, on the staff) to the the preceding
134 note.  If you add a high-quote an extra octave is added.  The lowered
135 quote (a comma) will subtract an extra octave.  Because the first note
136 has no predecessor, you have to give the (absolute) pitch of the note
137 to start with.
138 @example 
139
140         \sequential @{
141  
142 @end example 
143   What follows is sequential music, i.e.,
144 notes that are to be played and printed after each other.
145 @example 
146
147         \time 3/4;
148  
149 @end example 
150   This command changes the time signature of the current piece: a 3/4
151 sign is printed.  This command is also used to generate bar lines in
152 the right spots.
153 @example 
154
155         \key g \major;
156  
157 @end example 
158   This command changes the current key to G-major.  Although this
159 command comes after the @code{\time} command, in the output, the key
160 signature comes before the time signature: LilyPond knows about music
161 typesetting conventions.
162 @example 
163
164         \repeat "volta" 2
165  
166 @end example 
167   This command tells LilyPond that the following piece of music must
168 be played twice; @code{"volta"} means that volta brackets should be used
169 for alternatives---if there were any.
170 @example 
171
172         @{
173  
174 @end example 
175 The subject of the repeat is again sequential music.  Since
176 @code{\sequential} is such a common construct, a shorthand is provided:
177 just leave off @code{\sequential}, and the result is the same.
178 @example 
179
180         d4
181  
182 @end example 
183  This is a note with pitch @code{d} (determined up to octaves).  The
184 relative music was started with a @code{c''}, so the real pitch of this
185 note is @code{d''}.  The @code{4} designates the duration of the note
186 (it is a quarter note).
187 @example 
188
189         a b
190  
191 @end example 
192 These are notes with pitch @code{a} and @code{b}.  Because their
193 duration is the same as the @code{g}, there is no need to enter the
194 duration (You may enter it anyway, e.g. @code{a4 b4})
195 @example 
196
197         d4 g, g |
198  
199 @end example 
200  Three more notes.  The `@code{|}' character is a `bar check'.  When
201 processing the music, LilyPond will verify that bar checks are found at
202 the start of a measure.  This can help you track down errors.
203
204  So far, no notes were chromatically altered.  Here is the first one
205 that is: @code{fis}. Lilypond by default uses Dutch note names, and
206 ``Fis'' is the Dutch note name for ``F sharp''.  However, there is no
207 sharp sign in the output. The program keeps track of key signatures,
208 and will only print accidentals if they are needed.
209 @example 
210
211         c8 d e fis
212  
213 @end example 
214 LilyPond guesses were beams can be added to eighth and shorter notes.
215 In this case, a beam over 4 eighths is added.
216 @example 
217
218         c4 d8( )c b a( )b4 c8 b a g |
219  
220 @end example 
221   The next line shows how to make a slur:
222 the beginning and ending note of the slur is marked with an opening and
223 closing parenthesis respectively.  In the line shown above, this is
224 done for two slurs.  Slur markers (parentheses) are put between
225 the notes.
226 @example 
227
228         a4 [b8 a] [g fis] 
229  
230 @end example 
231 Automatic beaming can be overridden by inserting beam marks
232 (brackets).  Brackets are put around the notes you want beamed.
233 @example 
234
235         g2.  |
236  
237 @end example 
238 A duration with augmentation dot  is notated
239 with the duration number followed by a period.
240 @example 
241
242         @}
243  
244 @end example 
245   This ends the sequential music to be repeated.  LilyPond will typeset
246 a repeat bar.
247 @example 
248
249         cis'4 b8 cis a4 |
250  
251 @end example 
252  This line shows that Lily will print an accidental if that is
253 needed: the first C sharp of the bar will be printed with an accidental,
254 the second one without.
255 @example 
256
257         a8-. b-. cis-. d-. e-. fis-.
258  
259 @end example 
260 You can enter articulation signs either in a verbose form using a
261 shorthand.  Here we demonstrate the shorthand: it is formed by a dash
262 and the character for the articulation to use, e.g. `@code{-.}' for
263 staccato as shown above.
264 @example 
265
266         fis a, r8 cis8
267  
268 @end example 
269  
270 Rests are denoted by the special notename `@code{r}'.  You can also enter
271 an invisible rest by using the special notename `@code{s}'.
272 @example 
273
274         d2.-\fermata
275  
276 @end example 
277  All articulations have a verbose form, like @code{\fermata}.  The
278 command `@code{\fermata}' is not part of the core of the language (most
279 of the other discussed elements are), but it is a shorthand for a more
280 complicated description of a fermata.  @code{\fermata} names that
281 description and is therefore called an @emph{identifier}.
282 @example 
283
284         @}
285  
286 @end example 
287  
288 Here the music ends.
289 @example 
290
291         \paper @{
292                 linewidth = 14.0\cm;
293         @}
294  
295 @end example 
296 This specifies a conversion from music to notation output.  Most of
297 the details of this conversions (font sizes, dimensions, etc.) have
298 been taken care of, but  to fit the output  in this document, it has
299 to be smaller.  We do this by setting the line width to 14 centimeters
300 (approximately 6 inches).
301 @example 
302
303         @}
304  
305 @end example 
306 The last brace ends the @code{\score} block.
307
308 There are two things to note here. The format contains musical
309 concepts like pitches and durations, instead of symbols and positions:
310 the input format tries to capture the meaning of @emph{music}, and not
311 notation.  Second, the format tries to be @emph{context-free}:
312 a note will sound the same regardless of the current time signature,
313 the key, etc.
314
315 The purpose of LilyPond is explained informally by the term `music
316 typesetter'.  This is not a fully correct name: not only does the
317 program print musical symbols, it also makes esthetic decisions.  All
318 symbols and their placement is @emph{generated} from a high-level musical
319 description.  In other words,  LilyPond would be best
320 described by `music compiler' or `music to notation compiler'.
321
322
323
324
325 @node Lyrics and chords
326 @section Lyrics and chords
327
328 In this section we show how to typeset a song of unknown
329 origin.@footnote{The author would welcome information about the origin
330 of this song.}.
331
332 @example 
333 \header @{
334         title = "The river is flowing";
335         composer = "Traditional (?)";
336 @}
337 \include "paper16.ly"
338 melody = \notes \relative c' @{
339         \partial 8;
340         \key c \minor;
341         g8 |
342         c4 c8 d [es () d] c4 | f4 f8 g [es() d] c g |
343         c4 c8 d [es () d] c4 | d4 es8 d c4.
344         \bar "|.";
345 @}
346
347 text = \lyrics @{
348         The ri -- ver is flo- __ wing, flo -- wing and gro -- wing, the
349         ri -- ver is flo -- wing down to the sea.
350 @}
351
352 accompaniment =\chords @{
353         r8
354         c2:3- f:3-.7 d:min es4 c8:min r8
355         c2:min f:min7 g:7^3.5 c:min @}
356
357 \score @{
358         \simultaneous @{
359 %         \accompaniment
360           \context ChordNames \accompaniment
361
362           \addlyrics
363             \context Staff = mel @{        
364               \property Staff.noAutoBeaming = ##t
365               \property Staff.automaticMelismata = ##t
366               \melody 
367             @}
368             \context Lyrics \text
369         @}
370         \midi  @{ \tempo 4=72;@}
371         \paper @{ linewidth = 10.0\cm; @}
372 @} 
373 @end example 
374
375
376 The result would look this@footnote{The titling and font size shown
377 may differ, since the titling in this document is not generated by
378 @file{ly2dvi}.}.
379
380 @center @strong{The river is flowing}
381 @center Traditional 
382
383 @lilypond[center]
384 \header {
385         title = "The river is flowing";
386         composer = "Traditional (?)";
387 }
388 \include "paper16.ly"
389 melody = \notes \relative c' {
390         \partial 8;
391         \key c \minor;
392         g8 |
393         c4 c8 d [es () d] c4 | f4 f8 g [es() d] c g |
394         c4 c8 d [es () d] c4 | d4 es8 d c4.
395         \bar "|.";
396 }
397
398 text = \lyrics {
399         The ri -- ver is flo- __ wing, flo -- wing and gro -- wing, the
400         ri -- ver is flo -- wing down to the sea.
401 }
402
403 accompaniment =\chords {
404         r8
405         c2:3- f:3-.7 d:min es4 c8:min r8
406         c2:min f:min7 g:7^3.5 c:min }
407
408 \score {
409         \simultaneous {
410 %         \accompaniment
411           \context ChordNames \accompaniment
412
413           \addlyrics
414             \context Staff = mel {
415               \property Staff.noAutoBeaming = ##t
416               \property Staff.automaticMelismata = ##t
417               \melody 
418             }
419             \context Lyrics \text
420         }
421         \midi  { \tempo 4=72;}
422         \paper { linewidth = 10.0\cm; }
423 }
424 @end lilypond
425
426 Again, we will dissect the file line by line.
427 @example 
428
429         \header @{
430  
431 @end example 
432 Information about the music you are about to typeset goes into a
433 @code{\header} block.  The information in this block is not used by
434 LilyPond, but it is included in the output.  @file{ly2dvi} uses this
435 information to print titles above the music.
436 @example 
437
438         title = "The river is flowing";
439         composer = "Traditional (?)"; 
440 @end example 
441 the @code{\header} block contains assignments.  An assignment starts
442 with a string.  (which is unquoted, in this case). Then comes the
443 equal sign `@code{=}'.  After the equal sign comes the expression you
444 want to store.  In this case, you want to put in strings.  The
445 information has to be quoted here, because it contains spaces. Each
446 assignment is finished with a semicolon.
447 @example 
448
449         \include "paper16.ly"
450  
451 @end example 
452 Smaller size for inclusion in a book.
453 @example 
454
455         melody = \notes \relative c' @{
456  
457 @end example 
458 The structure of the file will be the same as the previous one, a
459 @code{\score} block with music in it.  To keep things readable, we will
460 give names to the different parts of music, and use the names to
461 construct the music within the score block.
462
463 @example 
464         \partial 8;
465 @end example 
466
467 The piece starts with an anacrusis of one eighth.
468 @example
469         \key c \minor;
470 @end example
471 The key is C minor: we have three flats.
472
473 @example 
474
475         c4 c8 d [es () d] c4 | f4 f8 g [es() d] c g |
476         c4 c8 d [es () d] c4 | d4 es8 d c4.
477         \bar "|.";
478  
479 @end example 
480
481 We use explicit beaming.  Since this is a song,  we will turn automatic
482 beams off, and use explicit beaming where needed.
483 @example 
484
485         @}
486  
487 @end example 
488 This ends the definition of @code{melody}.  Note that there are no
489 semicolons after assignments at top level.
490 @example 
491
492         text = \lyrics @{
493  
494 @end example 
495 Another identifier assignment.  This one is for the lyrics. 
496 Lyrics are formed by syllables that have duration, and not by
497 notes. To make LilyPond parse words as syllables,  switch it  into
498 lyrics mode with @code{\lyrics}.  Again, the brace after @code{\lyrics}
499 is a shorthand for @code{\sequential @{}.
500 @example 
501
502   The4 ri -- ver is flo- __ wing,  flo -- wing and gro -- wing, the
503   ri- ver is flo- __ wing down to the sea.
504 @}
505  
506 @end example 
507 The syllables  themselves are  separated by spaces.  You can get syllable
508 extenders by entering `@code{__}', and centered hyphens with
509 `@code{-}@code{-}'.  We enter the syllables as if they are all quarter notes
510 in length (hence the @code{4}), and use a feature to align the
511 syllables to the music (which obviously isn't all quarter notes.)
512 @example 
513
514         accompaniment =\chords @{
515  
516 @end example 
517 We'll put chords over the music.  There is a special mode (analogous
518 to @code{\lyrics} and @code{\notes} mode) where you can give the names
519 of the chords you want, instead of the notes comprising the chord.
520 @example 
521
522         r8
523  
524 @end example 
525 There is no accompaniment during the anacrusis.
526 @example 
527
528         c2:3- f:3-.7
529  
530 @end example 
531 A chord is started by  the tonic of the chord. The
532 first one lasts a half note.  An unadorned note creates a major
533 triad, while a minor triad is wanted.  @code{3-} modifies the third to
534 be small. @code{7} modifies (adds) a seventh, which is small by default
535 to create the @code{f a c es} chord.  Multiple modifiers must be
536 separated by a dot.
537 @example 
538
539         d:min es4 c8:min r8
540  
541 @end example 
542 Some modifiers have predefined names, eg. @code{min} is  the same as
543 @code{3-}, so @code{d-min} is a minor @code{d} chord.
544 @example 
545
546         c2:min f:min7 g:7^3.5 c:min @}
547  
548 @end example 
549 A named modifier @code{min} and a normal modifier @code{7} do not have
550 to be separated by a dot.  Tones from a chord are removed with chord
551 subtractions.  Subtractions are started with a caret, and they are
552 also separated by dots.  In this example, @code{g:7^3.5} produces a
553 minor seventh.  The brace ends the sequential music.
554 @example 
555
556         \score @{
557                 \simultaneous @{
558  
559 @end example 
560 We assemble the music in the @code{\score} block.  Melody, lyrics and
561 accompaniment have to sound at the same time, so they should be
562 @code{\simultaneous}.
563 @example 
564
565         %\accompaniment
566  
567 @end example 
568 Chord mode generates notes grouped in @code{\simultaneous} music.  If
569 you remove the comment sign, you can see the chords in normal
570 notation: they will be printed as note heads on a separate
571 staff.
572 @example 
573
574         \context ChordNames \accompaniment
575  
576 @end example 
577 Normally, the notes that you enter are transformed into note heads.
578 The note heads alone make no sense, they need surrounding information:
579 a key signature, a clef, staff lines, etc.  They need @emph{context}.  In
580 LilyPond, these symbols are created by objects called `interpretation
581 contexts'.  Interpretation contexts only exist during a run of
582 LilyPond.  Interpretation contexts that are for printing music (as
583 opposed to playing music) are called `notation contexts'.
584
585 By default, LilyPond will create a Staff context for you.  If you
586 removed the @code{%} sign in the previous line, you would see that
587 mechanism in action.
588
589 We don't want default contexts here, because we want chord names, not
590 note heads.  An interpretation context can also created upon explicit
591 request. The keyword for such a request is @code{\context}.  It takes
592 two arguments.  The first is the name of an interpretation context.
593 The name is a string, it can be quoted with double quotes).  The
594 second argument is the music that should be interpreted in this
595 context.  For the previous line, we could have written @code{\context
596 Staff \accompaniment}, and get the same effect.
597 @example 
598
599         \addlyrics
600  
601 @end example 
602 The lyrics need to be aligned with the melody.  This is done by
603 combining both with @code{\addlyrics}.  @code{\addlyrics} takes two
604 pieces of music (usually a melody and lyrics, in that order) and
605 aligns the syllables of the second piece under the notes of the
606 first piece.  If you would reverse the order, the notes would be
607 aligned on the lyrics, which is not very useful. (Besides, it looks
608 silly.)
609 @example 
610
611         \context Staff = mel @{
612  
613 @end example 
614 This is the argument of @code{\addlyrics}.  We instantiate a
615 @code{Staff} context explicitly: should you chose to remove the comment
616 before the ``note heads'' version of the accompaniment, the
617 accompaniment will be on a nameless staff.  The melody has to be on a
618 different staff as the accompaniment.  This is accomplished by giving
619 the melody staff a different name.
620 @example 
621
622         \property Staff.noAutoBeaming = ##t
623  
624 @end example 
625 An interpretation context has variables that tune its behaviour.  One
626 of the variables is @code{noAutoBeaming}.  If set and non-zero (i.e.,
627 true) LilyPond will not try to put automatic beaming on the current
628 staff.
629 @example 
630
631         \property Staff.automaticMelismata = ##t
632  
633 @end example 
634 Similarly, we  don't want to print a  syllable when there is
635 a slur. This sets up the Staff context to signal slurs while
636 @code{\addlyrics} is processed.
637 @example 
638
639           \melody
640         @}
641  
642 @end example 
643 Finally, we put the melody on the current staff.  Note that the
644 @code{\property} directives and @code{\melody} are grouped in sequential
645 music,  so the property settings are done before the melody is
646 processed.
647 @example 
648
649         \context Lyrics \text
650  
651 @end example 
652 The second argument of @code{\addlyrics} is the text. The text also
653 should not land on a Staff, but on a interpretation context for
654 syllables, extenders, hyphens etc.  This context is called
655 Lyrics.
656 @example 
657
658         @}
659  
660 @end example 
661 This ends @code{\simultaneous}.
662 @example 
663
664         \midi  @{ \tempo 4=72;@}
665  
666 @end example 
667 This makes the music go to a MIDI file.  MIDI is great for
668 checking music you enter.  You listen to the MIDI file: if you hear
669 something unexpected, it's probably a typing error.  @code{\midi} is an
670 `output definition', a declaration that specifies how to output music
671 analogous to @code{\paper @{ @}}. You can specify the tempo using the
672 @code{\tempo} command, in this case the tempo of quarter notes is set
673 to 72 beats per minute.
674 @example 
675
676         \paper @{ linewidth = 10.0\cm; @}
677  
678 @end example 
679 We also want notation output.  The linewidth is short so the piece
680 will be set in two lines.
681 @example 
682
683         @}
684  
685 @end example 
686 End the score block.
687
688 @node More movements 
689 @section More movements
690
691 You probably ran @file{ly2dvi} on the last example, and ended up with a
692 viewable @file{.dvi} file.  However, between there are a few steps of
693 which LilyPond is only one. To enhance your understanding of what's
694 happening under the hood when you run ly2dvi,  we explain what programs
695 are run.   
696
697 @code{ly2dvi} is a program that calls a number of programs  in sequence.
698 The first thing it does, is running LilyPond on the input file. After
699 some calculations, lily comes up with a @file{.tex} file. The contents
700 of this file are very  low-level instructions.
701
702 For example, if you'd put the following in a file called
703 @file{layout.ly},
704
705 @example
706   \version "1.3.124";
707   \header @{ title = "Two miniatures";  @}
708   
709   #(set! point-and-click #t)
710   
711   \paper @{
712         linewidth = -1.0; @}
713
714   \score @{
715     \notes @{ c'4 d'4 @}
716     \header @{
717         opus = "Opus 1.";
718         piece = "Up"; @}
719   @}
720   \score @{
721     \notes @{ d'4 c'4  @}
722     \header @{
723         opus = "Opus 2.";
724         piece = "Down"; @}
725   @}
726 @end example
727
728 The result should look somewhat like this@footnote{The titling in this
729 manual was not generated by ly2dvi, so details will differ.}
730
731 @center @strong{Two miniatures}
732 @flushright
733   Opus 1.
734 @end flushright
735 @flushleft
736 @var{Up}
737 @end flushleft
738 @lilypond
739   \score {
740     \notes { c'4 d'4 }
741     \paper {
742         linewidth = -1.0; }
743   }
744 @end lilypond
745 @flushright
746   Opus 2.
747 @end flushright
748 @flushleft
749 @var{Down}
750 @end flushleft
751 @lilypond
752   \score {
753     \notes { d'4 c'4 }
754     \paper {
755         linewidth = -1.0; }
756   }
757 @end lilypond
758
759 This file is produced by ly2dvi in a few stages, with the help of text
760 formatting tools. LilyPond produces two output files, @file{layout.tex}
761 and @file{layout-1.tex}.  They both look like this:
762
763 @example
764         ...
765   \placebox@{-5  \outputscale @}%
766   @{  8.7229  \outputscale @}%
767   @{\magfontWXGEomMMBo\char90 @}%
768   
769   \placebox@{-4  \outputscale @}%
770   @{ 81.0647  \outputscale @}%
771         ...
772 @end example
773
774 @file{ly2dvi} analyses the progress indication that LilyPond spews out,
775 and generates a file called @file{layout_ly1.tex}. This file contains
776 formatting instructions for the title and page layout.  A fragment might
777 look like
778 @example
779
780         \geometry@{width=540.602362pt,headheight=2mm, ...
781         \renewcommand@{\@@oddfoot@}@{\parbox@{\textwidth@}@{\mbox@{@}   ...
782         \begin@{document@}
783         \lilypondtitle@{foo@}%
784         \makelilytitle
785         \input@{ly2dvi.tex@}
786
787 @end example
788
789 @file{ly2dvi} runs it through LaTeX. LaTeX is a text-formatting system
790 built on top of @TeX{}. It's very popular in the academic world. If LaTeX
791 is successful, this will produce a @file{.dvi} file, containing both the
792 titling and notes.  @code{ly2dvi} completes its task by deleting the two
793 temporary files, leaving only @file{layout.dvi}.
794
795 Next, now we'll look at the examples line by line to explain new things.
796
797 @example 
798 \version "1.3.124";
799 @end example 
800 Lilypond and its language are still under development, and occasionally,
801 details of the syntax are changed. This fragment indicates for which
802 version the input file was written. When you compile this file, the
803 version number will be checked, and you will get a warning when the file
804 is too old.
805
806 This version number is also used by the @code{convert-ly} program (See
807 @ref{convert-ly}), which uses it update the file to the latest lily
808 version.
809
810 @example
811   \header @{ title = "Two miniatures";  @}
812 @end example
813 This sets the titling information for the entire file.
814
815 @example
816         #(set! point-and-click #t)
817 @end example
818
819 Editing input files can be quite complicated if you're working with
820 large files: if you're digitizing existing music, you have to
821 synchronize the .ly file, the sheet music on your lap and the sheet
822 music on the screen.  The point-and-click mechanism makes it easy to
823 find the origin of an error in the .ly file: @footnote{This feature is
824 presently only available on X-windows using a patched version of xdvi
825 and emacs} when you view the file with Xdvi and click on a note using
826 control-right button, [checkme], you editor will jump to the spot where
827 that note was entered.
828
829 More information is in in @ref{Point and click} 
830
831 @example
832   \paper @{ 
833 @end example
834
835 The @code{\score} blocks that follow in the file, don't have
836 @code{\paper} sections, so the settings of this block are substituted: A
837 paper block, at top-level, i.e. not in a @code{\score} block sets the
838 default page layout.
839
840 @example
841   linewidth = -1.0; @}
842 @end example
843
844
845
846 The variable @code{linewidth} normally sets the length of the systems on
847 the page. However, a negative value has a special meaning here. If
848 @code{linewidth} is less than 0, no line breaks are inserted into the
849 score, and the spacing is set to natural length: a short phrase takes up
850 little space, a longer phrase more space.
851
852 @example
853   \score @{
854     \notes @{ c'4 d'4 @}
855 @end example
856
857 In previous examples, notes were specified in relative octaves,
858 i.e. each note was put in the octave that would put it closest to its
859 predecessor. Besides relative, there is also absolute octave
860 specification, and it is turned on by default. In this input mode, the
861 central C is denoted by @code{c'}. Going down, you get @code{c}
862 @code{c,} @code{c,,} etc.  Going up, you get @code{c''} @code{c'''} etc.
863
864 When you're copying music from existing sheet music, relative octaves
865 are probably the easiest to use: it's less typing work and errors are
866 easily spotted. However, if you write LilyPond input, either by hand
867 (ie. composing) or by computer, absolute octaves is probably less work.
868
869
870 @example
871     \header @{
872 @end example
873
874 The @code{\header} is normally at the top of the file, where it sets
875 values for the rest of the file. If you want to typeset different pieces
876 from one file (eg. if there are multiple movements, or if you're making
877 a etude-book), you can put different  @code{\score}
878 blocks into  the input file. ly2dvi will assemble all LilyPond
879 output files into a big document. The contents of \header blocks
880 specified within each score, are used for the titling of each movement.
881 @example
882         opus = "Opus 1.";
883         piece = "Up"; @}
884 @end example
885 For example, the Opus number is put at the right, and the piece string
886 will be at the left.
887
888
889
890 @node Piano music
891 @section Piano music
892
893 Our third subject is a piece of piano music.  The fragment in the input
894 file is a piano reduction of the G major Sinfonia by Giovanni Battista
895 Sammartini.  It was composed around 1740. 
896
897 @lilypond[verbatim]
898 \include "paper16.ly";
899
900 stemdown = \property Voice.Stem \override #'direction = #-1
901 stemup = \property Voice.Stem \override #'direction = #1
902 stemboth = \property Voice.Stem \revert #'direction  
903
904 viola = \notes \relative c' \context Voice = viola {
905         <c4-\f g' c>
906         \stemdown g'8. b,16
907         s1 s2. r4
908         g
909 }
910
911 oboes = \notes \relative c'' \context Voice = oboe {
912         \stemup s4  g8. b,16 c8 r <e'8.-\p g> <f16 a>
913         \grace <e8( g> <d4 f> <c2 e> \times 2/3 { <d8 \< f> <e g> <f a> }
914         <
915           { \times 2/3 { a8 g c } \! c2 }
916           \context Voice = oboeTwo {
917                 \stemdown
918                 \grace {
919                   \property Grace.Stem \override #'direction = #-1
920                   [f,16 g] }
921                 f8 e e2
922         } >
923         \stemboth
924         \grace <c,8( e> <)b8. d8.-\trill> <c16 e> | 
925         [<d ( f> < )f8. a>] <)b,8 d> r [<d16( f> <f8. )a>] <b,8 d> r  |
926         [<c16( e>  < )e8. g>] <c8 e,>
927 }
928
929 hoomPah  = \notes \repeat unfold 8 \transpose c' { c8 \stemdown c'8 \stemup }
930
931 bassvoices = \notes \relative c' {
932         c4 g8. b,16
933         \autochange Staff \hoomPah o
934         \translator Staff = down
935         \stemdown [c8 c'8] r4
936         <g d'> r4
937         < {\stemup r2 <e4 c'> <c8 g'> }
938           \context Voice = reallyLow  {\stemdown g2 ~ | g4 c8 } >
939 }
940
941 \score {
942         \context PianoStaff \notes <
943                 \context Staff = up < \time 2/2;
944                         \viola
945                         \oboes
946                 >
947                 \context Staff = down < \time 2/2; \clef bass;
948                         \bassvoices
949                 >
950         >
951         \midi { }
952         \paper {
953           indent = 0.0;
954           linewidth = 15.0 \cm; }
955 }
956 @end lilypond
957
958 If it looks like incomprehensible gibberish to you, then you are right.
959 This example has been doctored this example to have as many quirks as
960 possible.
961
962 @example
963 stemdown = \property Voice.Stem \override #'direction = #-1
964 stemup = \property Voice.Stem \override #'direction = #1
965 stemboth = \property Voice.Stem \revert #'direction  
966 @end example
967
968 [explain grob push/pop] 
969
970 @example 
971 viola = \notes \relative c'  \context Voice = viola @{ 
972 @end example 
973 In this example, you can see multiple parts on a staff.  Each part is
974 associated with one notation context.  This notation context handles
975 stems and dynamics (among others).  The name of this context is
976 @code{Voice}.  For each part we have to make sure that there is
977 precisely one Voice context.
978 @example 
979 <c4-\f g' c> 
980 @end example 
981 @code{<} and @code{>} are short hands for @code{\simultaneous @{} and
982 @code{@}}. So the expression enclosed in @code{<} and @code{>} is a
983 chord.  @code{\f} places a forte symbol  under the chord.
984
985 @example 
986    \stemdown
987 @end example 
988
989
990 @example 
991         g'8. b,16 
992 @end example 
993 Relative octaves work a little differently with chords.  The starting
994 point for the note following a chord is the first note of the chord.  So
995 the @code{g} gets an octave up quote: it is a fifth above the starting
996 note of the previous chord (the central C).
997
998 @example 
999 s1 s2. r4 
1000 @end example 
1001 @code{s} is a `spacer' rest.  It does not print anything,  but it does
1002 have the duration of a rest.
1003 @example 
1004 oboes = \notes \relative c'' \context Voice = oboe @{ 
1005 @end example 
1006 Now comes a part for two oboes.  They play homophonically, so we
1007 print the notes as one voice that makes chords. Again, we insure that
1008 these notes are indeed processed by precisely one context with
1009 @code{\context}.
1010 @example 
1011 \stemUp s4  g8. b,16 c8 r <e'8.-\p g> <f16 a> 
1012 @end example 
1013 @code{\stemUp} is an identifier reference.  It is shorthand for
1014 @code{\property Voice.Stem \override #'direction = #1}.  If possible, you
1015 should use predefined identifiers like these for setting properties.
1016 Your input will be less dependent upon the implementation of LilyPond.
1017 @example 
1018 \grace <e8( g> < )d4 f> <c2 e> 
1019 @end example 
1020 @code{\grace} introduces grace notes.  It takes one argument, in this
1021 case a chord.  The slur started on the @code{e} of the chord
1022 will be attached to the next note.@footnote{LilyPond will squirm
1023 about unended Slurs.  In this case, you can ignore the warning}.
1024 @example 
1025 \times 2/3 
1026 @end example 
1027 Tuplets are made with the @code{\times} keyword.  It takes two
1028 arguments: a fraction and a piece of music.  The duration of the
1029 second argument is multiplied by the first argument.  Triplets make
1030 notes occupy 2/3 of their notated duration, so in this case the
1031 fraction is 2/3.
1032 @example 
1033 @{ <d8 \< f> <e g> <f a> @} 
1034 @end example 
1035 The piece of music to be `tripletted' is sequential music containing
1036 three notes.  On the first chord (the @code{d}), a crescendo is started
1037 with @code{\<}.
1038 @example 
1039
1040 @end example 
1041 At this point, the homophonic music splits into two rhythmically
1042 different parts.  We can't use a sequence of chords to enter this, so
1043 we make a `chord' of sequences to do it.  We start with the upper
1044 voice, which continues with upward stems:
1045 @example 
1046  @{ \times 2/3 @{ a8 g c @} \! c2 @} 
1047 @end example 
1048 The crescendo is ended at the half note by the escaped exclamation
1049 mark `@code{\!}'.
1050 @example 
1051 \context Voice = oboeTwo @{
1052 \stemDown 
1053 @end example 
1054 We can't share stems with the other voice, so we have to create a new
1055 @code{Voice} context.  We give it the name @code{oboeTwo} to distinguish
1056 it from the other context.  Stems go down in this voice.
1057 @example 
1058 \grace @{  
1059 @end example 
1060 When a grace section is processed, a @code{Grace} context is
1061 created. This context acts like a miniature score of its own.  It has
1062 its own time bookkeeping, and you can make notes, beams, slurs
1063 etc. Here we fiddle with a property and make a beam.  The argument of
1064 @code{\grace} is sequential music.
1065
1066 @example 
1067 \property Grace.Stem \override #'direction = #-1
1068 [f,16 g] @}
1069 @end example 
1070
1071 Normally, grace notes are always stem up, but in this case, the upper
1072 voice interferes. We set the stems down here.
1073
1074 As far as relative mode is concerned, the previous note is the
1075 @code{c'''2} of the upper voice, so we have to go an octave down for
1076 the @code{f}.
1077 @example 
1078
1079   f8 e e2
1080 @} > 
1081 @end example 
1082 This ends the two-part section.
1083 @example 
1084 \stemBoth
1085 \grace <c,8( e> <)b8. d8.-\trill> <c16 e> |  
1086 @end example 
1087 @code{\stemBoth} ends the forced stem directions. From here, stems are
1088 positioned as if it were single part music.
1089
1090 The bass has a little hoom-pah melody to demonstrate parts switching
1091 between staffs.  Since it is repetitive, we use repeats:
1092 @example 
1093 hoomPah  = \notes  \repeat unfold 8
1094 @end example
1095
1096 @example
1097 \transpose c' @{
1098 @end example
1099 Transposing can be done with @code{\transpose}.  It takes two
1100 arguments; the first specifies what central C should be transposed to.
1101 The second is the to-be-transposed music.  As you can see, in this
1102 case, the transposition is a no-op, as  central C would be transposed to
1103 central C.
1104
1105 The purpose of this no-op is circumventing relative mode.  Relative mode
1106 can not be used in conjunction with transposition, so relative mode will
1107 leave the contents of @code{\hoomPah} alone.  We can use it without
1108 having to worry about getting the motive in a wrong octave.
1109 @example 
1110 bassvoices = \notes \relative c' @{
1111 c4 g8. b,16
1112 \autochange Staff \hoomPah 
1113 @end example 
1114 Entering the bass part is easy: the hoomPahHoomPah variable is
1115 repeated four times; @code{unfold} means that all four repetitions
1116 should be written out.
1117
1118 @example
1119         \translator Staff = down
1120 @end example
1121
1122 @example 
1123 \context Voice = reallyLow  @{\stemDown g2 ~ | g4 c8 @} > 
1124 @end example 
1125 After skipping some lines, we see @code{~}.  This mark makes ties.
1126 @example 
1127 \context PianoStaff 
1128 @end example 
1129 For piano music, a special context is needed to get cross staff
1130 beaming right.  It is called @code{PianoStaff}.
1131 @example 
1132 \context Staff = bottom < \time 2/2; \clef bass; 
1133 @end example 
1134 The bottom staff must have a different clef.
1135 @example 
1136 indent = 0.0; 
1137 @end example 
1138 To make some more room on the line, the first (in this case the only)
1139 line is not indented.  The line still looks very cramped, but that is due
1140 to the page layout of this document.
1141
1142 [TODO:
1143
1144 Split piano in 2
1145
1146 * Piano I:  autochange, simple chords, arpeggio, glissando, tuplets
1147 unfolded repeat, space rests.
1148
1149 * Piano II: property push/pop, grace notes, multiple voices,
1150 dynamics,  stem up/stem down, 
1151
1152 * Orchestral: demonstrate Hara-Kiri, part combining, part extraction,
1153 scores, transposition, instrument names,
1154
1155 ]
1156
1157 @node  end of tutorial
1158 @section The end        
1159          
1160 That's all folks.  From here, you can either try fiddling with input
1161 files, or you can read the reference manual.