# How to use lilypond-book with custom fonts?

I am trying to create a LaTeX (more precisely, XeLaTeX) document that contains lilypond snippets. I am using lilypond-book to create a .tex file. Within my (LaTeX) document, I use a (custom) TTF font. I would like my lyrics to appear in the same font. By using the \paper environment and the #(define fonts (make-pango-font-tree ...)) commands described here, for example, I can use that font within lilypond. I verified this by running the lilypond command on my (separate) lilypond file and by viewing the result in a PDF viewer.

Now, I basically want to get the same results within my LaTeX+Lilypond document. As far as I understand, lilypond-book mostly ignores the \paper section within a lilypond file. Is there a way to set the font from within a lilypond file in a way such that it is respected by lilypond-book?

I assume that there is an easy answer to this question, but I could not find any clues in the excellent lilypond documentation or elsewhere.

## 3 Answers

I'm not entirely sure what the problem is here, but I suspect that it was an issue with the particular version of lilypond-book you were using. If I do something like this:

\documentclass{scrartcl}

\usepackage{fontspec}

\newcommand*{\defaultfontfamily}{Calluna}%
\defaultfontfeatures{Mapping=tex-text, Ligatures={Discretionary, Common, Rare}}%
\setmainfont{\defaultfontfamily}%

\begin{document}

\subsection*{Lilypond \lilypondversion}

\begin{lilypond}
\paper {
myStaffSize = #20
#(define fonts
(make-pango-font-tree "Calluna"
"Calluna Sans"
"Luxi Mono"
(/ myStaffSize 20)))
}
\new Voice {
\time 3/4
g2 e4
a2 f4
g2.
}
\addlyrics {
\set stanza = #"1. "
Hi, my name is Bert.
}
\addlyrics {
\override StanzaNumber #'font-name = #"Calluna"
\set stanza = #"2. "
\override LyricText #'font-family = #'roman
Oh, ché -- ri, je t'aime
}
\end{lilypond}

\subsection*{Not lilypond}
Oh, ché -- ri, je t'aime

\end{document}


and then run:

lilypond-book --latex-program=xelatex test.lytex && xelatex test.tex


I get the following output:

Note the use of Calluna in both places. Since the lilypond part of the document is getting its fonts from pango (not fontspec/xelatex), any OpenType features that show up in the TeX document won't show up in the Lilypond output... at least you get consistent fonts though!

While I did not find a clean way to solve my issue, I could solve my problem. Just in case that there is no elegant solution to this problem, I provide an ugly-hack solution: I'm going to modify lilypond-book and add the #(define ...) statement manually (and hard-coded) to the paper (for all snippets):

Edit the python module book_snippets.py that comes with lilypond. In my case it is located in /usr/share/lilypond/2.14.2/python/book_snippets.py and search for PREAMBLE_LY within this file. Within the definition of PREAMBLE_LY, there is a \paper block. Add anything to it that you want, but keep in mind that lilypond-book sets some of the values itself and that some parameters can be overridden from within your LaTeX files. To my best knowledge, font definitions, unfortunately, can't be specified from LaTeX (as of version 2.14.2 of lilypond). You can easily write more maintainable solutions by adding parameters to this module that are specific to your requirements.

Interesting, I didn't know that lilypond-book would not respect your font declarations like that. So, I don't have a direct answer but two alternative approaches occur to me.

The first is to avoid lilypond-book altogether and just compile the lilypond code separately as snippets and then insert the pdfs into your XeLaTeX document. From what I understand this is what lilypond-book does anyway. I do this for most of my scores where I use Lilypond to produce the full pages and then LuaTeX to handle the front and back covers and all the other text. I know snippets are trickier to deal with than full pages but it should work pretty well.

The other is to learn MusixTeX. That's what I'm doing on my latest project. It will have around 1,200 snippets of music so instead of fiddling with lilypond-book or even just lilypond and inserting all of those outside pdfs I'm doing them in MusixTeX. The syntax is different than lilypond. Crazily so. And the learning curve steeper. And it doesn't have all the nice automatic spacing algorithms. But once you get the hang of the syntax it ain't so bad and it makes compiling and introducing snippets much easier since it does not rely on any outside software or multiple compiles.

A third crazier idea. Use Lilypond-book to produce the document with the snippets sans text and then use XeLaTeX to add the text you want on top of the lilypond snippets.

• Thanks for your suggestions. Unfortunately, I could not consider any other tools, because my current project was almost finished. I decided to slightly modify lilypond-book. – Markus Mayr Jul 10 '13 at 21:00