[Tex/LaTex] Set music in LaTeX with ABC or LilyPond

lilypondmusic

I’ll give a tutorial, “LaTeX for musicians”, in the next year and certainly I want the to show how to include music in their texts. I’m a musician myself but I never used LaTeX to typeset music.

I found two ways to include music in a LaTeX document (plus: integrate music as image) and I want to share my thoughts and ask for some help (see below). First, let's take a more detailed look at both ways:

ABC

Code

% file abc.tex
\documentclass{scrartcl}
\usepackage{abc}

\begin{document}
Text
\begin{abc}[name=c-dur]
X: 1 % start of header
K: C % scale: C major
"Text"C4 G4 | (3FED c4 G2 |
\end{abc}
\end{document}

Compile

pdflatex --shell-escape abc.tex

Output

ABC output

LilyPond

Code

% file lp.lytex
\documentclass{scrartcl}

\begin{document}
Text
\begin[staffsize=12]{lilypond}
\relative c' {
  c2^"Text" g'2 \times 2/3 { f8 e d } c'2 g4
}
\end{lilypond}
\end{document}

Compile

lilypond-book --output=out-dir --pdf lp.lytex
cd out-dir
pdflatex lp.tex

Output

LilyPond output

Comparison

Now let’s talk about the pros and cons.

  • The ABC workflow is much easier than the one of LilyPond and it simply needs a call of pdflatex. For this it’s preferable for the students of the tutorial since most of them aren't computer geeks.
  • The ABC syntax seems to bee easier at a first glance.
  • LilyPond uses the right font i.e. the font of the LaTeX document.
  • abc.sty always produces a display style image, i.e., it starts a new line and centers it’s content. Furthermore there seems to be now option to shorten the line that they fit just the typed notes and not span the whole \linewidth (workaround: set width=0.5\abcwidth).
  • As far as I can see LilyPond is more flexible in the number of ways to typeset music (inline, display style, …) and the possibilities of typesetting in general.
  • The LilyPond music font looks better except the fat bar lines.

Questions

The numbers are not to give an order. I numbered the questions to make it easy to answer the …

  1. Which way would you prefer to use? Which one do you suggest for students how a not very good in using computers?
  2. I wonder if it will be possible to create a package that integrates LilyPond in the same way that abc.sty does width ABC notation
  3. Can one change the font of the ABC output so that it matches the LaTeX main font?
  4. Is there a way to make ABC find a good line length for the given number of notes?
  5. Can one change the width of LilyPonds bar lines?

Conclusion

I’d prefer to use (and teach) the LilyPond way but I fear the compilation workflow would scare the students. So if question 2 could be solved it would be great 🙂

I hope you understand what I want since my english may be not the best. Please ask if something is not clear …

Best Answer

It's not possible to use the same font as the document with ABC, for inherent limitations of the abcm2ps program. However, it's quite a long time since I don't update it and things might have changed (I'll take a look).

It should be easy to add "inline" versions of the music snippet inclusion, which is not there because the package was intended for inclusion of complete tunes.

Integration with LilyPond in the same way is not as easy. I have some "kind-of-working" code. A major hindrance is the difficulty of passing LilyPond parameters and directives from the TeX side (with lilypond-book it's the other way around).

Since there seems to be interest in this topic, I'll try and develop the code. For now, the following code

\begin{filecontents*}{testlily.ly}
\version "2.13.38" 

\include "config.ly" 

\relative c'' { 
c d e c 
}
\end{filecontents*}
\begin{filecontents*}{testlily3.ly}
\version "2.13.38" 

\include "config.ly" 

\relative c' {
  c2^"Text" g'2 \times 2/3 { f8 e d } c'2 g4
}

\end{filecontents*}
\documentclass[draft]{article} 
\usepackage[final]{graphicx} 
\usepackage[MacOSX,between-system-space=3]{lilypond} 
\usepackage{lipsum} 
\pagestyle{empty}
\begin{document}

Test: integrating \LaTeX{} and LilyPond. 

\lilypondfile{testlily} 

Not too bad, isn't it? 

\lipsum[2] 

\lilypondfile{testlily3} 

\end{document}

will produce the following output:

enter image description here

Related Question