Extending the templates - GNU LilyPond
Description: GNU LilyPond

Next: How LilyPond files work, Up: Putting it all together

### 3.1 Extending the templates

You've read the tutorial, you know how to write music. But how can you get the staves that you want? The templates are ok, but what if you want something that isn't covered?

Start off with the template that seems closest to what you want to end up with. Let's say that you want to write something for soprano and cello. In this case, we would start with “Notes and lyrics” (for the soprano part).

    \version "2.7.39"
    melody = \relative c' {
      \clef treble
      \key c \major
      \time 4/4

      a4 b c d
    }

    text = \lyricmode {
      Aaa Bee Cee Dee
    }

    \score{
      <<
        \new Voice = "one" {
          \autoBeamOff
          \melody
        }
        \new Lyrics \lyricsto "one" \text
      >>
      \layout { }
      \midi { \tempo 4=60 }
    }

Now we want to add a cello part. Let's look at the “Notes only” example:

    \version "2.7.39"
    melody = \relative c' {
      \clef treble
      \key c \major
      \time 4/4

      a4 b c d
    }

    \score {
    \new Staff \melody
    \layout { }
    \midi { \tempo 4=60 }
    }

We don't need two `\version` commands. We'll need the `melody` section. We don't want two `\score` sections – if we had two `\score`s, we'd get the two parts separately. We want them together, as a duet. Within the `\score` section, we don't need two `\layout` or `\midi`.

If we simply cut and paste the `melody` section, we would end up with two `melody` sections. So let's rename them. We'll call the section for the soprano `sopranoMusic` and the section for the cello `celloMusic`. While we're doing this, let's rename `text` to be `sopranoLyrics`. Remember to rename both instances of all these names – both the initial definition (the `melody = relative c' {` part) and the name's use (in the `\score` section).

While we're doing this, let's change the cello part's staff – celli normally use bass clef. We'll also give the cello some different notes.

    \version "2.7.39"
    sopranoMusic = \relative c' {
      \clef treble
      \key c \major
      \time 4/4

      a4 b c d
    }

    sopranoLyrics = \lyricmode {
      Aaa Bee Cee Dee
    }

    celloMusic = \relative c {
      \clef bass
      \key c \major
      \time 4/4

      d4 g fis8 e d4
    }

    \score{
      <<
        \new Voice = "one" {
          \autoBeamOff
          \sopranoMusic
        }
        \new Lyrics \lyricsto "one" \sopranoLyrics
      >>
      \layout { }
      \midi { \tempo 4=60 }
    }

This is looking promising, but the cello part won't appear in the score – we haven't used it in the `\score` section. If we want the cello part to appear under the soprano part, we need to add

    \new Staff \celloMusic

underneath the soprano stuff. We also need to add `<<` and `>>` around the music – that tells LilyPond that there's more than one thing (in this case, `Staff`) happening at once. The `\score` looks like this now

    \score{
      <<
        <<
          \new Voice = "one" {
            \autoBeamOff
            \sopranoMusic
          }
          \new Lyrics \lyricsto "one" \sopranoLyrics
        >>
        \new Staff \celloMusic
      >>
      \layout { }
      \midi { \tempo 4=60 }
    }

This looks a bit messy; the indentation is messed up now. That is easily fixed. Here's the complete soprano and cello template.

>          \version "2.7.39"
>          sopranoMusic = \relative c' {
>            \clef treble
>            \key c \major
>            \time 4/4

>            a4 b c d
>          }

>          sopranoLyrics = \lyricmode {
>            Aaa Bee Cee Dee
>          }

>          celloMusic = \relative c {
>            \clef bass
>            \key c \major
>            \time 4/4

>            d4 g fis8 e d4
>          }

>          \score{
>            <<
>              <<
>                \new Voice = "one" {
>                  \autoBeamOff
>                  \sopranoMusic
>                }
>                \new Lyrics \lyricsto "one" \sopranoLyrics
>              >>
>              \new Staff \celloMusic
>            >>
>            \layout { }
>            \midi { \tempo 4=60 }
>          }

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

Other languages: English.
Using automatic language selection.