Skip to main content
โšก Calmops

LaTeX Music Typesetting: MusiXTeX, LilyPond, and ABC Notation

Introduction

LaTeX can typeset musical notation through several packages and tools. For simple notation embedded in documents, MusiXTeX works directly in LaTeX. For professional-quality sheet music, LilyPond is the gold standard and integrates cleanly with LaTeX via lilypond-book. This guide covers both approaches.

MusiXTeX: Music Notation in LaTeX

MusiXTeX is a LaTeX package for typesetting music directly in your document. It’s complex but powerful for embedding musical examples in academic papers.

Setup

\documentclass{article}
\usepackage{musixtex}
\usepackage{musixlyr}  % for lyrics

\begin{document}

Basic Notes

MusiXTeX uses a pitch-based notation system:

\begin{music}
\instrumentnumber{1}
\setstaffs{1}{1}
\setclef{1}{\treble}
\startextract

% Notes: \qu{pitch} = quarter note, \hu{pitch} = half note, \wu{pitch} = whole note
% Pitches: c d e f g h(=a) i(=b) and uppercase for higher octave
\notes \qu{c}\qu{d}\qu{e}\qu{f} \bar \qu{g}\qu{h}\qu{i}\wu{j} \endbar
\endextract
\end{music}

Note Duration Reference

\wu{c}   % whole note (semibreve)
\hu{c}   % half note (minim)
\qu{c}   % quarter note (crotchet)
\cu{c}   % eighth note (quaver)
\ccu{c}  % sixteenth note (semiquaver)

% Rests
\wr      % whole rest
\hr      % half rest
\qr      % quarter rest
\cr      % eighth rest

Chords and Beaming

\begin{music}
\instrumentnumber{1}
\setstaffs{1}{1}
\setclef{1}{\treble}
\startextract

% Chord: \zq stacks notes
\notes \zq{c}\qu{e} \zq{d}\qu{f} \zq{e}\qu{g} \zq{f}\qu{h} \endbar

% Beamed eighth notes
\notes \ibu0c0 \qb0{cde} \tbu0\qb0{f} \endbar

\endextract
\end{music}

Clefs and Key Signatures

\setclef{1}{\treble}   % treble clef
\setclef{1}{\bass}     % bass clef
\setclef{1}{\alto}     % alto clef

% Key signatures (number of sharps/flats)
\generalsignature{2}   % 2 sharps (D major)
\generalsignature{-3}  % 3 flats (Eb major)

% Time signatures
\generalmeter{\meterfrac{4}{4}}
\generalmeter{\meterfrac{3}{4}}
\generalmeter{\meterfrac{6}{8}}

A Complete Example: C Major Scale

\begin{music}
\instrumentnumber{1}
\setstaffs{1}{1}
\setclef{1}{\treble}
\generalmeter{\meterfrac{4}{4}}
\startextract

\notes \qu{c}\qu{d}\qu{e}\qu{f} \bar
       \qu{g}\qu{h}\qu{i}\wu{j} \endbar

\endextract
\end{music}

LilyPond: Professional Sheet Music

LilyPond is a dedicated music engraving program that produces publication-quality sheet music. It integrates with LaTeX via lilypond-book.

LilyPond Syntax Basics

% A simple melody in LilyPond
\version "2.24.0"

\relative c' {
  \clef treble
  \key c \major
  \time 4/4

  c4 d e f |
  g a b c |
  c b a g |
  f e d c2 |
}

Note Syntax

% Pitches: c d e f g a b
% Octave: ' raises one octave, , lowers one octave
c'   % middle C
c''  % C one octave above middle C
c    % C below middle C

% Durations: 1=whole, 2=half, 4=quarter, 8=eighth, 16=sixteenth
c4   % quarter note C
d2   % half note D
e1   % whole note E
f8   % eighth note F

% Dotted notes
c4.  % dotted quarter (= 3/8)
d2.  % dotted half (= 3/4)

% Rests
r4   % quarter rest
r2   % half rest

Chords and Dynamics

\relative c' {
  % Chord: notes in angle brackets
  <c e g>4 <d f a> <e g b> <f a c> |

  % Dynamics
  c4\p d\mp e\mf f\f |
  g4\ff a\fff b\ffff c |

  % Crescendo/decrescendo
  c4\< d e f\! |
  g4\> a b c\! |
}

Integrating LilyPond with LaTeX (lilypond-book)

% In your .lytex file (LaTeX + LilyPond)
\documentclass{article}
\begin{document}

Here is a musical example:

\begin{lilypond}
\relative c' {
  \clef treble
  \key g \major
  \time 3/4
  g4 a b |
  c2 b4 |
  a2. |
  g2 r4 |
}
\end{lilypond}

The melody above is in G major.

\end{document}

Compile with:

lilypond-book --output=out document.lytex
cd out
pdflatex document.tex

LilyPond for Piano (Grand Staff)

\version "2.24.0"

upper = \relative c'' {
  \clef treble
  \key c \major
  \time 4/4
  e4 f g a |
  b c d e |
}

lower = \relative c {
  \clef bass
  \key c \major
  \time 4/4
  c4 d e f |
  g a b c |
}

\score {
  \new PianoStaff <<
    \new Staff \upper
    \new Staff \lower
  >>
}

ABC Notation: Simple Text-Based Music

ABC notation is a plain-text music format that can be converted to sheet music or MIDI:

X:1
T:Twinkle Twinkle Little Star
M:4/4
L:1/4
K:C
C C G G | A A G2 | F F E E | D D C2 |
G G F F | E E D2 | G G F F | E E D2 |
C C G G | A A G2 | F F E E | D D C2 |

Convert to PDF with abcm2ps or include in LaTeX with the abc package:

\usepackage{abc}

\begin{abc}
X:1
T:Simple Melody
M:4/4
L:1/4
K:C
C D E F | G A B c |
\end{abc}

Choosing the Right Tool

Tool Best For Complexity Output Quality
MusiXTeX Simple examples in LaTeX docs High Good
LilyPond Professional sheet music Medium Excellent
ABC notation Quick sketches, folk music Low Good
Sibelius/Finale Professional composition Medium Excellent
MuseScore Free professional notation Low Excellent

For most academic papers needing musical examples, LilyPond + lilypond-book provides the best quality with reasonable effort. For standalone sheet music, LilyPond or MuseScore are the best free options.

Resources

Comments