Typesetting music with LilyPond

I tried typesetting music in LaTeX some time ago and gave up. The packages I found were hard to install, the examples didn’t work, etc. This weekend I decided to try again. I tried plowing through the MusiXTeX documentation and got no further than I did last time.

I posted a note on StackOverflow and got some good responses. Nikhil Chelliah suggested I look at LilyPond. I had looked at LilyPond before, and @jleedev explained how to integrate LaTeX and LilyPond.

Here’s some sheet music I included in my previous post, March in 7/4 time.

sheet music example

Here’s a full-sized PDF file version of the music above. And here’s the LilyPond source code used to create the music.

\relative c' {
\time 7/4
\key f \major
\clef treble
f g f \times 2/3{ c8 c c} f4 g a
g a8. bes16 a4 g f g c,
f g f \times 2/3{ c8 c c} f4 g a
g a8. bes16 a4 g f e f
}

The notation looks cryptic at first, but it makes sense after a few minutes. The command relative c' means that the following pitches will be relative to middle C. For example, the first note, F, is the F closest to middle C. Each note is the same length as the previous note by default, and the first note is a quarter note by default. The notation c8 means that the C is an eighth note, except it’s in the context of a triplet (times 2/3) and so it’s an eighth note triplet. The next F is denoted f4 to indicate that we’re back to quarter notes.

The notation a8. says that the A is a dotted eighth note. For the next note, bes16 means a B-flat sixteenth note. The suffix “es” stands for “flat” and “is” stands for “sharp.” (The documentation says it’s Dutch. I’ve never seen it before.) I don’t understand why I had to tell it that the B was flat. The code specified earlier that the key was F major, which implies B’s are flat. I suppose the code for individual notes is decoupled from the code to draw the key signature. That would make entering music painful in keys that have lots of sharps or flats. Maybe there’s a way to specify default sharps or flats.

The comma in c, gives the absolute pitch of the C. In relative mode, LilyPond assumes by default that each pitch name refers to the pitch closest to its predecessor. The C closest to the previous note, F, would have been the C up one fourth rather than down one fifth, so the comma was necessary to tell LilyPond to go down.

If I were to do a lot of music processing, I’d probably look at a commercial package such as Sibelius. But for now I’m just interested in producing small excerpts like that above, and it looks like LilyPond may be fine.

Update: I double checked the rules about flats etc. Yes, I do have to specify explicitly that the B in this example is B-flat. If I just say b rather than bes, LilyPond will add a natural sign in front of the B! It’s strange. It is aware of the key signature: when I tell it the B is flat, it says “OK, then I don’t have to mark that specially since it’s implicit in the key signature.” And if I don’t tell it the B is flat, it says “Oh, that’s an exception to the key signature. Better mark it with a natural sign.”

18 thoughts on “Typesetting music with LilyPond

  1. I personally hate source code for sheet music. It looks so ugly, and it’s never implemented correctly because of the expressiveness of the songs. For only one instrument (or one hand), it’s kind of fine, though.

  2. I never quite got into using Lilypond for music. For some reason it just doesn’t feel right to me. I think, for me, it’s because writing music is still faster by hand than it is on the computer, unlike with typing regular text. Also, I think writing music is more of a spatial process, and so it helps to be able to see the whole sheet, which I find hard to do on the computer (same is true with software like Finale). I guess it might help to have a huge monitor or something (projector?).

  3. I use Lilypond for all my music typesetting; I’ve gotten so it’s about three times as fast for me to type Lilypond as to write out a score. I also have had fun with writing Scheme for it; this is an exceptionally fun example (not mine), but you can do simpler, less impressive things to cut out repetitive work. You could probably make a “virtual Philip Glass” and put him out of business . . .

  4. Stick with Lilypond. In terms of engraving quality, it’s far and above anything Finale and Sibelius will ever produce and nearly on par with the real music typesetting software such as SCORE. Although Sibelius / Finale may seem easier / faster, lily wins hands down because you very rarely have to mess around laying things out manually.

    Lily will never be a composing tool (nor, for that matter will Sib / Finale). For that, Rosegarden + Ardour + JACK etc. is a great solution and can be integrated into a professional setup with the commercial sample libraries for realisations / demos and the like.

  5. I downloaded lilypond.
    I also placed a lilypond code in a latex file.
    Texshop won’t process it. It gives an error.
    I don’t see from the attached info how to get Latex to recognize a lilypond command.
    I use Snow Leopard.
    THANKS for your help

  6. I would like to get my kids (14 and 12) into LaTeX (definitely in high school) and maybe LilyPond, but right now they’re having a blast with Sibelius, writing music, listening to their creations, and going back and forth between their instruments and the computer. I think LilyPond would slow them down, and I wouldn’t want them to get the wrong impression of free software, which is most of the time quite wonderful. I like the Scheme example Jason mentioned–it has the potential to show them connections between music and mathematics.

  7. Hello! I am looking for a program which allows me to type many little pieces of music (like a measure, four beats) in a line for my drum class. Do you know how can i do that if i can?

    Thanks,

  8. Martin Tarenskeen

    Hi,

    I am Dutch and a very happy Lilypond user.
    I can tell you German use “B” for B-flat, Dutch do NOT.
    LilyPond syntax is based on the note that sounds, not the sound that is notated, and it is based on the Dutch notenames by default. Therefore you have to write “bes” even if you have written “key f major”. If you want to seriously use LilyPond read the documentation and join the Lilypond mailinglist. You will get all the help you need. It is worth the effort.

  9. The requirement to specify sharps, flats etc. whatever the key signature is actually quite important, because otherwise you could run into trouble copy-pasting music. Suppose that you copy the last bar of your music and copy-paste it into a section where the key signature is C major. If you hadn’t specified the B-flat, you’d suddenly have that note change to a B-natural in its new location. Or suppose that you now remove or change a key signature for stylistic reasons, or add a key signature to a location. Again, without the requirement to enter the exact pitches, you would now have to manually tweak a lot of notes.

    If you don’t like the Dutch syntax for note names, it’s perfectly possible to use an alternative from a variety of different languages, including English, French, German and others. See:
    http://lilypond.org/doc/v2.16/Documentation/notation/writing-pitches#note-names-in-other-languages

  10. How (if possible) can I use “lilypond” with “beamer”. This will be very useful.

  11. I have worked a little with musixtex, lilypond, and abc. These are interesting applications for music typesetting in the *TeX environment; but, for me, they are more useful for short music snippets since using an application like musescore gives all the facilities to edit (and compose) scores interactively. Also, for typesetting purposes, musecore can export in formats (such as SVG) that can be imported by *TeX (or transformed through MusicXML) with full quality! Anyway, from the 3 above, actually I prefer abc(plus) since its syntax is very easy and its graphical output is auite suitable for mixing text and music excerpts in more complex documents (where, by the the way, the “harmony” latex package can be handy!). I must admit however, that LilyPond (with the Frescobaldi editor) is also a nice piece of software and that I have tried with success, despite the plethora of auxiliary files that it creates when integrated in a LaTeX work-flow ;-)

Comments are closed.