# Engraving Challenges: Horizontal Spacing

This post is part of a series analyzing LilyPond’s performance during the preparation of a new edition of Oskar Fried’s songs.

Now that we’ve looked at structuring the input and beautifying slurs and ties, it’s time to move to the most important issue: how the notes are laid out on the page.

LilyPond usually produces very good horizontal spacing, mimicking traditional hand-engraved scores
(in particular, it employs “optical spacing” – this is something like kerning for musical notation). However, there are two areas where default spacing is unsatisfactory:

• vocal music with long lyric syllables,
• very tight music.

In case of vocal music, the most important problem is that long lyric syllables disturb the placement of the notes – LilyPond always centers the syllables under the respective noteheads, and if there are some long syllables under short notes, LilyPond has to move the notes away from each other to make room for these syllables. Of course, lyrics usually should be centered – but in some circumstances it’s better to move them around. I attempted to improve this by working on LilyPond’s code in summer 2012, but I failed (I still hope to get back to it, but we have to release a new stable version before I’ll be able to resume this work).

As for tight music, there are several small issues here – most of them are very subtle, but when combined they may produce subpar results. Example: in cramped conditions, accidentals, arpeggios, dots and other such elements should be moved closer to the notes to ensure optimal spacing. In fact, it would be best to actually have special narrow versions of some glyphs (particularly accidentals) for use in tightly spaced music. Until LilyPond learns how to do this, the spacing will occasionally get wrong even if theoretically there was enough horizontal space to lay out all elements.

Here’s one example:

broken spacing in op. 5 no. 1 (click to enlarge)

We decided to change the line breaking to avoid this problematic situation. However, sometimes this isn’t possible – in opus 8 no. 1 we also had a problem with tight spacing, but there was no reasonable line breaking alternative. As you can see below, the triplets in fourth measure are spaced a bit weirdly, and the last measure is uneven (3rd beat takes much more space than other beats):

op. 8 no. 1 – problems with spacing (click for pdf)

Here’s how this system looks after applying corrections. Do you see what I have done?

op. 8 no. 1 after adjusting spacing (click for pdf)

There were two changes: firstly, some lyric syllables were moved around to allow for more even note spacing. Secondly, I have squeezed the notation in the right-hand staff: notes and accidentals are slightly narrower, and the accidentals are moved closer to their respective notes. Some long lyric syllables were squeezed as well.

While the problems with default spacing are nothing to be proud of, the fix is actually very nice, and it demonstrates the power of LilyPond. I didn’t have to grab each notehead and each accidental and manually change it’s size and placement (God forbid using the mouse for such fine adjustments!). I simply created a function `\squeezeNotation`, like this:

```squeezeNotation = {
\override Staff.Accidental #'stencil =
#(lambda (grob)
(ly:stencil-scale (ly:accidental-interface::print grob) 0.92 1))
#(lambda (grob)
\override Lyrics.LyricText #'stencil =
#(lambda (grob)
(ly:stencil-scale (lyric-text::print grob) 0.92 1))
}```

and then I called the function at the beginning of the problematic passage, and then reverted it after the passage. If I ever need to unsqueeze the notation here, all I have to do is to remove two lines from the code. And since I already have the function defined, I can use it in many other similar situations!

Unfortunately, fixing a spacing problem may sometimes require more effort. There is one such irritating bug in the spacing algorithm: when music with unequal subdivisions is stretched (or compressed) a lot, the horizontal spacing may become heavily distorted (issue 3304). Here’s an example of very uneven spacing caused by this bug:

bad note spacing in op. 7 no. 1

To fix this issue, I had to put every affected system in a new spacing section, and manually adjust `common-shortest-duration` property of the `SpacingSpanner` in each of those sections. It was a lot of work, but the spacing is now perfectly fine:

this looks much better!

Again, I have defined a simple function that made these adjustments more convenient to make.

Update: Keith OHara fixed the bug that was causing uneven triplet spacing – hooray! Read more here 🙂

Finally, I’d like to mention one situation where I had the impression that LilyPond just didn’t want to listen to me. It was the last system of op.7-3, and the task was to adjust the horizontal positions of the noteheads in note columns, so that interlocking voices didn’t collide:

what are the augmentation dots in second measure thinking? Also, notice that some ties cannot be seen because they are too short.

I managed to fix this, but it took me a long time (and I’m a fairly experienced LilyPond user!):

Now, this makes more sense 🙂

This problem may have been caused by inappropriate voicing, but I’m pretty sure this was not the case. Anyway, it would be nice to have a more fool-proof interface for adjusting horizontal positions of the notes than what we have now 🙂

That’s all for now. Next time I’ll talk about vertical spacing, and the biggest patch in the history of LilyPond!

## 2 thoughts on “Engraving Challenges: Horizontal Spacing”

1. Pingback: Happy New Year! | Scores of Beauty