How LilyPond files work - GNU LilyPond
Description: GNU LilyPond

Next: Score is a single musical expression, Previous: Extending the templates, Up: Putting it all together

### 3.2 How LilyPond files work

The LilyPond input format is quite free-form, giving experienced users a lot of flexibility to structure their files however they wish. However, 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 File structure.

Most examples in this manual are little snippets – for example

    c4 a b c

As you are (hopefully) aware by now, this will not compile by itself. These examples are shorthand for complete examples. They all need at least curly braces to compile

    {
      c4 a b c
    }

Most examples also make use of the `\relative c'` (or `c''`) command. This is not necessary to merely compile the examples, but in most cases the output will look very odd if you omit the `\relative c'`.

>          \relative c'' {
>            c4 a b c
>          }

Now we get to the only real stumbling block: LilyPond input in this form is actually another shorthand. Although it compiles and displays the correct output, it is shorthand for

    \score {
      \relative c'' {
        c4 a b c
      }
    }

A `\score` must begin with a single music expression. Remember that a music expression could be anything from a single note to a huge

    {
      \new GrandStaff <<
        insert the whole score of a Wagner opera in here
      >>
    }

Since everything is inside `{ ... }`, it counts as one music expression.

The `\score` can contain other things, such as

    \score {
      { c'4 a b c' }
      \layout { }
      \paper { }
      \midi { }
      \header { }
    }

Some people put some of those commands outside the `\score` block – for example, `\header` is often placed above the `\score`. That's just another shorthand that LilyPond accepts.

Another great shorthand is the ability to define variables. All the templates use this

    melody = \relative c' {
      c4 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 names – it could be `melody`, `global`, `pianorighthand`, or `foofoobarbaz`. You can use whatever variable names you want. For more details, see Saving typing with identifiers and functions.

For a complete definition of the input format, see File structure.

This page is for LilyPond-2.8.8 (stable-branch).

Other languages: English.
Using automatic language selection.