LilyPond Tips

Or: how to do things that might not be obvious just from reading the manual

LilyPond is a powerful program that can produce beautiful output. But it's not always easy to figure out how to do some useful things that aren't part of the basic syntax. In the course of setting up vocal scores (mostly), I've poked around the LilyPond mailing list and the web to find out how to do some handy things, which I document here for your reference (and mine).

The tips here are for 2.7.38 or better (though they might work in older versions). You can see a lot of them in action in the source code available on my hymns page. (You might even find out other things by browsing scores there.)

I don't claim that anything here is necessarily the best way to accomplish the task at hand; I only claim that they worked for me. I make no guarantees that they'll work at all on your system; i.e., if they reduce your computer to a molten hunk of silicon, that's your problem, not mine, and the same goes for all other disasters, inconveniences, and simple frustrations.

While LilyPond is not in Perl's league when it comes to multiple ways to do same thing, there often are several equally practical ways to lay out a given score, and your preferences might not match mine. In particular, I'm a fan of putting things in the \layout block whenever possible, and I'm not a fan of \new ... \with. If your preferences are the reverse, you'll want to adapt things I've done here to your tastes.

Please note that I know next to nothing about the internal workings of LilyPond. Most of what's here I either figured out through painful trial-and-error (which means you could do the same) or by browsing the LilyPond mailing list archives (ditto). So if you want to know how to do something that's not listed here, it won't do you any good to email me asking how to do it because I probably don't know.

All Lilypond code on this page is in monospace font, like this.

Just to make things clearer, here's a basic LilyPond file with my names for each section labelled so you'll know what I mean when I refer to something.

% "headers" follow
\include "english.ly"
% end of "headers"


% "music definitions" follow
melody = \relative c' {
  d2 e f1
}

bass = \relative c {
  bf2 c <f f,>1
}
% end of "music definitions"

% beginning of "score block"

\score {
  \context ChoirStaff <<
    \context Staff = upper <<
      \context Voice =
        sopranos { \voiceOne << \melody >> }
    >>
    \context Staff = lower <<
      \clef bass
      \context Voice =
        basses { \voiceTwo << \bass >> }
    >>
>>

% beginning of "layout block" (within score block)

\layout {
  }

% ending of "layout block"
}

% ending of "score block"

How do I . . . .

print lyrics under reciting notes?
set LaTeX margins in lilypond-book?
put a note in parentheses?
put verse numbers before every line, not just the first?
suppress the time signature?
put more space between notes?
hide a phrasing slur?
change the size of the lyrics?
add more space between words in the lyrics?
change the spacing between stanzas?
put double quotes in lyrics?
add verses below the music?
add a refrain before verses?
print a whole verse in italics?
turn off bar numbers?
put single rests in choral music?
add markup next to a note?
add a translator to the headers?

Printing lyrics under reciting notes.

Sometimes a single note is used for multiple syllables; this is often the case in church music in chant-like contexts. Merely enclosing all the syllables in quotation marks doesn't work well, since the note is centered over the line. The note needs to be left-aligned with the words. To do that, make your lyrics look like this:

nor- mal ly- rics \once \override LyricText #'self-alignment-X = #-1 "these words are left-aligned under one note "

Back to top

Setting LaTeX page margins for lilypond-book

THIS HAS NOT BEEN REVIEWED IN 2.7.38. IT WAS TRUE IN 2.6.4

(I'm having trouble with lilypond-book in Windows, and Cygwin doesn't have a 2.7 version available, so I haven't been able to check this yet.)

I am reliably informed that lilypond-book does indeed recognize packages (contrary to what I said on an earlier edition of this page), but I haven't been able to get it to work right. When I do, I'll try to put further instructions here. In the meantime, here's how I set LaTex page margins:

LaTeX measures all distances from a spot one inch from the left edge of the page and one inch from the top edge of the page. So if you want page margins of one-half inch, and assuming you're using letter paper (8.5 x 11):

\setlength{\evensidemargin}{-0.5in}
\setlength{\oddsidemargin}{-0.5in}
\setlength{\textwidth}{7.5in}
\setlength{\topmargin}{-0.5in}
\setlength{\textheight}{9.5in}

I'm missing something important because the margins still don't quite come out right, but playing with \textwidth and \textheight will fix it.

Back to top

Putting parentheses around a note

This was made much easier in the 2.7 versions of LilyPond. It's so much easier that if you want to do this and aren't running 2.7 or better, I'd seriously consider it. If you really can't upgrade, directions are here. After you've looked at them, I suspect you'll decide converting to 2.7 is a lot easier.

Let's assume you want to give the basses an optional low note. Set it in a chord, like this:

< f \parenthesize \tweak #'font-size #-1 f, >2

The \tweak part makes the note a little smaller, which I prefer (I might even use #-2). If all you want is the parentheses, omit the \tweak #'font-size #-1.

Back to top

Numbering every line of a verse

When typesetting a hymn with lots of verses, it can be helpful to put the stanza number in front of every line, rather than just on the first line. To do that, don't use \set stanza at all. Instead, use both \set vocalName = "1. " and \set vocNam = "1. ". This will probably produce verse numbers on top of lyrics, which isn't good. To get around that, add this to the \layout block of your score:

  \context {
    \score
    \override VocalName #'extra-offset = #'(2 . 0)
  }

Change the 2 to whatever it takes to make your output look good; the larger the number, the farther left the verse number prints.

I've also run into situations where the verse number doesn't print on the first line when doing this; if that happens, do use \set stanza (even though I said not to above).

Back to top

Turning off the time signature

Some music really doesn't need a time signature (chant) or changes it so frequently that it's more confusing than helpful to show them. There are two (at least) ways to turn off time signatures and barring.

Method 1 is lifted straight from the Gregorian chant template in the documentation. At the beginning of the music definition, add the bold-faced lines below:

melody = \relative c' {
  \set Score.timing = ##f
  \override Staff.TimeSignature #'transparent = ##t

One drawback to this is that LilyPond saves space for the time signature, even though the signature itself doesn't appear on the output. This can result in music that starts at an unsightly distance from the beginning of the first system. Another problem is that the Staff.TimeSignature line has to appear at least once for every staff that's used. Method 2 solves both of these problems.

Method 2 involves two things. First, at the start of your music definition, add the bold-faced line:

melody = \relative c' {
  \cadenzaOn

Second, in the layout block of your score, add the bold-faced lines below:

\layout {
  \context {
    \Staff
    \remove "Time_signature_engraver"
  }

You can probably use \cadenzaOn or the Score.Timing lines interchangeably--I haven't noticed any difference between the two. But if something obscure breaks in your score, you can always try switching to the other one and see if that fixes it.

(Update: Upon reviewing property-init.ly, I found out that \cadenzaOn is equivalent to \set Timing.timing = ##f. I do not know what difference Timing instead of Score makes; I suspect that one has a wider effect than the other. Experiment as needed.)

Back to top

Putting more space between notes

The default spacing between notes on a score produces a pretty busy-looking page if there are a lot of black notes and no lyrics to force wider spacing. Insert this into the \layout block to get more space:

  \context {
    \Score
    \override SeparationItem #'padding = #0.2
  }

The 0.2 can be changed to whatever makes your score look good. The larger the number, the more the extra space.

Back to top

Hiding phrasing slurs

When I'm typesetting music with slurred eighth-notes, the beaming on the eighths is usually enough to indicate that they're slurred; an actual slur is just visual clutter. But LilyPond still needs to know they're slurred so that lyrics will align correctly.

Plan A: If auto-beaming is off, then manually beamed notes are treated as slurred notes. So put \autoBeamOff at the start of the music definition and beam eighth notes manually (e.g., e8[ d]). If you want to let LilyPond assign some beams on its own, then put \autoBeamOff right before the notes you want to beam manually, and \autoBeamOn right after them.

Plan B: Make slurs invisible by adding \override Slur #'transparent = ##t to your music definition. That will get rid of all slurs. If you only want to get rid of some of them, put \once \override Slur #'transparent = ##t before the notes that should have an invisible slur.

If you're going to be doing that a lot, add this to the file before any music definitions:

hideSlur = \once \override Slur #'transparent = ##t

Then put \hideSlur before the notes with a to-be-hidden slur.

Plan A is a lot easier, but you may run into situations where B fits your needs better.

Some unison hymns have a melody line that's repeated as the top line of the accompaniment. The accompaniment is often printed with no slurs (even on longer notes). To do this, add the following somewhere toward the top of your file, before the music definitions:

optSlur = \tag #'withoutSlur { \once \override Slur #'transparent = ##t }

Then, in the music definitions right before the slur you want in one part but not the other, add \optSlur. Then, in the score block, before the music that should not have the slur, put either \keepWithTag #'withoutSlur or nothing at all (it will keep the tagged material by default anyhow). Before the music that should have the slur, put \removeWithTag #'withoutSlur, somewhat like this:

\context Voice = sopranos { \voiceOne \removeWithTag #'withoutSlur \soprano }

Back to top

Changing the font size in lyrics

Add this to your \layout block:

  \context {
    \Lyrics
    \override LyricText #'font-size = #-1
  }

This makes a relative change to the font size; the above makes it one size smaller. #2 would make it two sizes bigger, #-2 would make it two sizes smaller, and so forth.

Back to top

Putting more space between words in the lyrics

The 2.7 series can have a problem with insufficient (or little to no) space between words in the lyrics. The manual deals with this in section 7.3.7.1, but the fix presented there (at least as of the time I write this) is not an option I like. I have two other suggestions in its stead.

My first suggestion is to change the font size to a smaller one, which often solves the problem at the cost of slightly smaller lyrics. I think the default font is a little too big anyhow, so you may find this works well enough, in which case you're done.

If that doesn't solve the problem, or if smaller lyrics aren't a viable option, add this to your \layout block:

  \context {
    \Lyrics
    \override LyricSpace #'minimum-distance = #0.6
  }

The #0.6 is changeable to suit your tastes. The larger the number, the wider the minimum separation between words.

If you already have a \Lyrics section (e.g., from the previous tip), just add the \override line to it.

Back to top

Changing the space between stanzas

The spacing between lines of stanzas is generous--maybe a little too generous. To change it, add the following to your \layout block:

  \context {
    \Lyrics
    minimumVerticalExtent = #'(0 . 0)
  }

If you already have a \Lyrics section (e.g., from the previous tip), just add the minimumVerticalExtent line to it.

If you want to expand the space rather than shrink it, try #'(-1.5 . 1.5), and keep increasing the numbers until you like the results.

Back to top

Putting double quotes in lyrics

If you're using a UTF-8 capable editor (e.g., jEdit, Emacs [with some adjusting]), I suggest inserting actual left and right double quotation marks into your score. The output will look much better, and the input will look much less busy.

If for some reason this isn't possible:

If you want "these words in quotes" in your lyrics, you'd enter them like this:

"\"these" words in "quotes\""

In other words, any word preceded or ended by a quote should itself be put into quotes, and the extra quotation mark (the one you want to be visible) preceded by a backslash. (It's easier to see by example than by that convoluted explanation, which is why I put the example first.)

Back to top

Putting more verses below the lyrics

When setting a hymn with lots of verses, putting the words to all the verses between the staves will result in the lower staff being a long way from the upper staff, making it hard to play. After shrinking the font size as far as you can without making the words too hard to read, the next thing to do is to move the excess verses below the music. This is covered in the manual (starting with 2.7), but there's more you can do than is apparent there.

Say you want something like this below the music:

4. Verse four's first line
And verse four's second
Is followed by a third line
And then by a fourth

5. Verse five's first line
And verse five's second
Is followed by a third line
and then by a fourth

To make this happen, at the bottom of the file and outside of any enclosing blocks, add this:

\markup {
  \large { % \teeny \tiny \small \normalsize \large \huge
           % are all viable options here, with \normalsize
           % the default. Pick whatever looks good.
    \fill-line { % This centers the words, which looks nicer
      \hspace #1.0 % gives the fill-line something to work with
      \column {
        "4. Verse four's first line"
        "And verse four's second"
        "Is followed by a third line"
        "And then by a fourth"
        \hspace #1.0 % (Basically inserts a blank line; the argument is irrelevant)
        "5. Verse five's first line"
        " . . . etc. . . . "
      }
      \hspace #1.0 % gives the fill-line something to work with
    }
  }
}

If you're running onto a second page and the verses aren't too wide, you can try this to put the verses into two columns:

\markup {
  \fill-line {
    \hspace #1.0 % add more of these at the beginning and end
    \hspace #1.0 % to move the verses closer together
    \column {
      "5. The fifth verse goes here"
      "With all its many lines"
    }
    \hspace #3 % The argument does matter here!
    \column {
      "6. The sixth verse goes here"
      "With all its many lines"
    \hspace #1.0 % Delete these at beginning and end
    \hspace #1.0 % if the verses are wide and you need room
    }
  }
}

It gets a little trickier if you want the verse numbers outdented from the text, like this:

4. Verse four's words
   Go right here

What makes it complex is that you probably want the words in a standard proportional font, not a typewriter-like one, and therefore can't just space the words over. Fortunately, the solution is not as bad as one might fear:

\markup {
  \normalsize {
    \fill-line {
      \hspace #3.0
      \line {
        "3. "
        \column {
          "Verse four's words"
          "Go right here"
      }
      \hspace #3.0
    }
  }

Extending this to more verses and to printing verses both across the page and down can be done by combining the above techniques, which I leave as an exercise for the reader.

If there's a way to spill neatly onto a second page, I don't know what it is. So if all of the above fails, you're going to have to learn how to use LaTeX and lilypond-book.

Back to top

When the refrain goes first

Occasionally you'll run into a hymn where the refrain goes first rather than last (e.g., "All Glory, Laud and Honor"). There are two challenges here: Getting the verses to line up right with the music, and getting the opening staves to shrink when there's only one line of words between them. (This latter challenge is handled automatically in the 2.7 version of LilyPond, thus becoming no challenge at all).

First challenge: Getting the verses to line up. To do this, define the refrain and verse music separately:

refrain = \relative c' {
  c4 c c c
}

verse = \relative c'' {
  g4 g g g
}
refrainWords = \lyricmode {
  re -- frain re -- frain
}

verseOne = \lyricmode {
  \set stanza = "1. "
  verse one verse one
}

verseTwo = \lyricmode {
  \set stanza = "2. "
  verse two verse two
}

Then, in the score, put something like this:

    \context Staff = up <<
      \context Voice = refrain { \voiceOne \refrain
        \context Voice = verse { \voiceOne \verse }
      }
      \context Lyrics = one \lyricsto refrain \refrainWords
      \context Lyrics = one \lyricsto verse \verseOne
      \context Lyrics = two \lyricsto verse \verseTwo
    >>

If you're in 2.6, you then need to handle the second challenge by putting this in your \score block, immediately after the opening brace or << of the first \context Staff:

\override Score.RemoveEmptyVerticalGroup #'remove-first = ##t

(This one's in the documentation under "Hiding Staves". Apparently lyrics are in effect a stave unto themselves.)

Back to top

Printing a verse in italics

I needed this when printing Latin verses with English ones; putting the Latin in italics helps differentiate it. I've also seen hymnals which put every other verse in italics, presumably to help singers follow the verses. Whatever the reason you want to do it, here's how:

verseOne = \lyricmode {
  \override LyricText #'font-shape = #'italic
  Words to verse one fol -- low here
}

The \override must to be put at the beginning of each verse you want in italics.

This will not put the stanza number into italics. If you want to do that, too, then add another override:

  \override StanzaNumber #'font-shape = #'italic

Back to top

Adding a translator to the headers

Even though the LilyPond manual mentions the existence of a texttranslator field (at least through the late 2.7 series), it doesn't appear in the output. I want it to appear right below the name of the poet, so I make the header field look like this:

  poet = \markup {
    \override #'(baseline-skip . 2.5)
    \column {
      "Poet name" "Translator name" " "
    }
  }

baseline-skip is tweaked because I think there's too much space between the poet and translator lines by default. The 2.5 can be tuned to taste, and the whole line can be removed if you like things the way they are by default.

Back to top

Turning off bar numbers

This one is now explicit in the documentation (starting with 2.7.39), but I'll leave it here for those using older versions or having trouble finding it in the manual. Add this to your \layout block:

  \context {
    \Score
    \remove "Bar_number_engraver"
  }

Back to top

Manual placement of rests

Another way to ask the question addressed here is, “How do I get rests to work right with \voiceOne, \voiceTwo, etc.?”

The automatic placement of rests doesn't work very well when using \voiceNNN. Usually they're too high on upper parts and too low on lower parts. This is glaringly obvious when placing a rest in one part and a skip or a note (placed well away from the center of the staff) in the other--I'd like to see the rest centered on the staff, but instead it ends up on the top line of the staff or on a ledger line below the staff (depending on which part has the rest and which has the skip).

The manual suggests using, for example, b2\rest to place a rest in the middle of a G clef. This works in isolation, but something connected with the \voiceNNN implementation breaks it. (This is a known bug, but there's no fix yet.)

The only solution I've found is to turn the voicing off when setting a rest, and then to turn it back on after the rest, like this:

\oneVoice b2\rest \voiceOne

This needs to be done only for the part(s) with the rest(s). The part(s) with skips or notes can be left unchanged.

Be careful to use the same \voiceNNN command throughout the same part; that is to say, be sure to use \voiceOne in the music definition if that's how you set up the score. Your music might turn out OK anyhow, but why chance it?

Back to top

Putting markup next to a note

Except for fingering notations, LilyPond has no automatic way to accomplish this, so if you came here looking for that, sorry. It doesn't exist. All you can do is to move the markup manually by adding something like this before the note:

\once \override TextScript #'extra-offset = #'(-5 . 4)
d2_\markup \tiny \italic "Org."

The first number of the parenthesized pair of numbers is the horizontal shift, measured in the size of one space on a staff. Negative numbers go left. The second number is the vertical shift, measured in the same units. Positive numbers go up. I know of no canned procedure to calculate them--trial-and-error is your friend in this case.

Note also that this is in the manual, in section 4.3, but it's not immediately apparent that "Dealing With Overlapping Output" has the answers you want, nor that there's no way to do this automatically (the manual can scarcely list everything for which there's no automatic procedure).

Back to top

Parenthesizing notes in 2.6

It's a pain. You don't want to. Use 2.8 instead! But if you really can't upgrade . . . .

Let's assume you're printing choral music and the bass line goes lower than you think the basses can handle. The obvious thing to do is to set this as a chord:

\include "english.ly"

melody = \relative c' {
  d2 e f1
}

bass = \relative c {
  bf2 c <f f,>1
}

\score {
  \context ChoirStaff <<
    \context Staff = upper <<
      \context Voice =
        sopranos { \voiceOne << \melody >> }
    >>
    \context Staff = lower <<
      \clef bass
      \context Voice =
        basses { \voiceTwo << \bass >> }
    >>
>>

But this doesn't show which note is primary and which is optional. By default, LilyPond always (AFAIK) engraves all notes in a chord with the same engraver, so changing the property for one note changes it for all of them, so something like this won't work:

< \small f f,>1

because you can't put a modifier inside the angle brackets (you'll get a syntax error). This won't work either:

\small < f f,>1

because all it does it make both notes small. (The same issues apply when adding parentheses, but they're even more complex, so we'll stick with small notes for now).

(Update: There's code in the documentation for working on only one item in a chord, but it would add an order of magnitude more messiness to an already messy solution. What I give here will work much more easily and to the same effect.)

Here's the most general way to make it work:

bass = \relative c {
  bf2 c f,1
}

altbass = \relative c {
  \small s2 s f1
}
. . . .
    \context Staff = lower <<
      \clef bass
      \context Voice =
        basses { \voiceTwo << \bass \altbass >> }
    >>

So now you know how to handle the two notes separately. (You'll probably have to insert a \stemUp or \stemDown in the \altbass code to get them to line up properly with the principal notes.)

Another much simpler way to do this might work if you're setting multiple parts per staff, such as most SATB hymns. Assuming you want the small/parenthesized note in the bass line, and assuming that the tenor line has a note of the same duration in the same place as the bass line, add the "real" bass note as a chord in the tenor line (e.g., <e c> if the tenor note is e and the bass note is c) and then set the lower note in the bass line by itself.

tenor = \relative c' { a g f <e c> }
bass = \relative c { c c c \small c, }

In either case, the small note will probably not line up correctly with the larger ones; this is less visible when working with whole notes, but hard to miss when looking at anything with a stem. Adding \override NoteColumn #'force-hshift = #0.2 before \small will help a little (adjust the 0.2 to taste), but it's still not going to look great.

Putting parentheses around the notes is an added level of complexity. The key is found in stencil-hacking.ly. The technique is easier to show than to explain. Put the following code at the top of your file (after \version (if you use it) and any initial \include statements you may need.



#(define (parenthesize-callback callback)
   (define (parenthesize-stencil grob)
     (let* ((fn (ly:get-default-font grob))
            (pclose (ly:font-get-glyph fn "accidentals.rightparen"))
            (popen (ly:font-get-glyph fn "accidentals.leftparen"))
            (subject (callback grob))

            (subject-dim-x (ly:stencil-extent subject 0))
     (subject-dim-y (ly:stencil-extent subject 1)))
     (set! subject
            (ly:stencil-combine-at-edge
            (ly:stencil-combine-at-edge subject 0 1 pclose 0.2)
            0 -1 popen 0.2))
     (ly:make-stencil
     (ly:stencil-expr subject) subject-dim-x subject-dim-y)))
   parenthesize-stencil)

And rewrite the altbass section like this:
altbass = \relative c {
  \override NoteHead #'print-function
  =
  #(parenthesize-callback Note_head::print)
  \small s2 s f1
}

If you're using the simpler method outlined above, change \override to \once \override

(Or use 2.7+!)

Back to top