## Wednesday, February 16, 2011

### Typesetting music: lilypond and latex

I made it through the first four exercises before I flipped ahead and thought, "wow, there a lot of exercises. It would be nice if someone had mad a workbook to accompany the text." Then I thought, "I should make a workbook to accompany the text!" Then I went off the deep-end figuring out how to typeset music in lilypond and LaTeX.

I've now typeset the first 60 exercises-- the two-voices section of the book. Check out a sample page in PDF-- it looks cool! I'm going to stop, now, for fear of burning out. I'll go back to working on the music, and typeset three-voice counterpoint after I finish two-voice. Here are a few of the things I've learned:

To integrate lilypond scores into a LaTeX document:

First, in your LaTeX document, reference the file with your score, e.g:

\lilypondfile{WB.1.2.E.a.ly}

Then, when you want to compile the typeset file (mine is called "WORKBOOK.tex") do these steps:
`\$ mkdir out\$ lilypond-book --pdf --output=out WORKBOOK.tex\$ cd out\$ pdflatex WORKBOOK.tex`

This is pretty self-explanatory: lilypond-book creates a new TeX document, converting the score sources into images and LaTeX code, then dumps it into the "out" directory.

Naming conventions:

I've settled on this naming convention, to help me keep track of proliferating lilypond files:

<species>.<voices>.<mode>.<above/below>

For example: 1.2.E.b.ly

Meaning first species, two voices, phrygian mode, counterpoint below the cantus firmus.

Because Fux re-uses the same cantus firmi throughout the book, I can re-use the same scores five times in each section of the workbook, once for each species. Including the species number helps me keep track of completed exercises, though.

Landscape formatting in LaTeX:

Including "landscape" as an option under \documentclass will format landscape. If you use pdflatex to compile the source, it handles everything. If you're using latex to compile the source, you've got to tell latex to respect the landscape option. And then you have to tell dvips to respect the landscape option. Pdflatex is the way to go.

Empty bars in lilypond:

Since I'm making a workbook, I need blank staves that I'll fill in by hand. Lilypond freaks out about bars with no notes in them, though. The way around is to fill in every bar with a whole note, and then use \hideNotes and \unHideNotes around the set of bars I want empty. A similar trick can make roomy, regularly-sized empty bars for workbooks.

Formatting single-line scores:

All the Fux exercises are one-line scores. I want to use all the available page space, to make them easier to write on. That means reducing the default indent and turning off the ragged right edge. (Lilypond indents the first system of any score, and doesn't justify full the last system in a score.) To accomplish this, I've included this bit at the top of every score:

\paper {
indent = 10\mm
ragged-right = ##f
}

Also at the top of every score is this: \version "2.12.3"

Apparently lilypond somewhat regularly breaks backwards compatibility. When they do, they release a script that converts old scores to the new version, and that script looks for a version string at the top of the file it's crunching.