## Sunday, November 1, 2009

### Making Lilypond work with TeXShop

I have a confession to make. I have a secret desire to someday write a book on music theory, if only to have the opportunity to start the chapter on key signatures with a quote from Harry Chapin:

Those of you who are musically inclined will note that we have switched to a minor key, which of course means that the plot is about to thicken.

But, hey, how to pull this off? Typesetting a book that would contain equations describing the properties of sound is tough. The best tool for that would be LaTeX (with which I am fond of using the TeXShop front-end). And music engraving is even tougher. Quite frankly, most computer-generated music notation is of very poor quality. Fortunately there is Lilypond, which ostensibly can be made to work with TeXShop.

For the benefit of those not in the know, TeX/LaTeX is a very old system for doing page markup and typesetting, translating markup codes in text files into DVI, Postcript, or PDF files. I was first exposed to it as an undergrad on a VAXcluster back in the late eighties, and it remains the de facto standard for papers submitted for publication in scientific and mathematical journals since it contains very powerful commands for formatting mathematical expressions. Since it is command-line based, many graphical front-ends exist for simplifying its use in the GUI era, of which TeXShop is but one example.

Similarly, Lilypond interprets its own markup commands from a text file to generate engraved music, the output of which can be incorporated into the output of the LaTeX processor.

So I downloaded LilyPond and followed the instructions for making it work with TeXShop, but I kept running into errors which my Unixy shell-foo skills were inadequate to resolve. Finally, after a few months of ocassionally twiddling with the issue, I found an example of settings for the Lilypond-LaTeX.engine configuration file which worked a little further than any others I had found. After correcting some minor errors in it, I wound up with the following:

#!/bin/bash
export PATH=$PATH:/Applications/LilyPond.app/Contents/Resources/bin # Delete existing output (PDF) file rm "$1".pdf
# Create directory to put all temporary stuff
mkdir -p "$1_r"-out/ # Invoke L-B lilypond-book --output="$1_r"-out --pdf "$1" cd "$1_r"-out
pdflatex "$1" # Copy output (PDF) file to parent directory mv *.pdf .. # Delete temporary stuff rm -rf * cd .. # Delete (empty) temp directory rmdir "$1_r"-out
# Display output (PDF) file
open "\$1".pdf
Note that this verson contains no error checking in the shell commands, so if it fails, it will not fail gracefully, but at least it works. The end result? Given the following .tex file:

\documentclass[12pt]{amsart}
\usepackage{geometry} % see geometry.pdf on how to lay out the page. There's lots.

\title{Lilypond Example}
\author{Glen Mark Martin}

\begin{document}

\maketitle

In Western music, the primary reference tone is the pitch A above middle C, which is defined by ISO standards as having a fundamental frequency of 440Hz. In twelve-tone equal temperament, the frequencies of all other tones within that octave are related to this pitch according to the following formula:

\begin {displaymath}
F(n) = 440 \times 2^{ \frac{n}{12} }
\end {displaymath}

To find the frequency of the note in the next octave up, simply double the frequency. To go down an octave, divide the frequency by two.

With frequency intervals thus defined, we can begin dividing the continuous acoustic spectrum into discretized structures known as scales. Here is a one octave chromatic scale:

\begin[quote,fragment,staffsize=16]{lilypond}
\set Score.timing = ##f
\override Staff.TimeSignature #'transparent = ##t
\relative c' {
c4 cis d dis e f fis g gis a ais b c
}

\end{lilypond}
\end{document}

I end up with the following output:

#### 1 comment:

Mark said...

Thank you for the script; I had been running into problems, and this is a simple and effective solution.