# Optical Spacing (The Swan, Part 2)

Last week I wrote about a new edition of Saint-Saëns’ famous “Swan” for which I prepared a new piano arrangement. In the closing of the article I showed the exception to the rule, namely an issue where LilyPond did not serve me well right from the start, with this example of particularly ugly horizontal spacing:

Surprisingly irregular default spacing (click to enlarge)

Of course there is a piano part in addition to that cello line, but nevertheless it seemed more than strange that LilyPond should be doing that badly, and I had to find a solution (and more urgently, a cause) for the issue. As it turned out, my spacing issue is an ugly side-effect of a major LilyPond strength: optical spacing. Today I will show you in some detail what this is about – and why it is a limitation in this special case.

### Optical Spacing

LilyPond works very hard to avoid the mechanical appearance of many computer-engraved scores, actually this has been of the main incentives to create the program in the first place. One of the “secrets” of LilyPond’s outstanding readability and “vintage” appearance is the technique of optical spacing. The horizontal position of notes (and other items) is not only calculated through mathematical proportions of their rhythmic value, instead other factors such as the actual outline with remaining whitespace or the perceived weight of objects are taken into account as well.

Looking closely at the right hand of the piano one can see that the semiquavers are spaced out technically irregularly (consider the absolute distance between adjacent stems) but appearing extremely balanced:

Technically irregular semiquavers (click to enlarge)

While the second and third notes of each group have stems next to their left side there is some empty (or “white”) space to the left of the fourth note. In order to produce a balanced impression LilyPond now pulls the fourth note slightly to the left, as if that empty space had some gravity. This is a somewhat similar approach to what kerning does in text typography and something that a hand engraver would do as well. It is interesting to compare that with output from other programs, Sibelius, SCORE and Amadeus. While the notes are spaced “perfectly“ in these programs they look irregularly because the white space between the third and fourth notes has more weight than the one before the third note.

Default spacing in Sibelius (click to enlarge)

Default spacing in SCORE (click to enlarge)

Default spacing in Amadeus (click to enlarge)

This may seem like a tiny detail, and you may ask why this should matter. But this sort of microtyphography really makes a difference and is one of the reasons why LilyPond scores are so beautiful by default. I can tell you from experience that this difference has direct impact on the readability of the music on the music stand and therefore also affects the actual performance. It is well worth (re-)reading the chapters in LilyPond’s introductory essay on Engraving Details and The LilyPond Story on LilyPond’s website.

So obviously LilyPond is doing “The Right Thing” for that right hand, and in a way it really provides the best result of the competition. But unfortunately it is obviously not the appropriate approach for the whole score, as it causes an unacceptable irregularity in the quavers of the left hand and the cello part. LilyPond should have realized (or been told to do so) that the spacing of the slower notes is more important than that of the faster figuration, and acted accordingly. If you compare LilyPond’s default engraving with solutions by Durand (1886) and Henle (2009) you can see that both publishers prioritize the quavers’ spacing over the semiquavers. But while Henle doesn’t show any optical spacing at all (well, presumably their notation program doesn’t know what this is) the hand-engraved Durand score slightly shifts the fourth semiquaver to the left, leaving more space to the right. The oldest rendering is definitely the best, preserving both regular quavers and optical spacing, while that resulting extra space between the groups even supports the structure and helps the eye.

Default spacing with LilyPond (click to view PDF)

Hand engraving by Durand 1886 (click to enlarge)

Computer engraving by Henle 2009 (click to enlarge)

Fortunately LilyPond can use the same approach as Henle, which is as simple as inserting `\override Score.SpacingSpanner.uniform-stretching = ##t` in a stylesheet:

LilyPond engraving with optical spacing switched off (click to view PDF)

Do you realize how great that is? LilyPond does not only have a spacing engine that is clearly superior to the competition but it also provides a trivial switch to deactivate it when not appropriate! Do you think one of the other mentioned programs can deliver this? If I’m not mistaken they don’t even have a notion at all of a thing like optical spacing, let alone the option to conveniently switch back and forth between the modes.

But while I’m pretty happy with the rendering of this measure now our journey isn’t over yet. The lack of advanced features makes the competition produce better default results in this case – and that constellation isn’t really extravagant, so LilyPond should actually do better than this. What we need and would want LilyPond to do by default is Durand’s solution: space the quavers regularly but still apply optical improvements to the semiquavers. Or put the other way round: perform that optical spacing magic but notice when side-effects should limit its effect. As LilyPond isn’t able to do that yet we have opened a feature request, but this is probably a pretty involved task and can’t be expected to be implemented very soon. So for now I will have to look for an interim solution that doesn’t get too hacky and is somewhat future-proof. In the third and last part of this series I will demonstrate a number of techniques of separating content from presentation that will hopefully achieve that goal.

## 3 thoughts on “Optical Spacing (The Swan, Part 2)”

1. David Kastrup

I don’t really see this as a problem of optical spacing but rather of the alignment not following suit. The noteheads are aligned exactly vertically _in_ _spite_ of LilyPond having figured out that they appear optically displaced because of the accidentals and correcting for it.

For one thing, the combination with an accidental would allow loosening up the alignment by using a “spring” instead of a rigid placement as the note/accidental combination has a less sharp defined position than an isolated notehead. For another, adopting the shifted optical axis used for spacing would go a long way to defusing the result even with rigid alignment.

1. Urs Liska

I think we’re talking of two different issues here. If I understand you correctly you suggest that LilyPond could shift (for example) the fifth notehead of the cello line (the `cis'` in tenor clef) a little to the right, loosening the alignment with the piano noteheads in favor of a better visual impression.

What I am talking about isn’t related to accidentals at all. I’m referring to the fourth beat of the “LilyPond default spacing” example now: The fourth semiquaver `fis'` in the right hand of the piano is shifted to the left according to optical spacing. The following first note of the fifth beat is then started as close as possible, resulting in an irregular quaver line in the other voices.