Typevia
Field-Specific Applications

Introduction to Music Notation in LaTeX

Typesetting music within a document is a specialized task. While LaTeX is primarily known for mathematical and scientific text, it is also an incredibly powerful tool for creating professional-grade musical scores. Whether you are writing a musicology thesis, a songbook, or an instrumental method, LaTeX provides several ways to integrate music notation directly into your workflow.

The primary advantage of using LaTeX for music is the seamless integration of text and notation. Instead of copying and pasting low-resolution images from external software, you can define your music using code, ensuring that the fonts and layouts match your document perfectly. In this guide, we will explore the three most common methods: MusiXTeX, ABC Notation, and LilyPond integration.


Getting Started with MusiXTeX

MusiXTeX is the traditional, "native" way to write music in LaTeX. It is a set of macros that allows for sophisticated typesetting of complex scores. However, it has a steep learning curve because its syntax is quite dense.

Basic Structure

In MusiXTeX, you define the number of instruments (staffs) and then write the notes using commands that represent their pitch and duration.

\usepackage{musixtex}

\begin{document}

\begin{music}
    \instrumentnumber{1} % One instrument
    \setstaffs1{1}       % One staff for instrument 1
    \startextract        % Begin the musical snippet
        \notes \qu{c} \qu{e} \qu{g} \hu{j} \en
    \zendextract         % End the snippet
\end{music}

\end{document}

Understanding the Commands

  • \qu{pitch}: A quarter note (crotchet).
  • \hu{pitch}: A half note (minim).
  • \notes ... \en: A horizontal slice of music where notes are aligned vertically.
  • Pitch labels: MusiXTeX uses letters where a-h represent notes in the lower octave and i-p represent the next octave.

Note: MusiXTeX requires a three-pass compilation process (latex -> musixflx -> latex) to correctly calculate the horizontal spacing of notes. Most modern LaTeX editors can be configured to do this automatically.


Using ABC Notation for Simple Melodies

For beginners or those who only need to include simple melodies, lead sheets, or folk tunes, the ABC package is often the easiest entry point. ABC is a shorthand notation system that uses standard ASCII characters.

How it Works

The abc package allows you to write music in a human-readable format inside an abc environment.

\usepackage{abc}

\begin{document}

\begin{abc}[name=simple-tune]
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 |]
\end{abc}

\end{document}

Key Components of ABC

  1. Headers: X: (Reference number), T: (Title), M: (Meter/Time signature), K: (Key).
  2. Notes: Capital letters CDEFGAB are the middle octave. Adding a comma (e.g., C,) goes down an octave; lowercase letters (e.g., c) go up an octave.
  3. Rhythms: A number after a note (e.g., G2) multiplies its length.

Tip: The abc package requires the -shell-escape flag enabled in your LaTeX compiler settings because it calls an external program (abcm2ps) to generate the music.


High-Quality Notation with LilyPond (lyluatex)

LilyPond is widely considered the "gold standard" of open-source music engraving. While it is a separate program from LaTeX, the lyluatex package allows you to embed LilyPond code directly into your LaTeX document using the LuaLaTeX engine.

Why use LilyPond?

LilyPond produces much more aesthetic results than MusiXTeX with far less effort. It handles collision detection, complex beaming, and lyrical alignment automatically.

% Use LuaLaTeX to compile this!
\usepackage{lyluatex}

\begin{document}

Here is a snippet of a C Major scale:

\begin{lilypond}
\relative c' {
  c4 d e f | g a b c |
}
\end{lilypond}

\end{document}

Syntax Breakdown

  • \relative c': Tells LilyPond that notes should be placed in the octave closest to "middle C".
  • c4: A 'C' note that is a quarter note (the number represents the denominator of the duration).
  • LilyPond stays in the same duration until you change it. If you write c8 d e f, all four will be eighth notes.

Adding Lyrics and Dynamics

A common requirement is adding text or performance instructions to the music. In the lyluatex approach, this is very intuitive.

\begin{lilypond}
\relative c' {
  \clef treble
  \key c \major
  \time 4/4
  c4\f d e( f) | g2 g\p |
}
\addlyrics {
  Sing a song to- day.
}
\end{lilypond}

Essential Symbols:

  • Dynamics: Use a backslash followed by the dynamic (e.g., \f for forte, \p for piano, \ff for fortissimo).
  • Slurs: Use parentheses around the notes you want to slur (e.g., c( d e)).
  • Lyrics: The \addlyrics command automatically aligns syllables with the notes above them. Use a hyphen -- to split syllables across notes.

Best Practices and Tips

To ensure your musical documents look professional and compile without errors, follow these best practices:

  1. Choose the Right Engine: If you want to use the most modern features, use LuaLaTeX with the lyluatex package. It is significantly easier to manage than the older musixtex workflows.
  2. Keep Snippets Short: If you are writing a book about music, keep your musical examples in small blocks. This helps LaTeX manage page breaks more effectively.
  3. Use Comments: Music code can become cluttered. Use % to comment your code, especially to mark sections (e.g., % Chorus, % Measure 10).
  4. External Files: For very large scores, save the music in a separate .ly or .abc file and use commands like \lilypondfile{filename.ly} to keep your main LaTeX file clean.

Common Pitfalls to Avoid

  • Missing Dependencies: Most LaTeX music packages rely on external software (like LilyPond or Ghostscript). Ensure these are installed on your system and added to your "Path" environment variable.
  • Shell Escape: If your music isn't appearing, check if you have enabled --shell-escape. This is a security feature in LaTeX that must be turned on to allow LaTeX to "talk" to the music engines.
  • Octave Confusion: In both LilyPond and MusiXTeX, it's easy to lose track of which octave you are in. Always use "relative" mode in LilyPond to make the code more predictable.
  • Font Conflicts: Occasionally, music fonts can conflict with text fonts. Stick to standard packages like geometry to manage your page margins to ensure the music stays within the printable area.

By mastering these tools, you can transform LaTeX from a simple text processor into a powerful workstation for musical engraving. Start with the abc package for simple tasks, and migrate to lyluatex when you are ready for professional-quality publishing.