Next: , Previous: An example of a musicological document, Up: LilyPond-book


14.2 Integrating LaTeX and music

LaTeX is the de-facto standard for publishing layouts in the exact sciences. It is built on top of the TeX typesetting engine, providing the best typography available anywhere.

See The Not So Short Introduction to LaTeX for an overview on how to use LaTeX.

Music is entered using

\begin[options,go,here]{lilypond}
  YOUR LILYPOND CODE
\end{lilypond}

or

\lilypondfile[options,go,here]{filename}

or

\lilypond{ YOUR LILYPOND CODE }

Running lilypond-book yields a file that can be further processed with LaTeX.

We show some examples here. The lilypond environment

\begin[quote,fragment,staffsize=26]{lilypond}
  c' d' e' f' g'2 g'2
\end{lilypond}

produces

[image of music]

The short version

\lilypond[quote,fragment,staffsize=11]{<c' e' g'>}

produces

[image of music]

Currently, you cannot include { or } within \lilypond{}, so this command is only useful with the fragment option.

The default line width of the music will be adjusted by examining the commands in the document preamble, the part of the document before \begin{document}. The lilypond-book command sends these to LaTeX to find out how wide the text is. The line width for the music fragments is then adjusted to the text width. Note that this heuristic algorithm can fail easily; in such cases it is necessary to use the line-width music fragment option.

Each snippet will call the following macros if they have been defined by the user:

\preLilyPondExample called before the music

\postLilyPondExample called after the music

\betweenLilyPondSystem[1] is called between systems if lilypond-book has split the snippet into several postscript files. It must be defined as taking one parameter and will be passed the number of files already included in this snippet. The default is to simply insert a \linebreak.

For printing the LaTeX document you need a DVI to PostScript translator like dvips. To use dvips to produce a PostScript file, add the following options to the dvips command line:

-o -Ppdf -h file.psfonts

where the filepsfonts file is obtained from lilypond-book, See Invoking lilypond-book, for details. PDF can then be produced with a PostScript to PDF translator like ps2pdf (which is part of GhostScript). Running dvips will produce some warnings about fonts; these are harmless and may be ignored.

If you are running latex in twocolumn mode, remember to add -t landscape to the dvips options.

Sometimes it is useful to display music elements (such as ties and slurs) as if they continued after the end of the fragment. This can be done by breaking the staff and suppressing inclusion of the rest of the lilypond output.

In LaTeX, define \betweenLilyPondSystem in such a way that inclusion of other systems is terminated once the required number of systems are included. Since \betweenLilypondSystem is first called after the first system, including only the first system is trivial.

\def\betweenLilyPondSystem#1{\endinput}

\begin[fragment]{lilypond}
  c'1\( e'( c'~ \break c' d) e f\)
\end{lilypond}

If a greater number of systems is requested, a TeX conditional must be used before the \endinput. In this example, replace "2" by the numer of systems you want in the output,

\def\betweenLilyPondSystem#1{
    \ifnum##1<2\else\endinput\fi
}

Remember that the definition of \betweenLilyPondSystem is effective until TeX quits the current group (such as the LaTeX environment) or is overridden by another definition (which is, in most cases, for the rest of the document). To reset your definition, write

\let\betweenLilyPondSystem\undefined

in your LaTeX source.

This may be simplified by defining a TeX macro

\def\onlyFirstNSystems#1{
    \def\betweenLilyPondSystem##1{\ifnum##1<#1\else\endinput\fi}
}

and then saying only how many systems you want before each fragment,

\onlyFirstNSystems{3}
\begin{lilypond}...\end{lilypond}
\onlyFirstNSystems{1}
\begin{lilypond}...\end{lilypond}


Next: , Previous: An example of a musicological document, Up: LilyPond-book

This page is for LilyPond-2.10.33 (stable-branch).

Report errors to http://post.gmane.org/post.php?group=gmane.comp.gnu.lilypond.bugs.