Music notation (MusiXTeX/LilyPond)

Mathematics is LaTeX’s home turf — but what about sheet music? There are really three roads. MusiXTeX engraves scores inside TeX itself; LilyPond, a separate high-quality engraver, can be pulled in with lilypond-book; and the compact ABC notation can be embedded with the abc package. This page walks through how each works and which to reach for.

MusiXTeX — engraving inside TeX

MusiXTeX is a music-typesetting system built purely from TeX macros. It is the successor to the older MusicTeX, and it draws staves, noteheads, beams, and slurs entirely within the TeX framework. Because it leans on no external drawing program, it suits you when you want to keep the whole toolchain inside TeX.

Its processing model is distinctive. To optimize the spacing between notes, MusiXTeX runs in three passes. TeX (in practice e-TeX) processes the file once and writes a .mx1 file; the bundled Lua script **musixflx** reads it, computes the spacing for each beat, and emits a .mx2 file; then TeX runs a final time to produce the output. A wrapper script called **musixtex** orchestrates all three runs for you, invoking preprocessors (the pmx / M-Tx front-ends below) as needed.

terminal
musixtex score.tex   # runs e-TeX → musixflx → e-TeX, then makes the PDF

The raw MusiXTeX macros are universally acknowledged to be hard to write by hand, so almost nobody does. Instead you use a higher-level front-end. **pmx is a preprocessor that compiles a far simpler input language down to MusiXTeX macros. For vocal music with lyrics, M-Tx** sits one stage earlier and feeds pmx, giving the chain M-Tx → pmx → MusiXTeX. Below is a bare MusiXTeX skeleton (in practice you would more likely write pmx input).

document.tex
\input musixtex
\begin{music}
  \instrumentnumber{1}
  \setclef{1}{\treble}
  \startextract
    \Notes \qa{cdef} \en
    \Notes \ha{g} \en
  \endextract
\end{music}
\end

LilyPond & lilypond-book — bringing in a separate engine

LilyPond is a dedicated score engraver with its own input language. It is a program separate from TeX and is widely regarded as producing the highest-quality engraving. Its input looks nothing like MusiXTeX’s, yet a plain notation yields results at least as polished. The standard way to combine it with a LaTeX document is **lilypond-book**.

The idea: in a file with the extension **.lytex, you intermix LaTeX prose with LilyPond fragments**. Short fragments go in \lilypond{…}, longer ones in a lilypond environment, and external files via \lilypondfile{…}. Running lilypond-book over it renders each fragment with LilyPond into an image (or PDF) and writes a **plain .tex file** in which the fragments are replaced by graphics-inclusion commands. You then compile that as usual, and the score blends into the document.

document.tex
% --- score.lytex ---
\documentclass{article}
\begin{document}
Here is a short phrase:
\begin{lilypond}[quote,fragment,staffsize=26]
  c'4 d' e' f' g'2 g'
\end{lilypond}
\end{document}

The build has two stages. First run lilypond-book on the .lytex file (pass --pdf to embed fragments as PDF), then compile the generated .tex with pdflatex or similar. The line width of the music is matched automatically by reading the text width out of your preamble.

terminal
lilypond-book --pdf score.lytex   # renders snippets, writes score.tex
pdflatex score.tex                # compile the generated document

A more modern option is the **lyluatex** package. Built for LuaLaTeX, it skips the lilypond-book preprocessing step and calls LilyPond directly during the compile to render and include scores. You write music in a lily environment or with \lilypond, and simply start lualatex with **--shell-escape**. It is positioned as a drop-in superset of lilypond-book.

The abc package — simple ABC notation

The **abc package embeds ABC notation — a very compact text music format — into LaTeX. ABC is designed above all to be readable and writable by humans, and is widely used to record folk tunes and lead sheets (single-line melodies)** of Western European origin such as Irish, English, and Scottish music.

Like lilypond-book, it relies on an external converter. Music written in an abc environment is handed by the package, via \write18 (shell execution), to **abcm2ps, which renders it to a score image that is then included in the document. Because it uses shell execution, you must compile with -shell-escape. There is also a classic tool, abc2mtex**, that converts ASCII ABC into MusicTeX/MusiXTeX input, usable as a preprocessor.

document.tex
\documentclass{article}
\usepackage{abc}
\begin{document}
\begin{abc}
X:1
T:Simple Tune
M:4/4
L:1/8
K:C
CDEF|GABc|
\end{abc}
\end{document}
terminal
pdflatex -shell-escape tune.tex   # abc calls abcm2ps via \write18

Which to choose

A rough guide. If you want to keep the toolchain inside TeX and do elaborate engraving without leaving it, use MusiXTeX (in practice via pmx / M-Tx). If quality comes first for serious scores, bring in LilyPond through lilypond-book (or lyluatex if you use LuaLaTeX). And if you just need folk tunes or short single-line melodies quickly, reach for the abc package.

ApproachHow it worksBest for
MusiXTeXEngraved by TeX macros (3-pass + pmx / M-Tx)Staying in TeX; elaborate engraving
LilyPond (lilypond-book)Rendered by an external engine → image/PDF includedTop-quality, serious scores
lyluatexLuaLaTeX calls LilyPond live during the compileLilyPond made easy under LuaLaTeX
abcABC notation converted by abcm2ps and embeddedFolk tunes and single-line lead sheets

All of these live on CTAN and ship with the major distributions such as TeX Live. LilyPond itself (and thus lilypond-book) may need a separate install. Note that the externally-driven abc and lyluatex both require **-shell-escape** to permit shell execution.