4.1.1 Introduction to the LilyPond file structure
A basic example of a LilyPond input file is
\version "2.25.21" \header { } \score { … compound music expression … % all the music goes here! \layout { } \midi { } }
There are many variations of this basic pattern, but this example serves as a useful starting place.
Up to this point none of the examples you have seen have used a
\score
command. This is because LilyPond automatically
adds the extra commands that are needed when you give it simple
input. LilyPond treats input like this:
\relative { c''4 a b c }
as shorthand for this:
\book { \score { \new Staff { \new Voice { \relative { c''4 a b c } } } \layout { } } }
In other words, if the input contains a single music expression, LilyPond will interpret the file as though the music expression was wrapped up inside the commands shown above.
A word of warning! Many of the examples in the LilyPond
documentation will omit the \new Staff
and \new
Voice
commands, leaving them to be created implicitly. For
simple examples this works well, but for more complex examples,
especially when additional commands are used, the implicit
creation of contexts can give surprising results, maybe creating
extra unwanted staves. For a detailed explanation how to create
contexts explicitly, see Contexts and engravers.
Note: When entering more than a few lines of music it is advisable to always create staves and voices explicitly.
For now, though, let us return to the first example and examine
the \score
command, leaving the others to default.
A \score
block must always contain exactly one music
expression. Remember that a music expression could be anything
from a single note to a huge compound expression like
{ \new StaffGroup << … insert the whole score of a Wagner opera in here … >> }
Since everything is inside { … }
, it counts as one
music expression.
As we saw previously, the \score
block can contain other
things, such as
\score { { c'4 a b c' } \header { } \layout { } \midi { } }
Note that these three commands – \header
, \layout
,
and \midi
– are special: unlike many other commands that
begin with a backward slash (\
) they are not music
expressions and are not part of any music expression. So they may
be placed inside a \score
block or outside it. In fact,
these commands are commonly placed outside the \score
block
– for example, \header
is often placed above the
\score
command, as the example at the beginning of this
section shows.
Two more commands you have not previously seen are \layout
and \midi
. If these appear as shown they will cause
LilyPond to produce a printed output and a MIDI output,
respectively. They are described fully in the Notation Reference
– Score layout, and Creating MIDI output.
You may code multiple \score
blocks. Each will be treated
as a separate score, but they will be all combined into a single
output file. A \book
command is not necessary – one will
be implicitly created. However, if you would like separate output
files from one .ly file then the \book
command
should be used to separate the different sections: each
\book
block will produce a separate output file.
To summarize:
- Every
\book
block creates a separate output file (e.g., a PDF file). If you haven’t explicitly added one, LilyPond wraps your entire input code in a\book
block implicitly. - Every
\score
block is a separate chunk of music within a\book
block. -
Every
\layout
block affects the\score
or\book
block in which it appears – i.e., a\layout
block inside a\score
block affects only that\score
block, but a\layout
block outside of a\score
block (and thus in a\book
block, either explicitly or implicitly) will affect every\score
in that\book
.
See Multiple scores in a book for details.
Another great shorthand is the ability to define variables, as shown previously (see Organizing pieces with variables). All the templates use this:
melody = \relative { c'4 a b c } \score { \melody }
When LilyPond looks at this file, it takes the value of
melody
(everything after the equals sign) and inserts it
whenever it sees \melody
. There’s nothing special about
the name – it could be melody
, global
,
keyTime
, pianorighthand
, or something else.
Remember that you can use almost any name you like as long as it
contains just alphabetic characters and is distinct from LilyPond
command names. For more details, see Saving typing with variables and functions. The exact limitations on variable names
are detailed in File structure.
See also
For a complete definition of the input format, see File structure.