• Martin Thoma
  • Home
  • Categories
  • Tags
  • Archives
  • Support me

How to write music with LaTeX

Contents

  • Symbols
  • musixtex
  • ABC
    • Preparation
    • Example
  • LilyPond
    • Preparation
    • Example
  • Further Reading

It is possible to write music with LaTeX. My girlfriend was quite surprised of this, so I decided to write a little tutorial show some examples.

Symbols

Some basic music symbols
Some basic music symbols
\documentclass[a4paper,12pt]{article}
\usepackage{wasysym}
\begin{document}
\eighthnote ~~~ \halfnote ~~~ \twonotes ~~~ \fullnote ~~~
\quarternote ~~~ $\natural$ ~~~ $\flat$ ~~~ $\sharp$
\end{document}

The harmony package offers some additional symbols:

music symbols form the LaTeX-harmony package
music symbols form the LaTeX-harmony package
\documentclass[a4paper,12pt]{article}
\usepackage{harmony}

\begin{document}
    \noindent \AAcht ~~~ \Acht ~~~ \AchtBL ~~~ \AchtBR ~~~ \AcPa \\
    \DD ~~~ \DDohne ~~~ \Dohne ~~~ \Ds ~~~ \DS \\
    \Ganz ~~~ \GaPa ~~~ \Halb ~~~ \HaPa ~~~ \Pu ~~~ \Sech \\
    \SechBL ~~~ \SechBl ~~~ \SechBR  ~~~ \SePa ~~~ \UB ~~~ \Vier \\
    \ViPa ~~~ \VM ~~~ \Zwdr ~~~ \ZwPa
\end{document}

musixtex

musixtex example
musixtex example
\documentclass[a4paper,12pt]{article}
\usepackage{musixtex}

\begin{document}
    \noindent This is a clef:
    \begin{music}\trebleclef\end{music}
    - a simple example\\
    for the \LaTeX{} package musixtex.
\end{document}

ABC

Preparation

You have to have ABC installed. For Ubuntu-Users:

sudo apt-get install abcm2ps

Example

\documentclass[a4paper]{article}
\usepackage{abc}

\begin{document}
    You can create music sheets within the abc-environment:
    \begin{abc}[name=c-dur]
        X: 1 % start of header
        K: C % scale: C major
        "Text"c2 G4 | (3FED c4 G2 |
    \end{abc}
\end{document}

compile with

pdflatex --shell-escape myTexFile.tex

to get this:

ABC example for creating music sheets with LaTeX
ABC example for creating music sheets with LaTeX

LilyPond

Preparation

Make sure that you have installed GNU LilyPond and LaTeX.

Ubuntu-Users have to type

sudo apt-get install lilypond

to install Lilypond.

Example

From the Documentation

Save the following source as lilybook.lytex:

\documentclass[a4paper]{article}

\begin{document}
    Documents for \verb+lilypond-book+ may freely mix music and text.
    For example,

    \begin{lilypond}
        \relative c' {
          c2 g'2 \times 2/3 { f8 e d } c'2 g4
        }
    \end{lilypond}

    Options are put in brackets.
    \begin[fragment,quote,staffsize=26,verbatim]{lilypond}
      c'4 f16
    \end{lilypond}

    Larger examples can be put into a separate file, and introduced
    with \verb+\lilypondfile+.
\end{document}

Compile it with these commands:

lilypond-book --output=out --pdf lilybook.lytex
cd out/
pdflatex lilybook
mv lilybook.pdf ../lilybook.pdf
cd ..
rm -rf out

For simplification, you can save this as compile.sh, execute chmod +x compile.sh and now you only have to enter ./compile.sh to generate the PDF.

Output:

Lilypond example - output was a PDF
Lilypond example - output was a PDF

Further Reading

  • ABC
    • ABC-environment Documentation
  • LilyPond
    • LilyPond — Learning Manual
    • Lilypond 2.14 Documentation
    • Lilypond: Free, Beautiful Music Notation Engraving for Anyone by Peter Kirn
    • Including Lilypond in LaTeX
    • Beautiful example
  • Creating a custom songbook with the songs package
  • MusiXTeX

If you've made some more complex examples with LaTeX, I'd be happy if you added them in the comments.

Published

Aug 1, 2012
by Martin Thoma

Category

The Web

Tags

  • LaTeX 29
  • music 2
  • note 1
  • sheet music 1

Contact

  • Martin Thoma - A blog about Code, the Web and Cyberculture
  • E-mail subscription
  • RSS-Feed
  • Privacy/Datenschutzerklärung
  • Impressum
  • Powered by Pelican. Theme: Elegant by Talha Mansoor