@c -*-texinfo-*-

@c TODO:
@c   * more details about running lilypond; error messages,
@c     compiling/viewing (emacs?)
@c   * where to go from  First steps+More basics?

@node Tutorial
@chapter Tutorial



Using LilyPond comes down to encoding music in an input file. After
entering the music, the program is run on the file producing output
which can be viewed or printed.  In this tutorial, we will show step
by step how to enter such files, and illustrate the process with
fragments of input and the corresponding output.  At the end of every
section, a paragraph will list where to find further information on
the topics discussed.

Many people learn programs by trying and fiddling around with the
program.  This is also possible with LilyPond. If you click on a
picture in the HTML version of this manual, you will see the exact
LilyPond input that was used to generate that image.
@ifhtml
For example, consider the following input:
@c TODO: intertext fixme
@lilypond[relative 1,singleline,verbatim,intertext="with the following output:"]
  c'^\markup { \bold \huge { Click on this image! } }
@end lilypond

@end ifhtml
By cutting and pasting the full input into a test file, you have a
starting template for experiments. If you like learning in this way,
you will probably want to print out or bookmark
@ifhtml
the
@end ifhtml
@ref{Cheat sheet}, which is a table listing all commands for quick
reference.


This tutorial starts with a short introduction to the LilyPond music
language.  After this first contact, we will show you how to to
produce printed output.  You should then be able to create and print
your first sheets of music.

@menu
* First steps::                 
* Running LilyPond::            
* More about pitches ::         
* Octave entry::                
* Combining music into compound expressions::  
* Adding articulation marks to notes::  
* Combining notes into chords::  
* Printing lyrics::             
* A lead sheet::                
* Listening to output::         
* Titling::                     
* Single staff polyphony::      
* Piano staves::                
* Setting variables::           
* Fine tuning layout::          
* Organizing larger pieces::    
* An orchestral part::          
* Integrating text and music::  
@end menu


@node First steps
@section First steps

We start off by showing how very simple music is entered in LilyPond:
you get a note simply by typing its note name, from @samp{a}
through @samp{g}.  So if you enter

@example
c d e f g a b
@end example

@noindent
then the result looks like this:

@c ?
@c \transpose c c' { c d e f g a b }
@c @lily pond[notime]
@c \property Score.timing = ##f
@lilypond[notime, relative=2]
c d e f g a b
@end lilypond

The length of a note is specified by adding a number, @samp{1} for a
@rglos{whole note}, @samp{2} for a @rglos{half note}, and so on:

@example
a1 a2 a4 a16 a32
@end example

@lilypond[notime]
\property Score.timing = ##f
\property Staff.autoBeaming = ##f
\transpose c c' { a1 a2 a4 a16 a32 s16_" " }
@end lilypond

If you do not specify a @rglos{duration}, the previous one is used: 

@example
a4 a a2 a
@end example

@lilypond[notime]
\property Score.timing = ##f
\transpose c c' { a a a2 a s16_" " }
@end lilypond


Rests are entered just like notes, but with the name ``@code{r}'':

@cindex rests
@quotation
@example
r2 r4 r8 r16
@end example

@lilypond[fragment]
\property Score.timing = ##f
\property Staff.Clef = \turnOff
\property Staff.TimeSignature = \turnOff
r2 r4 r8 r16
s16_" "
@end lilypond
@end quotation
@separate


Add a dot @samp{.} after the duration to get a @rglos{dotted note}:

@example
a2. a4 a8. a16
@end example

@lilypond[notime]
\property Score.timing = ##f
\transpose c c' { a2. a4 a8. a16 s16_" " }
@end lilypond


The @rglos{meter} (or @rglos{time signature}) can be set with the
@code{\time} command:

@example
\time 3/4
\time 6/8
\time 4/4
@end example

@c a clef here may lead to confusion
@lilypond
\property Staff.Clef \set #'transparent = ##t 
\time 3/4
s4_" "
\time 6/8
s4_" "
\time 4/4
s16_" "
@end lilypond


The @rglos{clef} can be set using the @code{\clef} command:

@c what is more common name treble or violin?
@c in Dutch, its violin.
@c in English its definitely treble.
@example
\clef treble
\clef bass
\clef alto
\clef tenor
@end example

@lilypond[notime]
\property Score.timing = ##f
\clef violin
s4_" "
\clef bass
s4_" "
\clef alto
s4_" "
\clef tenor
s16_" "
@end lilypond

Notes and commands like @code{\clef} and @code{\time} , are enclosed
in @code{\notes @{@dots{}@}}.  This indicates that music (as opposed
to @rglos{lyrics}) follows:

@example
\notes @{
  \time 3/4
  \clef bass
  c2 e4 g2.
  f4 e d c2 r4
@}
@end example
Now the piece of music is almost ready to be printed.  The final step is to
combine the music with a printing command.

The printing command is the so-called @code{\paper} block:

@example
\paper @{ @} 
@end example

The @code{\paper} block is used to customize printing specifics. The
customization commands go between @code{@{} and @code{@}}, but for
now, we accept the defaults.  The music and the @code{\paper} block
are combined by enclosing them in @code{\score @{ ... @}}, so the
following is a complete and valid input file:

@example
\score @{
  \notes @{
    \time 3/4
    \clef bass
    c2 e4 g2.
    f4 e d c2 r4
  @}
  \paper @{ @}
@}
@end example

@lilypond[noindent]
\score {
  \notes {
     \time 3/4
     \clef bass
     c2 e4 g2.
     f4 e d c2 r4
  }
  \paper {
    linewidth = 55 * \staffspace
  }
}
@end lilypond

In the rest of the tutorial we will often leave out @code{\score}
and @code{\paper} for clarity. However, both must be present when
feeding the file to LilyPond.

For more elaborate information on

@table @asis
@item  entering pitches and durations
see 
@ref{Pitches} and @ref{Durations}.
@item Clefs
see @ref{Clef}
@item Time signatures and other timing commands
see  @ref{Time signature}.
@end table 

@node Running LilyPond
@section Running LilyPond

In the last section we explained what kind of things you could enter
in a LilyPond file.  In this section we explain what commands to run
and how to view or print the output.  If you have not used LilyPond
before, want to test your setup, or want to run an example file
yourself, read this section.  The instructions that follow are for
Unix-like systems.  Some additional instructions for Microsoft Windows
are given at the end of this section.

Begin by opening a terminal window and starting a text editor.  For
example, you could open an xterm and execute
@code{joe}.@footnote{There are macro files for VIM addicts, and there
is a @code{LilyPond-mode} for Emacs addicts. If it has not been
installed already, then refer to the file @file{INSTALL.txt}}.  In
your text editor, enter the following input and save the file as
@file{test.ly}:

@quotation
@example
\score @{
  \notes @{ c'4 e' g' @}
@} 
@end example
@end quotation

To process @file{test.ly}, proceed as follows:

@quotation
@example
lilypond test.ly
@end example
@end quotation

You will see something resembling:

@quotation
@example
GNU LilyPond 1.8.0
Now processing: `/home/fred/ly/test.ly'
Parsing...
Interpreting music...[1]
 @emph{ ... more interesting stuff ... }
PDF output to `test.pdf'...
DVI output to `test.dvi'...
@end example
@end quotation
@cindex DVI file
@cindex Viewing music
@cindex xdvi

The result is the file @file{test.pdf}.@footnote{For @TeX{}
afficionados: there is also a @file{test.dvi} file. It can be viewed
with @code{xdvi}. The DVI uses a lot of PostScript specials, which do
not show up in the magnifying glass. The specials also mean that the
DVI file cannot be processed with @code{dvilj}. Use @code{dvips} for
printing.
@cindex dvips
@cindex dvilj
@cindex DVI driver
}  One of the following commands should put the PDF on your
screen:
@quotation
@example
  gv test.pdf
  ghostview test.pdf
  ggv test.pdf
  kghostview test.pdf
  xpdf test.pdf
  gpdf test.pdf
  acroread test.pdf
  gsview32 test.pdf
@end example
@end quotation

@noindent
If the music on your screen looks good, you can print it by clicking
File/Print inside your viewing program.

@cindex Ghostscript
@cindex @code{lpr}
@cindex Printing output
@cindex PostScript
@cindex PDF


On Windows, the same procedure should work, the terminal is started by
clicking on the LilyPond or Cygwin icon.  Any text editor (such as
NotePad, Emacs or Vim) may be used to edit the LilyPond file.

To view the PDF file, try the following:
@itemize
@item
If your system has a PDF viewer installed, open
@file{C:\Cygwin\home\@var{your-name}} in the explorer and double-click
@file{test.pdf}.
@item
If you prefer the keyboard, you can try to enter one of the commands
from the list shown before in the terminal. If none work, go to
@uref{http://www.cs.wisc.edu/~ghost/} to install the proper software.
@end itemize

The commands for formatting and printing music on all platforms are
detailed in @ref{Invoking LilyPond}.

@node More about pitches 
@section More about pitches 

A @rglos{sharp} (@texisharp{}) pitch is made by adding @samp{is} to
the name, a @rglos{flat} (@texiflat{}) pitch by adding @samp{es}.  As
you might expect, a @rglos{double sharp} or @rglos{double flat} is
made by adding @samp{isis} or @samp{eses}:@footnote{This syntax
derived from note naming conventions in Nordic and Germanic languages,
like German and Dutch.}

@example
cis1 ees fisis aeses
@end example

@lilypond[notime]
\property Score.timing = ##f
\transpose c c' { cis1 ees fisis aeses s16_" " }
@end lilypond

 
@cindex key signature, setting

The key signature is set with the command ``@code{\key}'', followed by
a pitch and @code{\major} or @code{\minor}:
@quotation
@example
\key d \major
g1
\key c \minor
g
@end example

@lilypond[fragment]
\property Staff.TimeSignature = \turnOff
\key d \major
g'1
\key c \minor
g'
@end lilypond
@end quotation


Key signatures together with the pitch (including alterations) are
used together to determine when to print accidentals.  This is a
feature that often causes confusion to newcomers, so let us explain it
in more detail:


LilyPond has a sharp distinction between musical content and
layout. The alteration (flat, natural or sharp) of a note is part of
the pitch, and is therefore musical content. Whether an accidental (a
flat, natural or sharp @emph{sign}) is a printed in front of the
corresponding note is a question of layout. Layout is something that
follows rules, so accidentals are printed automatically according to
those rules.  The pitches in your music are works of art, so they will
not be added automatically, and you must enter what you want to hear.

For example, in this example:
@lilypond[fragment]
\property Staff.TimeSignature = #'()
\key d \major
d' cis' fis'
@end lilypond

@noindent
no note gets an explicit accidental, but still you enter

@example
\key d \major
d cis fis
@end example

The code @code{d} does not mean ``print a black dot just below the
staff.'' Rather, it means: ``a note with pitch D-natural.'' In the key
of A-flat, it gets an accidental:

@lilypond[fragment]
\property Staff.TimeSignature = #'()
\key as \major
d'
@end lilypond

@noindent
@example
\key as \major
d
@end example

Adding all alterations explicitly might require some more effort when
typing, but the advantage is that transposing is easier, and music can
be printed according to different conventions.  See @ref{Accidentals}
for some examples how accidentals can be printed according to
different rules.


@cindex tie
A tie is created by adding a tilde ``@code{~}'' to the first note
being tied:
@quotation
@lilypond[fragment,verbatim,relative 2]
g4~ g a2~ a4
@end lilypond
@end quotation
@separate

This example shows the key signature, accidentals and ties in action:

@quotation
@example
\score @{
  \notes @{
    \time 4/4
    \key g \minor
    \clef violin
    r4 r8 a8 gis4 b
    g8 d4.~ d e'8
    fis4 fis8 fis8 eis4  a8 gis~
    gis2 r2
  @}
  \paper @{ @}
@}
@end example

@lilypond
\score {
  \notes { \transpose c c' { 
    \time 4/4
    \key g \minor
    \clef violin
    r4 r8 a8 gis4 b
    g8 d4.~ d e8
    fis4 fis8 fis8 eis4  a8 gis~
    gis2 r2
  }}
  \paper { linewidth = #(* 50 staffspace) }
}
@end lilypond
@end quotation
@cindex accidentals

There are some interesting points to note in this example.  Bar lines
and beams are drawn automatically.  Line breaks are calculated
automatically; it does not matter where the lines breaks are in the
source file. Finally, the order of time, key and clef changes is not
relevant: in the printout, these are ordered according to standard
notation conventions.


@cindex beams, by hand 
Beams are drawn automatically, but if you do not like where they are
put, they can be entered by hand. Mark the first note to be beamed
with @code{[} and the last one with @code{]}:
@quotation
@lilypond[fragment,relative 1, verbatim]
a8[ ais] d[ es r d]
@end lilypond
@end quotation
@separate

For more information on
@table @asis
@item Rests
see @ref{Rests}. 

@item Ties
see  @ref{Ties}.

@item Accidentals
see @ref{Accidentals}

@item Key signature
see @ref{Key signature}

@item Beams
see @ref{Beaming}
@end table


@node  Octave entry
@section Octave entry


@c Tim wants to move this quotes example just before the: quotes-do not-work
@c score, but we'd need to remove quotes from the other two (key and
@c tie) examples...

@c better to have this just before the `octaves are bad' snipped
@c but we'd need to remove the ', from \key and tie 
To raise a note by an octave, add a high quote @code{'} (apostrophe) to
the note name, to lower a note one octave, add a ``low quote'' @code{,}
(a comma).  Middle C is @code{c'}:

@quotation
@example
c'4 c'' c''' \clef bass c c,
@end example

@lilypond[fragment]
\property Score.timing = ##f
\property Staff.TimeSignature = \turnOff
c'4 c'' c''' \clef bass c c,
@end lilypond
@end quotation
@separate

An example of the use of quotes is in the following Mozart fragment:
@lilypond[singleline,fragment,verbatim]
  \key a \major
  \time 6/8
  cis''8. d''16 cis''8 e''4 e''8
  b'8. cis''16 b'8 d''4 d''8 
@end lilypond 

This example shows that music in a high register needs lots of quotes.
This makes the input less readable, and it is a source of errors.  The
solution is to use ``relative octave'' mode.  In practice, this is the
most convenient way to copy existing music.  To use relative mode, add
@code{\relative} before the piece of music.  You must also give a note
from which relative starts, in this case @code{c''}.  If you do not
use octavation quotes (i.e. do not add ' or , after a note), relative
mode chooses the note that is closest to the previous one.
For example, @code{c f} goes up while @code{c g} goes down:

@quotation
@example
\relative c'' @{
  c f c g c
@}
@end example

@lilypond[fragment]
\property Score.timing = ##f
\property Staff.TimeSignature = \turnOff
\relative c'' {
  c f c g c
}
@end lilypond
@end quotation
@separate


Since most music has small intervals, pieces can be written almost
without octavation quotes in relative mode.  The previous example is
entered as
@c
@lilypond[singleline,fragment,verbatim]
\relative c'' {
  \key a \major
  \time 6/8
  cis8. d16 cis8 e4 e8
  b8. cis16 b8 d4 d8
}
@end lilypond 


@c needed better, maybe even redundant explanation
@c   added another example below.
@c grappig: Pa vond het heel logies, en slim toen-i eenmaal begreep.
@c in eerste instantie drong het `relative' niet door zonder extra uitleg.
Larger intervals are made by adding octavation quotes.
@quotation
@example
\relative c'' @{
  c f, f c' c g' c,
@}
@end example

@lilypond[fragment]
\property Score.timing = ##f
\property Staff.TimeSignature = \turnOff
\relative c'' {
  c f, f c' c g' c,
}
@end lilypond
@end quotation
@separate

Quotes or commas do not determine the absolute height of a note; the
height of a note is relative to the previous one.
@c do not use commas or quotes in this sentence
For example: @code{c f,} goes down; @code{f, f} are both the same;
@code{c' c} are the same; and @code{c g'} goes up:



Here is an example of the difference between relative mode and
``normal'' (non-relative) mode:

@quotation
@example
\relative a @{
\clef bass
  a d a e d c' d'
@}
@end example

@lilypond[fragment]
\property Score.timing = ##f
\property Staff.TimeSignature = \turnOff
\relative a {
\clef bass
  a d a e d c' d'
}
@end lilypond
@end quotation
@separate

@quotation
@example
\clef bass
  a d a e d c' d'
@end example

@lilypond[fragment]
\property Score.timing = ##f
\property Staff.TimeSignature = \turnOff
\clef bass
  a d a e d c' d'
@end lilypond
@end quotation
@separate


For more information on Relative octaves see @ref{Relative octaves}
and @ref{Octave check}.






@node Combining music into compound expressions
@section Combining music into compound expressions

To print more than one staff, each piece of music that makes up a
staff is marked by adding @code{\context Staff} before it.  These
@code{Staff}'s are then grouped inside @code{\simultaneous @{} and
@code{@}}, as is demonstrated here:

@quotation
@lilypond[fragment,verbatim]
\simultaneous {
  \new Staff { \clef violin c'' }
  \new Staff { \clef bass c }
}
@end lilypond
@end quotation


In this example, @code{\simultaneous } indicates that both music
fragments happen at the same time, and must be printed stacked
vertically.  The notation @code{<< .. >>} can also be used as a
shorthand for @code{\simultaneous @{ .. @}}.

The command @code{\new} introduces a ``notation context''.  To
understand this concept, imagine that you are performing a piece of
music. When you are playing, you combine the symbols printed at a
certain point with contextual information. For example, without
knowing the current clef, and the accidentals in the last measure, it
would be impossible to determine the pitch of a note. In other words,
this information forms context that helps you decipher a
score. LilyPond produces notation from music, so in effect, it does
the inverse of reading scores. Therefore, it also needs to keep track
of contextual information. This information is maintained in
``notation contexts.''  There are several types of contexts,
e.g. @code{Staff}, @code{Voice} and @code{Score}, but also
@code{Lyrics} and @code{ChordNames}. Prepending @code{\new} to a chunk
of music indicates what kind of context to use for interpreting it,
and ensures that the argument is interpreted with a fresh instance of
the context indicated.


@separate

We can now typeset a melody with two staves:

@quotation
@lilypond[verbatim,singleline]
\score {
  \notes 
  << \new Staff {
      \time 3/4
      \clef violin
      \relative c'' {
        e2( d4 c2 b4 a8[ a]
        b[ b] g[ g] a2.) }  
    }
    \new Staff {
       \clef bass
       c2 e4  g2.
       f4 e d c2.
    }
  >>
  \paper {} 
}
@end lilypond
@end quotation

The example shows how small chunks of music, for example, the notes
@code{c2}, @code{e4}, etc. of the second staff, are combined to form a
larger chunk by enclosing it in braces. Again, a larger chunk is
formed by prefix @code{\new Staff} to it, and that chunk is combined
with @code{<< >>}. This mechanism is similar with mathematical
formulas: a big formula is created by composing small formulas.  Such
formulas are called expressions, and their definition is recursive, so
you can make arbitrarily complex and large expressions.  For example,

@quotation
  1

  1 + 2
  
  (1 + 2) * 3
  
  ((1 + 2) * 3) / (4 * 5)
@end quotation
@cindex expression
@cindex music expression
This example shows a sequence of expressions, where each expression is
contained in the next one.  The simplest expressions are numbers and
operators (like +, * and /). Parentheses are used to group
expressions.  In LilyPond input, a similar mechanism is used. Here,
the simplest expressions are notes and rests.  By enclosing
expressions in @code{<< >>} and @code{@{ @}}, more complex music is
formed. The @code{\new} command also forms new expressions; prepending
it to a music expression yields a new expression.

Like mathematical expressions, music expressions can be nested
arbitrarily deep, e.g.
@lilypond[verbatim,relative 1] 
  { c <<c e>>
      << { e f } { c <<b d>> }
      >>
  }
@end lilypond 


@cindex indent
When spreading expressions over multiple lines, it is customary to use
an indent that indicates the nesting level. Formatting music like this
eases reading, and helps you  insert the right amount of closing
braces at the end of an expression. For example,
@example
\score @{
  \notes <<
    @{
      @dots{}
    @}
    @{
      @dots{}
    @}
  >>
@}
@end example


For more information on context see the Technical manual description
in @ref{Interpretation context}.



@node Adding articulation marks to notes
@section Adding articulation marks to notes

@cindex articulation
@cindex accents
@cindex staccato

Common accents can be added to a note using a dash (`@code{-}') and a
single character:
@quotation
@lilypond[verbatim,relative 1]
c-. c-- c-> c-^ c-+ c-_
@end lilypond
@end quotation
@separate

@cindex fingering
Similarly, fingering indications can be added to a note using a dash
(`@code{-}') and the digit to be printed:
@c
@lilypond[verbatim,relative 1]
  c-3 e-5 b-2 a-1
@end lilypond


Dynamic signs are made by adding the markings to the note:
@quotation
@lilypond[verbatim,relative 1]
c\ff c\mf
@end lilypond
@end quotation
@separate

@cindex dynamics
@cindex decrescendo
@cindex crescendo

Crescendi and decrescendi are started with the commands @code{\<} and
@code{\>}. The command @code{\!} finishes a crescendo on the note it
is attached to:
@quotation
@lilypond[verbatim,relative 1]
c2\<  c2\!\ff\>  c2  c2\!
@end lilypond
@end quotation
@separate



@cindex slur

A slur is drawn across many notes, and indicates bound articulation
(legato).  The starting note and ending note are marked with a
``@code{(}'' and a ``@code{)}'' respectively:

@quotation
@lilypond[fragment,relative 1, verbatim]
d4( c16)( cis d e c cis d e)( d4)
@end lilypond
@end quotation
@separate
@cindex slurs versus ties
A slur looks like a tie, but it has a different meaning. A tie simply
makes the first note sound longer, and can only be used on pairs of
notes with the same pitch. Slurs indicate the articulations of notes,
and can be used on larger groups of notes. Slurs and ties are also
nested in practice:
@c
@lilypond[fragment, relative=1]
c2~( c8 fis fis4 ~ fis2 g2)
@end lilypond

@cindex phrasing slurs
If you need two slurs at the same time (one for articulation, one for
phrasing), you can also make a phrasing slur with @code{\(} and
@code{\)}.

@quotation
@lilypond[fragment,relative 1, verbatim]
a8(\( ais b  c) cis2 b'2 a4 cis,  c\)
@end lilypond
@end quotation


For more information on
@table @asis
@item fingering
  see @ref{Fingering instructions}
@item articulations
  see @ref{Articulations}
@item slurs
  see @ref{Slurs}
@item phrasing slurs
  see  @ref{Phrasing slurs}
@item dynamics
  see  @ref{Dynamics}
@item fingering
@end table

@node Combining notes into chords
@section Combining notes into chords

@cindex chords
Chords can be made by
surrounding pitches with @code{<} and @code{>}:
@quotation
@lilypond[relative 0, fragment,verbatim]
r4 <c e g>4 <c f a>8
@end lilypond
@end quotation
@separate


You can combine beams and ties with chords.  Beam and tie markings
must be placed outside the chord markers:
@quotation
@lilypond[relative 0, fragment,verbatim]
r4 <c e g>8[ <c f a>]~ <c f a>
@end lilypond
@end quotation

@quotation
@example
r4 <c e g>8\>( <c e g> <c e g>  <c f a>8\!)
@end example
@lilypond[relative 0, fragment]
\slurUp
r4 <c e g>8\>( <c e g> <c e g>  <c f a>8\!)
@end lilypond
@end quotation
@separate



@menu
* Basic rhythmical commands::   
* Commenting input files::      
@end menu

@node Basic rhythmical commands
@subsection  Basic rhythmical commands

@cindex pickup
@cindex anacruse
@cindex upstep
@cindex partial measure
A pickup (or upstep) is entered with the keyword @code{\partial}. It
is followed by a duration: @code{\partial 4} is a quarter note upstep
and @code{\partial 8} an eighth note:
@lilypond[relative 1,verbatim,fragment]
  \partial 8
  f8 c2 d e
@end lilypond

@cindex tuplets
@cindex triplets
Tuplets are made with the @code{\times} keyword.  It takes two
arguments: a fraction and a piece of music.  The duration of the piece
of music is multiplied by the fraction.  Triplets make notes occupy
2/3 of their notated duration, so  a triplet has  2/3 as its fraction:
@c
@lilypond[relative 0,verbatim,fragment]
  \times 2/3 { f8 g a }
  \times 2/3 { c r c }
@end lilypond 

@cindex grace notes
@cindex accacciatura
Grace notes are also made by prefixing a note, or a set of notes with
a keyword. In this case, the keywords are @code{\appoggiatura}
and @code{\acciaccatura}
@cindex appoggiatura
@cindex acciaccatura
      
@lilypond[relative 1, verbatim,fragment]
  c4 \appoggiatura b16 c4
  c4 \acciaccatura b16 c4
@end lilypond

@noindent

For more information on
@table @asis
@item  grace notes
see @ref{Grace notes},
@item tuplets
see @ref{Tuplets},
@item upsteps 
see @ref{Partial measures}.
@end table



@node Commenting input files
@subsection Commenting input files

@cindex comments
@cindex line comment
@cindex block comment
Comments are pieces of the input that are ignored.  There are two
types of comments. A line comments is introduced by @code{%}: after
that, the rest of that line is ignored.  Block comments span larger
sections of input.  Anything that is enclosed in @code{%@{} and
@code{%@}} is ignored too. The following fragment shows possible uses
for comments:

@example
  % notes for twinkle twinkle follow:
  c4 c   g' g  a a
  
  %@{
  
    This line, and the notes below
    are ignored, since they are in a
    block comment.

    g g f f e e d d c2 
  %@}
@end example




@node Printing lyrics
@section Printing lyrics
@cindex lyrics

@cindex Lyrics
@cindex Songs
Lyrics are entered by separating each syllable with a space, and
surrounding them with @code{\lyrics @{ @dots{} @}}, for example,
@example
  \lyrics @{ I want to break free @}
@end example

Like notes, lyrics are also a form of music, but they must not be
printed on a staff, which is the default way to print music. To print
them as lyrics, they must be marked with @code{ \new Lyrics}:
@example
  \new Lyrics  \lyrics @{ I want to break free @}
@end example
The melody for this song is as follows:

@lilypond[fragment,relative=1]
   \partial 8
     c8
   \times 2/3 {  f4 g g } \times 2/3 { g4( a2) }
@end lilypond

The lyrics can be set to these notes, combining both with the
@code{\addlyrics} keyword:
@example
 \addlyrics
    \notes @{ @dots{} @}
    \new Lyrics @dots{}
@end example

The final result is 
@lilypond[verbatim,linewidth=6.0cm]
\score  {
 \notes {
  \addlyrics
   \relative c' {
     \partial 8
     c8
     \times 2/3 { f g g } \times 2/3 { g4( a2) }
   }
   \new Lyrics  \lyrics { I want to break free }
 }
 \paper{ }
}
@end lilypond

@cindex melisma
@cindex extender line
@c synonyms?
This melody ends on a @rglos{melisma}, a single syllable (``free'')
sung to more than one note. This is indicated with an @emph{extender
line}. It is entered as two underscores, i.e.
@example
  \lyrics @{ I want to break free __ @}
@end example 
@lilypond[]
\score  {
 \notes {
  \addlyrics
   \relative c' {
   \partial 8
     c8
     \times 2/3 { f g g } \times 2/3 { g4( a2) }

     %% ugh, this is to deal with bugs in the extender implementation
     \hideNotes
     c32
   }
   \new Lyrics  \lyrics { I want to break free __ }
 }
 \paper{ linewidth = 9.0 \cm }
}
@end lilypond

Similarly, hyphens between words can be entered as two dashes,
resulting in a centered hyphen between two syllables:
@example
  Twin -- kle twin -- kle
@end example
@lilypond[singleline]
\score {
  \addlyrics \notes \relative f' { \time 2/4
    f4 f c' c' }
    \new Lyrics \lyrics { Twin -- kle twin -- kle
 }
\paper { linewidth = 6.0 \cm }
 }
 
@end lilypond

More options, like putting multiple lines of lyrics below a melody are
discussed in @ref{Vocal music}.



@node A lead sheet
@section A lead sheet

@cindex Lead sheets
@cindex chords
@cindex chord names
 
In popular music, it is common to denote accompaniment as chord-names.
Using them in LilyPond has two parts, just like lyrics: entering the
chords (with @code{\chords}), and printing them (with @code{\new
ChordNames}).

Chord names are entered by starting chords mode (with @code{\chords}).
In chords mode, you can enter chords with a letter (indicating the
root of the chord), and a durations following that:
@c
@lilypond[verbatim]
  \chords { c2 f4. g8 } 
@end lilypond

@noindent
The result of @code{\chords} is a list of chords, and is  equivalent
to entering chords with @code{<@dots{}>}.

Other chords can be created by adding modifiers, after a colon.  The
following example shows a few common modifiers:
@c
@lilypond[verbatim]
  \chords { c2 f4:m g4:maj7 gis1:dim7 }
@end lilypond

Printing chords is done by adding @code{\context ChordNames}
before the chords thus entered:
@c
@lilypond[verbatim]
 \context ChordNames \chords { c2 f4.:m g4.:maj7 gis8:dim7 }
@end lilypond

@cindex lead sheet
@separate
When put together,  chord names, lyrics and a melody form
a lead sheet, for example,

@example
\score @{
  <<
    \context ChordNames \chords @{ @emph{chords} @}
    \addlyrics
      \notes @emph{the melody}
      \context Lyrics \lyrics @{ @emph{the text} @}
  >>
  \paper @{ @}
@}
@end example
@lilypond[]
\score  {
  << 
   \context ChordNames \chords { r8 c2:sus4 f } 
   \addlyrics
    \notes \relative c' {
     \partial 8
     c8
     \times 2/3 { f g g } \times 2/3 { g4( a2) } }
   \context Lyrics  \lyrics { I want to break free __ }
  >>
 \paper{ raggedright = ##t }
}
@end lilypond


A complete list of modifiers, and other options for layout are in the
reference manual section @ref{Chords}.

@node Listening to output
@section Listening to output

@cindex sound
@cindex MIDI

MIDI (Musical Instrument Digital Interface) is a standard for
connecting and recording digital instruments.  A MIDI file is like a
tape recording of a MIDI instrument. The @code{\midi} block makes the
music go to a MIDI file, so you can listen to the music you entered.
It is great for checking the music: octaves that are off, or
accidentals that were mistyped, stand out very much when listening to
the musical transcription.

@code{\midi} can be used in similarly to @code{\paper @{ @}}, for
example,
@example 
\score @{
    @var{..music..}
    \midi  @{ \tempo 4=72 @}
    \paper  @{ @}
@}
@end example 

Here, the tempo is specified using the @code{\tempo} command.  In this
case the tempo of quarter notes is set to 72 beats per minute. More
information on auditory output is in the @ref{Sound} section in the
notation manual.



@node Titling
@section Titling

Bibliographic information is entered in a separate block, the
@code{\header} block. The name of the piece, its composer, etc. are
entered as an assignment, within @code{\header @{ @dots{} @}}. For
example,
@example 
  \header @{
    title = "Eight miniatures" 
    composer = "Igor Stravinsky"
    tagline = "small is beautiful"
  @}
  
  \score @{ @dots{} @}
@end example

@cindex bibliographic information
@cindex titles
@cindex composer
@cindex Engraved by LilyPond

When the file is processed by the @code{lilypond} wrapper script, then
the title and composer specified are printed above the music. The
`tagline' is a short line printed at bottom of the last page, which
normally says ``Engraved by LilyPond, version @dots{}''. In the
example above, it is replaced by the line ``small is
beautiful.''@footnote{Nicely printed parts are good PR for us, so do
us a favor, and leave the tagline if you can.}

Normally, the @code{\header} is put at the top of the file. However,
for a document that contains multiple pieces (e.g. an etude book, or
an orchestral part with multiple movements), then the header can be
put into the @code{\score} block as follows; in this case, the name of
each piece will be printed before each movement:


@cindex Lily was here
@cindex signature line
@cindex tag line

@example 
  \header @{
    title = "Eight miniatures" 
    composer = "Igor Stravinsky"
    tagline = "small is beautiful"
  @}
  
  \score @{ @dots{}
    \header @{ piece = "Adagio" @}
  @}
  \score @{ @dots{}
    \header @{ piece = "Menuetto" @}
  @}
@end example

More information on titling can be found in @ref{Invoking lilypond}.


@node Single staff polyphony
@section Single staff polyphony

@cindex polyphony
@cindex multiple voices
@cindex voices, more -- on a staff

When different melodic lines are combined on a single staff, these are
printed as polyphonic voices: each voice has its own stems, slurs and
beams, and the top voice has the stems up, while the bottom voice has
them down.

Entering such parts is done by entering each voice as a sequence (with
@code{@{ .. @}}), and combining those simultaneously, separating the
voices with @code{\\}:

@example
  << @{ a4 g2 f4~ f4 @} \\
    @{ r4 g4 f2 f4 @} >>
@end example
@lilypond[relative 1]
\context Staff   << { a4 g2 f4~ f4 } \\
    { r4 g4 f2 f4 } >>
@end lilypond

For polyphonic music typesetting, spacer rests can also be convenient: these
are rests that do not print.  It is useful for filling up voices that
temporarily do not play:
@example
  << @{ a4 g2 f4~ f4 @} \\
    @{ s4 g4 f2 f4 @} >>
@end example
@lilypond[relative 1]
\context Staff  << { a4 g2 f4~ f4 } \\
    { s4 g4 f2 f4 } >>
@end lilypond

Again, these expressions can be nested arbitrarily:

@lilypond[fragment]
<<
 \new Staff 
  \relative c''
    << { a4 g2 f4~ f4 } \\
      { s4 g4 f2 f4 } >>
 \new Staff 
  <<  { \clef bass <c g>1 ~ <c g>4   } \\
     { f4 d e2  ~ e4}
  >>
>>
@end lilypond


More features of polyphonic typesetting are in the notation manual
in @ref{Polyphony}.

@node Piano staves
@section Piano staves

@cindex staff switch, manual
@cindex cross staff voice, manual
@cindex @code{\translator}

Piano music is always typeset in two staves connected by a brace.
Printing such a staff is done similar to the polyphonic example in
@ref{Combining music into compound expressions}:
@example
 << \new Staff @{ @dots{} @}
   \new Staff @{ @dots{} @}
 >>
@end example
but now this entire expression must be interpreted as a
@code{PianoStaff}:
@example
 \new PianoStaff << \new Staff @dots{} >>
@end example

Here is a full-fledged example:

@lilypond[relative 0,fragment]
\new PianoStaff
 << \new Staff { \time 2/4
     c4 c g' g  }
   \new Staff {
     \clef bass c,, c' e c }
 >>
@end lilypond

More information on formatting piano music is in @ref{Piano music}. 

@node Setting variables
@section Setting variables

When the music is converted from notes to print, it is interpreted
from left-to-right order, similar to what happens when we read
music. During this step, context-sensitive information, such as the
accidentals to print, and where barlines must be placed, are stored in
variables. These variables are called @emph{context properties}.
The properties can also be manipulated from input files. Consider this input:
@example
\property Staff.autoBeaming = ##f
@end example 

@noindent
It sets the property named @code{autoBeaming} in the current staff at
this point in the music to @code{##f}, which means `false'. This
property controls whether beams are printed automatically:
@c
@lilypond[relative 1,fragment,verbatim]
  c8 c c c
  \property Staff.autoBeaming = ##f
  c8 c c c  
@end lilypond

@noindent
LilyPond includes a built-in programming language, namely, a dialect
of Scheme.  The argument to @code{\property}, @code{##f}, is an
expression in that language.  The first hash-mark signals that a piece
of Scheme code follows. The second hash character is part of the
boolean value true (@code{#t}).  Values of other types may be
entered as follows:
@itemize @bullet
@item a string, enclosed in double quotes, for example,
@example
  \property Staff.instrument = #"French Horn"
@end example
@item a boolean: either @code{#t} or @code{#f}, for true and false
respectively, e.g.
@example
  \property Voice.autoBeaming = ##f
  \property Score.skipBars = ##t
@end example

@item a number, such as
@example
  \property Score.currentBarNumber = #20
@end example

@item a symbol, which is introduced by a quote character, as in 
@example
  \property Staff.crescendoSpanner = #'dashed-line
@end example

@item a pair, which is also introduced by a quote character, like in
the following statements, which set properties to the pairs (-7.5, 6) 
and (3, 4) respectively:

@example
  \property Staff.minimumVerticalExtent  = #'(-7.5 . 6)
  \property Staff.timeSignatureFraction  = #'(3 . 4)
@end example

@item a list, which is also introduced by a quote character. In the
following example, the @code{breakAlignOrder} property is set to a
list of symbols:
@example
  \property Score.breakAlignOrder =
    #'(left-edge time-signature key-signatures)
@end example


@end itemize

There are many different properties, and not all of them are listed in
this manual. However, the program reference lists them all in the
section @internalsref{Context-properties}, and most properties are
demonstrated in one of the
@ifhtml
@uref{../../../input/test/out-www/collated-files.html,tips-and-tricks}
@end ifhtml
@ifnothtml
tips-and-tricks
@end ifnothtml
examples.


@node Fine tuning layout
@section Fine tuning layout

Sometimes it is necessary to change music layout by hand.  When music
is formatted, layout objects are created for each symbol.  For
example, every clef and every note head is represented by a layout
object.  These layout objects also carry variables, which we call
@emph{layout properties}. By changing these variables from their
values, we can alter the look of a formatted score:

@lilypond[verbatim,relative 0]
  c4
  \property Voice.Stem \override #'thickness = #3.0
  c4 c4 c4 
@end lilypond

@noindent
In the example shown here, the layout property @code{thickness} (a
symbol) is set to 3 in the @code{Stem} layout objects of the current
Voice.  As a result, the notes following @code{\property} have thicker
stems.

In most cases of manual overrides, only a single object must be
changed. This can be achieved by prefixing @code{\once} to the
@code{\property} statement, i.e.

@example
 \once \property Voice.Stem \set #'thickness = #3.0
@end example

@lilypond[relative 0]
  c4
  \once \property Voice.Stem \set #'thickness = #3.0
  c4 c4 c4 
@end lilypond

@noindent
Some overrides are so common that predefined commands are provided as
a short cut.  For example, @code{\slurUp} and @code{\stemDown}. These
commands are described in
@ifhtml
the
@end ifhtml
@ref{Notation manual}, under the sections for slurs and stems
respectively.

The exact tuning possibilities for each type of layout object are
documented in the program reference of the respective
object. However, many layout objects share properties, which can be
used to apply generic tweaks.  We mention a couple of these:

@itemize @bullet
@item The @code{extra-offset} property, which
@cindex @code{extra-offset}
has a pair of numbers as value, moves around objects in the printout.
The first number controls left-right movement; a positive number will
move the object to the right.  The second number controls up-down
movement; a positive number will move it higher.  The unit of these
offsets are staff-spaces.  The @code{extra-offset} property is a
low-level feature: the formatting engine is completely oblivious to
these offsets.

In the following example example, the second fingering is moved a
little to the left, and 1.8 staff space downwards:

@cindex setting object properties

@lilypond[relative 1,verbatim]
\stemUp
f-5
\once \property Voice.Fingering
  \set #'extra-offset = #'(-0.3 . -1.8) 
f-5
@end lilypond

@item
Setting the @code{transparent} property will make an object be printed
in `invisible ink': the object is not printed, but all its other
behavior is retained. The object still takes space, it takes part in
collisions, and slurs, ties and beams can be attached to it.

@cindex transparent objects
@cindex removing objects
@cindex invisible objects
The following example demonstrates how to connect different voices
using ties. Normally ties only happen between notes of the same
voice. By introducing a tie in a different voice, and blanking a stem
in that voice, the tie appears to cross voices:

@lilypond[fragment,relative 1,verbatim]
  c4 << {
      \once \property Voice.Stem \set #'transparent = ##t
      b8~ b8
  } \\ {
       b[ g8]
  } >>
@end lilypond

@item
The @code{padding} property for objects with
@cindex @code{padding}
@code{side-position-interface} can be set to increase distance between
symbols that are printed above or below notes. We only give an
example; a more elaborate explanation is in @ref{Constructing a
tweak}:

@lilypond[relative 1,verbatim]
  c2\fermata
  \property Voice.Script \set #'padding = #3
  b2\fermata
@end lilypond

@end itemize

More specific overrides are also possible.  The notation manual
discusses in depth how to figure out these statements for yourself, in
@ref{Tuning output}.

@node Organizing larger pieces
@section Organizing larger pieces

When all of the elements discussed earlier are combined to produce
larger files, the @code{\score} blocks get a lot bigger, because the
music expressions are longer, and, in the case of polyphonic and/or
orchestral pieces, more deeply nested. Such large expressions can
become unwieldy.

By using variables, also known as identifiers, it is possible to break
up complex music expressions.  An identifier is assigned as follows:
@c
@example
  namedMusic = \notes @{ @dots{}
@end example

The contents of the music expression @code{namedMusic}, can be used
later by preceding the name with a backslash, i.e. @code{\namedMusic}.
In the next example, a two note motive is repeated two times by using
variable substitution:

@lilypond[singleline,verbatim]
seufzer  = \notes {
  dis'8 e'8
}
\score { \notes {
  \seufzer \seufzer 
} }
@end lilypond

The name of an identifier should have alphabetic characters only, and
no numbers, underscores or dashes. The assignment should be outside of
the @code{\score} block.

It is possible to use variables for many other types of objects in the
input.  For example,
@example
  width = 4.5\cm
  name = "Wendy"
  aFivePaper = \paper @{ paperheight = 21.0 \cm @}
@end example
Depending on its contents, the identifier can be used in different
places. The following example uses the above variables:
@example
  \score @{
    \notes @{ c4^\name @}
    \paper @{
      \aFivePaper
      linewidth = \width
    @}
  @}
@end example

More information on the possible uses of identifiers is in the
technical manual, in @ref{Scheme datatypes}.


@node An orchestral part
@section An orchestral part

In orchestral music, all notes are printed twice: both in a part for
the musicians, and in a full score for the conductor. Identifiers can
be used to avoid double work: the music is entered once, and stored in
variable. The contents of that variable is then used to generate
both the part and the score.

It is convenient  to define the notes in a  special file, for example,
suppose that the @file{horn-music.ly} contains the following part of a
horn/bassoon duo.
@example
hornNotes = \notes \relative c @{
  \time 2/4
  r4 f8 a cis4 f e d
@}
@end example

Then, an individual part is made by putting the following in a file:
@example
\include "horn-music.lyinc"
\header @{
  instrument = "Horn in F"
@}
\score @{
  \notes \transpose c' f \hornNotes
@}
@end example
The @code{\include} command substitutes the contents of the file at
this position in the file, so that @code{hornNotes} is defined
afterwards.  The code @code{\transpose f c'} indicates that the
argument, being @code{\hornNotes}, should be transposed by a fifth
downwards: sounding @code{f} is denoted by notated @code{c'}, which
corresponds with tuning of a normal French Horn in F. The
transposition can be seen in the following output:

@lilypond[singleline]
\score {
  \notes \transpose f c' \notes \relative c {
  \time 2/4
  r4 f8 a cis4 f e d
}
}
@end lilypond

In ensemble pieces, one of the voices often does not play for many
measures. This is denoted by a special rest, the multi-measure
rest. It is entered with a capital R, and followed by a duration (1
for a whole note, 2 for a half note, etc.) By multiplying the
duration, longer rests can be constructed. For example, the next rest
takes 3 measures in 2/4 time:
@example
  R2*3
@end example

When printing the part, the following @code{skipBars} property must be
set to false, to prevent the rest from being expanded in three one bar
rests:
@example
  \property Score.skipBars = ##t
@end example
Prepending the rest and the property setting above, leads to the
following result:

@lilypond[singleline]
\score {\notes { \transpose f c' \relative c { \time 2/4
\property Score.skipBars = ##t 
        R2*3
    r4 f8 a cis4 f e d } }}
@end lilypond

The score is made by combining all of the music in a @code{\score}
block, assuming that the other voice is in @code{bassoonNotes}, in the
file @file{bassoon-music.ly}:
@example
\include "bassoon-music.lyinc"
\include "horn-music.lyinc"

\score @{
  \simultaneous @{
    \new Staff \hornNotes
    \new Staff \bassoonNotes
  @} @}
@end example

This would lead to the simple score depicted below:

@lilypond[singleline]
\score {
  \notes \relative c \simultaneous {
    \new Staff { \time 2/4
        R2*3
    r4 f8 a cis4 f e d }
    \new Staff { \clef bass
      r4 d,8 f | gis4 c |  b bes |
      a8 e f4 |  g d | gis f }
  } }
@end lilypond 

More in-depth information on preparing parts and scores is in the
notation manual, in @ref{Orchestral music}.


@node Integrating text and music
@section Integrating text and music

@cindex La@TeX{}, music in
@cindex HTML, music in
@cindex Texinfo, music in

Sometimes you might want to use music examples in a text that you are
writing (for example, a musicological treatise, a songbook, or (like us)
the LilyPond manual).  You can make such texts by hand, simply by
importing a PostScript figure into your word processor.  However,
there is an automated procedure to reduce the amount of work.

If you use HTML, La@TeX{}, or Texinfo, you can mix text and LilyPond
code.  A script called @code{lilypond-book} will extract the music
fragments, run LilyPond on them, and put back the resulting notation.
This program is fully described in @ref{lilypond-book manual}.  Here
we show a small example;  since the example contains also explanatory
text, we will not comment it further:

@example
\documentclass[a4paper]@{article@}
\begin@{document@}

In a lilypond-book document, you can freely mix music and text. For
example:
\begin@{lilypond@}
  \score @{ \notes \relative c' @{
     c2 g'2 \times 2/3 @{ f8 e d @} c'2 g4
  @} @}
\end@{lilypond@}

If you have no \verb+\score+ block in the fragment,
\texttt@{lilypond-book@} will supply one:

\begin@{lilypond@}
  c'4
\end@{lilypond@}

In the example you see here, two things happened: a
\verb+\score+ block was added, and the line width was set to natural
length. You can specify  options by putting them in brackets:

\begin[26pt,verbatim]@{lilypond@}
  c'4 f16
\end@{lilypond@}

If you want to include large examples into the text, it is more
convenient to put it in a separate file:

\lilypondfile@{screech-boink.ly@}

\end@{document@}
@end example

Under Unix, you can view the results as follows:
@example
$ cd input/tutorial
$ mkdir -p out/
$ lilypond-book --outdir=out/ lilbook.tex
lilypond-book (GNU LilyPond) 1.7.23
Reading `input/tutorial/lilbook.tex'
Reading `input/screech-boink6.ly'
@var{lots of stuff deleted}
Writing `out/lilbook.latex'
$ cd out
$ latex lilbook.latex
@var{lots of stuff deleted}
$ xdvi lilbook 
@end example

Running lilypond-book and running latex creates a lot of temporary
files, and you would not want those to clutter up your working
directory.  The @code{outdir} option to lilypond-book creates the
temporary files in a separate subdirectory @file{out}.

The result looks more or less like this: 

@separate

In a lilypond-book document, you can freely mix music and text. For
example:
@lilypond
\score {
  \notes \relative c' {
    c2 g'2 \times 2/3 { f8 e d } c'2 g4
  }
  \paper {
    raggedright = ##t
  }
}
@end lilypond

If you have no @code{\score} block in the fragment,
@code{lilypond-book} will supply one:

@lilypond
  c'4
@end lilypond

In the example you see here, two things happened: a
@code{score} block was added, and the line width was set to natural
length. You can specify  options by putting them in brackets:

@lilypond[26pt,verbatim]
  c'4 f16
@end lilypond

If you want to include large examples into the text, it is more
convenient to put it in a separate file:

@lilypondfile{screech-boink.ly}
