[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.1 How LilyPond input files work

The LilyPond input format is quite free-form, giving experienced users a lot of flexibility to structure their files however they wish. But this flexibility can make things confusing for new users. This section will explain some of this structure, but may gloss over some details in favor of simplicity. For a complete description of the input format, see (notation)File structure section ‘File structure’ in Notation Reference.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.1.1 Introduction to the LilyPond file structure

A basic example of a LilyPond input file is

 
\version "2.21.3"

\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 which 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. The way to create contexts explicitly is explained in 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 which 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 – (notation)Score layout section ‘Score layout’ in Notation Reference, and (notation)Creating MIDI output section ‘Creating MIDI output’ in Notation Reference.

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.

In summary:

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.

For details see (notation)Multiple scores in a book section ‘Multiple scores in a book’ in Notation Reference.

Another great shorthand is the ability to define variables, as shown in 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 (notation)File structure section ‘File structure’ in Notation Reference.

See also

For a complete definition of the input format, see (notation)File structure section ‘File structure’ in Notation Reference.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.1.2 Score is a (single) compound musical expression

We saw the general organization of LilyPond input files in the previous section, Introduction to the LilyPond file structure. But we seemed to skip over the most important part: how do we figure out what to write after \score?

We didn’t skip over it at all. The big mystery is simply that there is no mystery. This line explains it all:

A \score block must contain exactly one music expression.

To understand what is meant by a music expression, you may find it useful to review the tutorial, Music expressions explained. In that section, we saw how to build big music expressions from small pieces – we started from notes, then chords, etc. Now we’re going to start from a big music expression and work our way down. For simplicity, we’ll use just a singer and piano in our example. We don’t need a StaffGroup for this ensemble, which simply groups a number of staves together with a bracket at the left, but we do need staves for a singer and a piano, though.

 
\score {
  <<
    \new Staff = "singer" <<
    >>
    \new PianoStaff = "piano" <<
    >>
  >>
  \layout { }
}

Here we have given names to the staves – “singer” and “piano”. This is not essential here, but it is a useful habit to cultivate so that you can see at a glance what each stave is for.

Remember that we use << … >> instead of { … } to show simultaneous music. This causes the vocal part and piano part to appear one above the other in the score. The << … >> construct would not be necessary for the Singer staff in the example above if it were going to contain only one sequential music expression, but << … >> rather than braces is necessary if the music in the Staff is to contain two or more simultaneous expressions, e.g. two simultaneous Voices, or a Voice with lyrics. We’re going to have a voice with lyrics, so angle brackets are required. We’ll add some real music later; for now let’s just put in some dummy notes and lyrics. If you’ve forgotten how to add lyrics you may wish to review \addlyrics in Setting simple songs.

 
\score {
  <<
    \new Staff = "singer" <<
      \new Voice = "vocal" { c'1 }
      \addlyrics { And }
    >>
    \new PianoStaff = "piano" <<
      \new Staff = "upper" { c'1 }
      \new Staff = "lower" { c'1 }
    >>
  >>
  \layout { }
}

[image of music]

Now we have a lot more details. We have the singer’s staff: it contains a Voice (in LilyPond, this term refers to a set of notes, not necessarily vocal notes – for example, a violin generally plays one voice) and some lyrics. We also have a piano staff: it contains an upper staff (right hand) and a lower staff (left hand), although the lower staff has yet to be given a bass clef.

At this stage, we could start filling in notes. Inside the curly braces next to \new Voice = "vocal", we could start writing

 
\relative {
  r4 d''8\noBeam g, c4 r
}

But if we did that, the \score section would get pretty long, and it would be harder to understand what was happening. So let’s use variables instead. These were introduced at the end of the previous section, remember? To ensure the contents of the text variable are interpreted as lyrics we preface them with \lyricmode. Like \addlyrics, this switches the input mode to lyrics. Without that, LilyPond would try to interpret the contents as notes, which would generate errors. (Several other input modes are available, see (notation)Input modes section ‘Input modes’ in Notation Reference.)

So, adding a few notes and a bass clef for the left hand, we now have a piece of real music:

 
melody = \relative { r4 d''8\noBeam g, c4 r }
text   = \lyricmode { And God said, }
upper  = \relative { <g' d g,>2~ <g d g,> }
lower  = \relative { b,2 e }

\score {
  <<
    \new Staff = "singer" <<
      \new Voice = "vocal" { \melody }
      \addlyrics { \text }
    >>
    \new PianoStaff = "piano" <<
      \new Staff = "upper" { \upper }
      \new Staff = "lower" {
        \clef "bass"
        \lower
      }
    >>
  >>
  \layout { }
}

[image of music]

When writing (or reading) a \score section, just take it slowly and carefully. Start with the outer level, then work on each smaller level. It also really helps to be strict with indentation – make sure that each item on the same level starts on the same horizontal position in your text editor.

See also

Notation Reference: (notation)Structure of a score section ‘Structure of a score’ in Notation Reference.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.1.3 Nesting music expressions

It is not essential to declare all staves at the beginning; they may be introduced temporarily at any point. This is particularly useful for creating ossia sections – see (music-glossary)ossia section ‘ossia’ in Music Glossary. Here is a simple example showing how to introduce a new staff temporarily for the duration of three notes:

 
\new Staff {
  \relative {
    r4 g'8 g c4 c8 d |
    e4 r8
    <<
      { f8 c c }
      \new Staff {
        f8 f c
      }
    >>
    r4 |
  }
}

[image of music]

Note that the size of the clef is the same as a clef printed following a clef change – slightly smaller than the clef at the beginning of the line. This is usual for clefs printed in the middle of a line.

The ossia section may be placed above the staff as follows:

 
\new Staff = "main" {
  \relative {
    r4 g'8 g c4 c8 d |
    e4 r8
    <<
      { f8 c c }
      \new Staff \with {
        alignAboveContext = "main"
      } { f8 f c }
    >>
    r4 |
  }
}

[image of music]

This example uses \with, which will be explained more fully later. It is a means of modifying the default behavior of a single Staff. Here it says that the new staff should be placed above the staff called “main” instead of the default position which is below.

See also

Ossia are often written without clef and without time signature and are usually in a smaller font. These require further commands which have not yet been introduced. See Size of objects, and (notation)Ossia staves section ‘Ossia staves’ in Notation Reference.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.1.4 Structure of a note entry

A note entry in LilyPond consists of a pitch, followed by a duration, optionally followed by one or more ‘post-events’. LilyPond post-events add things such as articulations, fingerings, string numbers, slurs, ties and explanatory text.

The pitch may be explicitly defined using the current LilyPond input language as described in (notation)Note names in other languages section ‘Note names in other languages’ in Notation Reference. The pitch may be omitted. If the pitch is omitted, the pitch of a current note will be the same as the pitch of the previous note in the input file, see (notation)Durations section ‘Durations’ in Notation Reference. Note that neither r nor s is a pitch.

The duration includes a number and optionally one or more dots. If a duration is not explicitly defined, the duration of a current note will be the same as the duration of the previous note, chord, rest, or spacer rest, see (notation)Durations section ‘Durations’ in Notation Reference.

Post-events follow the note to which they are attached. Suppose we want to have an eighth note c’ with a fingering of 1, a tenuto articulation, a slur beginning with the note, a tie beginning with the note, and a text annotation. This can be accomplished as shown.

 
{ c'8-1--(~^\markup{"text annotation"} c' d') }

[image of music]

See also

Learning Manual: Ties and slurs, Articulations and dynamics, Adding text.

Notation Reference: (notation)Pitches section ‘Pitches’ in Notation Reference, (notation)Rhythms section ‘Rhythms’ in Notation Reference, (notation)Expressive marks section ‘Expressive marks’ in Notation Reference.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.1.5 On the un-nestedness of brackets and ties

You have already met a number of different types of bracket and bracket-like constructs in writing the input file to LilyPond. These obey different rules which can be confusing at first. Let’s first review the different types of brackets and bracket-like constructs.

Bracket TypeFunction
{ … }Encloses a sequential segment of music
< … >Encloses the notes of a chord
<< … >>Encloses simultaneous music expressions
( … )Marks the start and end of a slur
\( … \)Marks the start and end of a phrasing slur
[ … ]Marks the start and end of a manual beam

To these we should add other constructs which generate lines between or across notes: ties (marked by a tilde, ~), tuplets written as \tuplet x/y { … }, and grace notes written as \grace { … }.

Outside LilyPond, the conventional use of brackets requires the different types to be properly nested, like this, << [ { ( … ) } ] >>, with the closing brackets being encountered in exactly the opposite order to the opening brackets. This is a requirement for the three types of bracket described by the word ‘Encloses’ in the table above – they must nest properly. However, the remaining bracket-like constructs, described with the word ‘Marks’ in the table above together with ties and tuplets, do not have to nest properly with any of the brackets or bracket-like constructs. In fact, these are not brackets in the sense that they enclose something – they are simply markers to indicate where something starts and ends.

So, for example, a phrasing slur can start before a manually inserted beam and end before the end of the beam – not very musical, perhaps, but possible:

 
\relative { g'8\( a b[ c b\) a] g4 }

[image of music]

In general, different kinds of brackets, bracket-like constructs, and those implied by tuplets, ties and grace notes, may be mixed freely. This example shows a beam extending into a tuplet (line 1), a slur extending into a tuplet (line 2), a beam and a slur extending into a tuplet, a tie crossing two tuplets, and a phrasing slur extending out of a tuplet (lines 3 and 4).

 
\relative {
  r16[ g' \tuplet 3/2 { r16 e'8] }
  g,16( a \tuplet 3/2 { b16 d) e }
  g,8[( a \tuplet 3/2 { b8 d) e~] } |
  \tuplet 5/4 { e32\( a, b d e } a4.\)
}

[image of music]


[ < ] [ > ]   [ << ] [ Up ] [ >> ]

This document was generated by gub on July 13, 2020 using texi2html 1.82.

Outros idiomas: català, česky, deutsch, español, français, magyar, italiano, 日本語, nederlands.
Sobre seleção automática de idioma.