# Useful Lilypond Snippets, Macros and Solutions

Please contribute pieces or snippets of code which you have found useful in getting out of a difficult situation, macros which you have developed, or generally any useful tips to fellow Lilyponders on this project.

## How to prevent Lilypond from linebreaking unless I want it to?

Lilypond is quite unruly and may try to insert a linebreak whenever it finds a barline. Just after the barline that you want to prevent linebreaking, insert \noBreak e.g.

\bar "|" \noBreak


However Lilypond will now try to find another bar to break at. To prevent this, you can define a macro and call it within your music thus:

 nbbar = {\bar "|" \noBreak}

soprano = \relative c' { ... ... ... \nbbar ... ... \nbbar ... }


## How do I create overlapping notehead (which may be dotted)

When two vocal parts share a whole note, the image shows them as slightly overlapped, whereas Lilypond prints them side by side by default. Use \override NoteColumn #'force-hshift = #-0.xx on the lower of the vocal parts to force it leftwards to overlap. Usually only a small amount is needed. If the notes are dotted, Lilypond may print 2 dots. Turn the dot of the top one transparent. E.g.:

 soprano = \relative c' {
... ... ...
\once \override Dots #'transparent = ##t f1. \bar "||"}

alto    = \relative c' {
... ... ...
\override NoteColumn #'force-hshift = #-0.02 f1. \bar "||"}


When the shared notes are of different values (such as a quarter note and a dotted quarter note), the note head needs to be shifted in both parts. Use a positive shift value for the note appearing farther to the left and a negative (but numerically equal) shift value for the note appearing to the right. E.g.:

 soprano = \relative c' {
... ... ...
\override NoteColumn #'force-hshift = #-0.04 ees4.

 alto    = \relative c' {
... ... ...
\override NoteColumn #'force-hshift = #0.04 ees4


## How do I create 2 dots for merged noteheads?

When 2 vocal parts share a dotted notehead (less than a whole note in duration), Lilypond only prints 1 augmentation dot whereas the image uses two. There is no easy way. A macro has to be created and called from the lower of the two vocal parts which share the notehead. E.g. soprano and alto share a dotted g2.

 %% Macro for generating two augmentation dots for a notehead.
%% When dotted notes from two vocal parts share a single notehead,
%% Lilypond only inserts a single dot. In the image, two dots are used.
%% This macro generates the two dots by replacing the normal dot glyph
%% with a new glyph comprising a column of 2 dots one above the other.

augdots = {
\once \override Voice.Dots #'stencil = #ly:text-interface::print
\once \override Voice.Dots #'text = \markup {  \hspace #0 \raise #1
\line { \translate #(cons -0.2 0)
\override #'(baseline-skip . 1 )
\column {
\musicglyph #"dots.dot"
\musicglyph #"dots.dot" } } }
}

%%end of macro definition

soprano = \relative c' {
... ... a2 g2. f2 }

alto = \relative c' {
... ... a2 \augdots g2. f2 }


## How do I deal with incomplete measures?

Some of the hymns have irregular measures, or else are arranged with many partial measures in the middle of the piece. If timing is left on, Lilypond will attempt to insert barlines automatically at where it thinks the proper measure boundaries are, leading to extra unwanted barlines. If | is used to call for a barline, | must fall at what Lilypond considers a proper measure boundary otherwise there will be a barcheck fail warning. Strategies for dealing with this are:

• Turn timing off (in the layout block): \context { \Score timing = ##f }. However this creates problems with accidentals because now Lilypond thinks the entire score is one huge measure.
• Use \bar "|" instead of |.
• \partial can be used for pickups at the beginning of a piece, however the Lilypond manual does not recommend using it for partial measures in the middle of a piece (although this has been tried without ill-effect. The only problem is that it messes up bar numbering but we are turning bar numbering off for this project.)

The recommended method in the manual is to use

 \set Score.measurePosition = #(ly:make-moment X Y)


where X, Y are numbers meaning X number of Y duration notes have already past in the current measure. For example here is a snippet from a 4/2 meter piece:

d1 \bar "||" \set Score.measurePosition = #(ly:make-moment 2 2)  b2 a2 \bar "|" a4 g4


There is a partial measure "b2 a2". \set Score.measurePosition indicates that when "b2 a2" begin, 2 1/2 notes have already past, and therefore the measure should end after "b2 a2".

## How do I squeeze the music into as few pages as possible?

The lyrics can be presented in 1 or 2 columns. Copy the code from a piece where the lyrics are in 2 columns.

insert "annotate-spacing = ##t" in your paper block. This turns on lots of arrows and numbers showing how much space each item in your score takes up and gives you an idea of what spaces can be reduced with appropriate \paper or \layout settings.

## How do I insert a page break when I want it?

Within a music score, page breaking is easily controlled and this is discussed in the manual. However in this project, most of the time you want a page break after a few verses of lyrics or other markup text. This question has been posed in the Lilypond by other users before, with no satisfactory answer. The only way to force a page break is to insert a "fake" score block (which prints a blank score and does not take up any space).

 %%fake score block to occupy space and force a pagebreak.  Can't think of a better way of doing this.
\score{
{ s4 }
\header { breakbefore = ##t piece = ##f opus = ##f tagline = ##f }
\layout{
\context {
\Staff\remove Time_signature_engraver
\remove Key_engraver
\remove Clef_engraver
\remove Staff_symbol_engraver
}}}


The setting to tweak is "breakbefore". Set it to ##f if you have some space at the bottom of the page which you want the fake score to fill. Any markup or other items after it will start on a new page. Set it to ##t if you want to force a page break before the fake score. Any items after it will start at the top of the new page.

## Slurs, all sorts and shapes

Type of slur can be changed with \slurDashed, \slurDotted, \slurSolid.

To control the shape of the slur, \override Slur #'control points can be used. See this example:

 \override Slur #'line-thickness = #'2.5
\slurDotted
\once\override Slur  #'control-points = #'(( 1.6 . 0.8 ) ( 3.5 . 0.5 ) ( 4.4 . 0 ) (5.6 . -0.5 ))
e2( b1)


Many tweaks are being done to this slur between e2 and b1. We want a dotted slur which is made thicker (to make the dots more visible). Finally we set #'control-points. This is a list of pairs of numbers which specify coordinates through which the slur will travel. The first pair is the point of origin of the slur, ( 0 . 0 ), X=0 means the note column of the first note, and Y=0 means the centre (3rd line) of the staff. The final pair specifies where the slur ends. As far as I can determine, you are only allowed 4 pairs of control points, but this is usually enough for a slur of any shape, even a straight line.